无法从char序列中提取主题的值

时间:2014-04-26 11:48:34

标签: 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 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

        }
    }


}

我从一些其他活动中传递了s1的学期价值,每个学期都有一套科目,我需要选择多个科目,并将所有值提取为字符串,但我无法这样做。请帮助

1 个答案:

答案 0 :(得分:0)

我建议您查看此代码示例:here

样本实际上与您尝试的内容非常相似......

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