prof_jack
prof_jack

Reputation: 2073

how to iterate through each checkboxes in each row of a listview in android?

I have an application with three textviews and one checkbox in each row of a listview.what I want that on a click of a button I will be able to get the state of each checkbox and the row corresponding to (isChecked) checkboxes get deleted.one more thing my checkboxes are hardcoded in an xml file.I have searched a lot but couldn't find anything specific.thanks in advance.HERE IS MY CODE...

  public class recentcalllistultimate extends ListActivity implements OnClickListener {
            CheckBox cb;
            Button edit,done;

            ImageButton contacts;
            ListView lv;
            ListView lvultimate;
            listviewadapterultimate lvar;


            int[] uniqueid;
            String[] names;
            String[] types;
            ;
        RelativeLayout rl;
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                // TODO Auto-generated method stub
                super.onCreate(savedInstanceState);

        LayoutParams params=newRelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
                LinearLayout mainLayout = new LinearLayout(this);
                mainLayout.setOrientation(LinearLayout.VERTICAL);
                LayoutInflater layoutInflater = getLayoutInflater();
                mainLayout.addView(layoutInflater.inflate(R.layout.listviewonly, null));
                mainLayout.addView(layoutInflater.inflate(R.layout.allbuttons, null));
                this.addContentView(mainLayout, params);

                cb = (CheckBox) findViewById(R.id.checkboxdelete);

                getContacts();

                lv = (ListView) findViewById(android.R.id.list);
                lvar = new listviewadapterultimate(this, names, types,uniqueid);
                lv.setAdapter(lvar);

                contacts = (ImageButton) findViewById(R.id.button_keypad);
                contacts.setOnClickListener(this);

                edit = (Button) findViewById(R.id.editbutton);
                done=(Button)findViewById(R.id.donebutton);

                done.setOnClickListener(new View.OnClickListener() {



    ------>>>                          public void onClick(View v) {
                        // TODO Auto-generated method stub
          cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {

                public void onCheckedChanged(CompoundButton buttonView, booleanisChecked) {
                                // TODO Auto-generated method stub

                               //WHAT TO DO HERE....        

                                }

                            }
                        });
                    }
     ------>>>          });


    edit.setOnClickListener(new View.OnClickListener() {

                    public void onClick(View v) {
                        // TODO Auto-generated method stub

                        AddDialog ada=new AddDialog(recentcalllistultimate.this);
                        ada.show();
                    }
                });

        }// on create

            public void getContacts() {

                ContentResolver cr = getContentResolver();

                Cursor cur = cr.query(android.provider.CallLog.Calls.CONTENT_URI, null,
                        null, null, null);

                if (cur.getCount() > 0) {
                    int i = 0;
                    int foo = 0;
                    names = new String[cur.getCount()];
                    types = new String[cur.getCount()];
                    duration = new long[cur.getCount()];
                    uniqueid = new int[cur.getCount()];

                    int n = cur.getColumnIndex(CallLog.Calls._ID);
                    int k = cur.getColumnIndex(CallLog.Calls.CACHED_NAME);
                    int y = cur.getColumnIndex(CallLog.Calls.NUMBER);
                    int z = cur.getColumnIndex(CallLog.Calls.CACHED_NUMBER_TYPE);


                    while (cur.moveToNext()) {

                        uniqueid[foo] = cur.getInt(n);

                        String str = cur.getString(k);

                        if (str == null) {
                            names[foo] = cur.getString(y);
                        }// if
                        else {
                            names[foo] = str;
                        }

                        int temp = cur.getInt(z);
                        switch (temp) {
                        case 0:
                            types[foo] = "unknown";
                            break;
                        case 1:
                            types[foo] = "home";
                            break;
                        case 2:
                            types[foo] = "mobile";
                            break;
                        case 3:
                            types[foo] = "work";
                            break;
                        }// switch

                        long doo = cur.getInt(d);
                        duration[foo] = doo;

                        foo++;

                    } // while
                }// if

            }//getcontacts

            public void onClick(View v) {
                // TODO Auto-generated method stub
                if(v==contacts){
                Intent intent = new Intent();
                intent.setClassName("com.a.Activities",
                        "com.a.Activities.DialPad");
                startActivity(intent);
                finish();
                }
                }

     }// class
        .................................

        public class listviewadapterultimate extends BaseAdapter {
            viewHolder holder;

            Activity context;
            String[] names;
            String[] types;
            String[] duration;
            int[] uniqueid;


            public listviewadapterultimate(Activity context, String[] names,
                    String[] types, int[] uniqueid2 ) {
                this.context = context;
                this.names = names;
                this.types = types;

                uniqueid=uniqueid2;

            }




            public int getCount() {
                // TODO Auto-generated method stub
                return names.length;
            }

            public Object getItem(int position) {
                // TODO Auto-generated method stub
                return null;
            }

            public long getItemId(int position) {
                // TODO Auto-generated method stub
                return 0;
            }

            public class viewHolder {
                TextView top;
                TextView bottom;

                TextView down;


            }

            public View getView(int position, View convertView, ViewGroup parent) {
                // TODO Auto-generated method stub



                if (convertView == null) {
                    holder = new viewHolder();
                    LayoutInflater inflator = context.getLayoutInflater();
                    convertView = inflator.inflate(R.layout.recenttextviewonlyultimate, null);
                   holder.top = (TextView) convertView.findViewById(R.id.toptext_u);
                    holder.bottom = (TextView) convertView
                            .findViewById(R.id.bottomtext_u);

                holder.down = (TextView) convertView.findViewById(R.id.recentuniqueid_u);

                convertView.setTag(holder);

                } else {
                    holder = (viewHolder) convertView.getTag();
                    //holder.cb.setVisibility(View.VISIBLE);
                }

                holder.top.setText(names[position]);
                holder.bottom.setText(types[position]);

                holder.down.setText("" + uniqueid[position]);


                return convertView;
            }

        }
        ................

Upvotes: 3

Views: 3820

Answers (2)

Vinayak Bevinakatti
Vinayak Bevinakatti

Reputation: 40513

Try this:

Inside your getView(...) method...

 final CheckBox lChk = ((CheckBox) pConvertView.findViewById(R.id.myChkBoxID)); 
 private List<lisInfo> m_lisInfo = new ArrayList<lisInfo>();

 lChk.setOnCheckedChangeListener(new OnCheckedChangeListener() {
     @Override
     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
     // Populate the listInfo with check box status
     m_lisInfo.get(lPosition).setChkBoxStatus((isChecked));
  }
 });


 public class lisInfo{
   private boolean chkBoxStatus;

   public boolean isChkBoxStatus() {
        return chkBoxStatus;
    }

    public void setChkBoxStatus(boolean chkBoxStatus) {
        this.chkBoxStatus = chkBoxStatus;
    }
}

Now iterate the listInfo wherever required to get the check boxes statuses in the list view

Upvotes: 1

Shailendra Singh Rajawat
Shailendra Singh Rajawat

Reputation: 8242

maintain an array of boolean inside adapter . set listener on ckeckbox in getview which will swipe values of array on check/uncheck .

now make this array accesible in activity where on button

click()
{
for(int i=0;i<array.size;i++)
{
if(array[i])
adapter.deelet(item i);

//modify syntax
}
}

Upvotes: 0

Related Questions