我有一个按钮,点击该按钮我会显示一个多选对话框。我使用数据库中的值加载对话框。我想跟踪对话框中检查的值。我怎么做??在最初加载对话框时是否可以检查一些服务。 我的ButtonOnClickHandler代码如下:
class ButtonClickHandler implements View.OnClickListener {
public void onClick( View view ) {
int i=0;
List<Service> svc = EditBusinessdh.getServiceList();
Log.v(TAG, "Setting svc size:"+ svc.size() );
serName = new ArrayList<String>();
for(i=0;i<svc.size();i++)
{
serName.add(svc.get(i).toString());
Log.v(TAG, "service ="+svc.get(i));
}
showDialog(0);
}
}
@Override
protected Dialog onCreateDialog(int id)
{
String[] ser = serName.toString().substring(1,serName.toString().length()-1).split(",");
return new AlertDialog.Builder( this )
.setTitle( "Select Services" )
.setMultiChoiceItems(ser, selected, new DialogSelectionClickHandler())
.setPositiveButton( "Ok", new DialogButtonClickHandler() )
.create();
}
public class DialogSelectionClickHandler implements DialogInterface.OnMultiChoiceClickListener
{
public void onClick( DialogInterface dialog, int clicked, boolean checked )
{
String firstSelected= serName.set(clicked, ser.toString());
}
}
public class DialogButtonClickHandler implements DialogInterface.OnClickListener
{
public void onClick( DialogInterface dialog, int clicked )
{
switch( clicked )
{
case DialogInterface.BUTTON_POSITIVE:
closeContextMenu();
break;
case DialogInterface.BUTTON_NEGATIVE:
break;
}
}
}
@Override
public void closeContextMenu() {
super.closeContextMenu();
}
谢谢
答案 0 :(得分:2)
创建一个ArrayList,在选择选项时只需添加到列表中,同时取消选择将其从列表中删除。多数民众赞成
ArrayList<String> checkedValue= new ArrayList<String>();
public void onClick( DialogInterface dialog, int clicked, boolean checked )
{
if (isChecked)
{
if(!checkedValue.contains(ser[which]))
checkedValue.add(ser[which]);
}
else
{
if(checkedValue.contains(ser[which]))
checkedValue.remove(ser[which]);
}
}