Android 无法从字符序列中提取主题值

Android 无法从字符序列中提取主题值,android,listview,Android,Listview,//testselect.java package com.example.eleave; import java.util.ArrayList; import android.annotation.SuppressLint; import android.app.Activity; import android.app.AlertDialog; import android.app.Dialog; import android.content.DialogInterface; import

//testselect.java

package com.example.eleave;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.util.SparseBooleanArray;
import android.view.View;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class TestSelect extends Activity {
    Intent i;
    Button btn4;
    CharSequence[] options;
    protected ArrayList<CharSequence> selectedSubjects = new ArrayList<CharSequence>();
     boolean[] selections;
     String s1;
     String sub[]=new String[5];
     TextView tv2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.testselect);
        i=getIntent();
        s1=i.getStringExtra("data1");
        tv2=(TextView) findViewById(R.id.textView2);
        btn4=(Button) findViewById(R.id.button4);

        btn4.setOnClickListener(new ButtonClickHandler());
         Bundle bundle = getIntent().getExtras();
         String username = bundle.getString("username"); 
         String reason = bundle.getString("reason");
         String sem = bundle.getString("sem"); 
         String from = bundle.getString("from");
         String to = bundle.getString("to");
         AsyncTask<String,Void,String> t1=new LeaveTask(TestSelect.this).execute(username,sem,reason,from,to);
            try{
                String result=t1.get();
                Toast.makeText(getBaseContext(), result,Toast.LENGTH_LONG).show();
            }catch(Exception e)
            {

            }

    }
     public class ButtonClickHandler implements View.OnClickListener {
            public void onClick( View view ) {
                if(s1.equals("III"))
                {

                    options=new CharSequence[]{"DSPD","CAO","BDP"};
                     selections =  new boolean[ options.length ];

                }
                else if(s1.equals("IV"))
                {
                    options=new CharSequence[] {"TOFCS","OOP","OS"};
                    selections =  new boolean[ options.length ];

                }
                else if(s1.equals("V"))
                {
                    options=new CharSequence[] {"MI","CG","SP"};
                    selections =  new boolean[ options.length ];

                }
                else
                {
                    options=new CharSequence[] {"DAA","DBMS","ICWS"};
                    selections =  new boolean[ options.length ];

                }
                showDialog( 0 );

                  /* do whatever you want with the checked item */
                 }  

        }

    @Override
    protected Dialog onCreateDialog( int id ) 
    {
        return 
        new AlertDialog.Builder( this )
            .setTitle( "SUBJECTS" )
            .setMultiChoiceItems( options, selections, new DialogSelectionClickHandler() )
            .setPositiveButton( "OK", new DialogButtonClickHandler() )
            .create();

    }

    public class DialogSelectionClickHandler implements DialogInterface.OnMultiChoiceClickListener
    {
        public void onClick( DialogInterface dialog, int clicked, boolean selected )
        {
            Log.i( "ME", options[ clicked ] + " selected: " + selected );
            if(selected)
            {
                selectedSubjects.add(options[clicked]);
            }
            else
            {
                selectedSubjects.remove(options[clicked]);
            }
        }
    }

    public class DialogButtonClickHandler implements DialogInterface.OnClickListener
    {
        private final String NULL = null;
        boolean[] checkedSubjects = new boolean[options.length];
        int count = options.length;




        public void onClick( DialogInterface dialog, int clicked, boolean isChecked)
        {

            switch( clicked )
            {
                case DialogInterface.BUTTON_POSITIVE:


                    printSelectedSubjects();

                    break;
            }
        }

        private void printSelectedSubjects() {
            // TODO Auto-generated method stub
            for( int i = 0; i < options.length; i++ ){

                Log.i( "ME", options[ i ] + " selected: " + selections[i] );
            }

            for(int i = 0; i < count; i++)
                checkedSubjects[i] = selectedSubjects.contains(options[i]);

            StringBuilder stringBuilder = new StringBuilder();

            for(CharSequence subject : selectedSubjects)
                stringBuilder.append(subject + ",");
            tv2=(TextView) findViewById(R.id.textView2);
            tv2.setText(stringBuilder.toString());
        }

        @Override
        public void onClick(DialogInterface arg0, int arg1) {
            // TODO Auto-generated method stub

        }
    }


}
package com.example.eleave;
导入java.util.ArrayList;
导入android.annotation.SuppressLint;
导入android.app.Activity;
导入android.app.AlertDialog;
导入android.app.Dialog;
导入android.content.DialogInterface;
导入android.content.Intent;
导入android.os.AsyncTask;
导入android.os.Bundle;
导入android.util.Log;
导入android.util.SparseBooleanArray;
导入android.view.view;
导入android.widget.Button;
导入android.widget.Spinner;
导入android.widget.TextView;
导入android.widget.Toast;
公共类TestSelect扩展了活动{
意图一;
按钮btn4;
CharSequence[]选项;
受保护的ArrayList selectedSubjects=新建ArrayList();
布尔[]选择;
字符串s1;
String sub[]=新字符串[5];
文本视图tv2;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
//TODO自动生成的方法存根
super.onCreate(savedInstanceState);
setContentView(R.layout.testselect);
i=getIntent();
s1=i.getStringExtra(“数据1”);
tv2=(TextView)findViewById(R.id.textView2);
btn4=(按钮)findViewById(R.id.button4);
setOnClickListener(新按钮ClickHandler());
Bundle Bundle=getIntent().getExtras();
字符串用户名=bundle.getString(“用户名”);
字符串原因=bundle.getString(“原因”);
String sem=bundle.getString(“sem”);
stringfrom=bundle.getString(“from”);
String to=bundle.getString(“to”);
AsyncTask t1=new LeaveTask(TestSelect.this).execute(用户名、sem、原因、发件人、收件人);
试一试{
字符串结果=t1.get();
Toast.makeText(getBaseContext(),result,Toast.LENGTH_LONG.show();
}捕获(例外e)
{
}
}
公共类按钮ClickHandler实现View.OnClickListener{
公共void onClick(视图){
如果(s1等于(“III”))
{
选项=新字符序列[]{“DSPD”、“CAO”、“BDP”};
选择=新布尔值[options.length];
}
如果(s1等于(“IV”))
{
options=newcharsequence[]{“TOFCS”、“OOP”、“OS”};
选择=新布尔值[options.length];
}
else如果(s1等于(“V”))
{
选项=新字符序列[]{“MI”、“CG”、“SP”};
选择=新布尔值[options.length];
}
其他的
{
选项=新字符序列[]{“DAA”、“DBMS”、“ICWS”};
选择=新布尔值[options.length];
}
显示对话框(0);
/*对选中的项目执行任何操作*/
}  
}
@凌驾
受保护的对话框onCreateDialog(int id)
{
回来
新建AlertDialog.Builder(此)
.setTitle(“主题”)
.setMultiChoiceItems(选项、选项、新对话框SelectionClickHandler())
.setPositiveButton(“确定”,新对话框按钮ClickHandler())
.create();
}
公共类DialogSelectionClickHandler实现DialogInterface.OnMultiChoiceClickListener
{
public void onClick(DialogInterface对话框,int单击,布尔选择)
{
Log.i(“我”,选项[点击]+“选中:”+选中);
如果(选定)
{
选择的主题。添加(选项[单击]);
}
其他的
{
选择的主题。删除(选项[单击]);
}
}
}
公共类DialogButtonClickHandler实现DialogInterface.OnClickListener
{
私有最终字符串NULL=NULL;
boolean[]checkedSubjects=新的boolean[options.length];
int count=options.length;
public void onClick(DialogInterface对话框,int单击,boolean检查)
{
开关(单击)
{
case DialogInterface.BUTTON_阳性:
打印选定的主题();
打破
}
}
private void printedselectedsubjects(){
//TODO自动生成的方法存根
对于(int i=0;i

我正在传递s1的值,这是某个其他活动的学期,每个学期都有一组科目。我需要选择多个科目,并将所有值提取为字符串,但我无法这样做。请帮助我建议您查看此代码示例:

这个示例实际上与您正在尝试的非常相似


您不需要自己跟踪检查的项目。您可以只使用传递给对话框的布尔数组。“选择”在您的情况下。

反馈会很好