// 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的学期价值,每个学期都有一套科目,我需要选择多个科目,并将所有值提取为字符串,但我无法这样做。请帮助
答案 0 :(得分:0)