Android: Keeping track of checked values in multi select dialog

隐身守侯 提交于 2019-12-13 00:58:02

问题


I have a button, on click of which i display a multi-select dialog. I load the dialog with values from the database. i wanna track the values in the dialog that are checked. How do i do that?? And is it possible to keep some services checked while initially loading the dialog. My code for ButtonOnClickHandler goes like this:

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();       
}  

Thank you


回答1:


Make an ArrayList, and while selecting option just add to the list and while unselecting remove it from the list. thats it

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]);
            }

} 


来源:https://stackoverflow.com/questions/10068848/android-keeping-track-of-checked-values-in-multi-select-dialog

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!