mercredi 2 mars 2016

all items in listview assign to checked in java

I have list view in android with multiple check option, when i checked some items, it execute the action for all items,

@Override
        public void onClick(View arg0) {
           int cntChoice = lv.getCount();
           String checkedd = "";

           String unchecked = "";
            SparseBooleanArray checked = lv.getCheckedItemPositions();
            ArrayList<String> selectedItems = new ArrayList<String>();
            for (int i = 0; i < cntChoice; i++) {


                    if(checked.get(i) == true)
                    {
                        checkedd = lv.getItemAtPosition(i).toString() ;
                        Pushbots.sharedInstance().tag(checkedd);
                    }
                    else  if(checked.get(i) == false)
                    {
                        unchecked= lv.getItemAtPosition(i).toString() ;

                    }

even some items i did not checked, but it assign it as checked and execute this code Pushbots.sharedInstance().tag(checkedd);

:

this my all code :_

String list[]={"decision","data","math","ic3"};
private String my_sel_items;
ArrayAdapter<String> ard;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    sharedInstance().init(this);
    my_sel_items = new String();

    btn = (Button) findViewById(R.id.button);

    lv = (ListView) findViewById(R.id.exp_list);
    ard = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, list);
    lv.setAdapter(ard);

    lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
  btn.setOnClickListener(new View.OnClickListener() {


       @Override
        public void onClick(View arg0) {
           int cntChoice = lv.getCount();
           String checkedd = "";

           String unchecked = "";
            SparseBooleanArray checked = lv.getCheckedItemPositions();
            ArrayList<String> selectedItems = new ArrayList<String>();
            for (int i = 0; i < cntChoice; i++) {


                    if(checked.get(i) == true)
                    {
                        checkedd = lv.getItemAtPosition(i).toString() ;
                        Pushbots.sharedInstance().tag(checkedd);
                    }
                    else  if(checked.get(i) == false)
                    {
                        unchecked= lv.getItemAtPosition(i).toString() ;

                    }}}

Aucun commentaire:

Enregistrer un commentaire