listview - Android checkbox list selection issue -
i have list , using adapter i'm adding items it. when select first item of check box automatically first item after scroll gets selected , items of subsequent scrolls. issue how solve.
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context="com.example.messagecleaner.mainactivity" > <linearlayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" > <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="select addresses don't want see" /> <listview android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/idaddresslist" > </listview> </linearlayout> </relativelayout> enter code here
package com.example.messagecleaner;
import java.util.arraylist; import java.util.collection; import java.util.iterator; import java.util.list; import java.util.listiterator; import android.support.v7.app.actionbaractivity; import android.telephony.smsmanager; import android.text.androidcharacter; import android.content.context; import android.content.intentfilter; import android.database.cursor; import android.net.uri; import android.os.bundle; import android.view.layoutinflater; import android.view.menu; import android.view.menuitem; import android.view.view; import android.view.viewgroup; import android.widget.arrayadapter; import android.widget.checkbox; import android.widget.checkedtextview; import android.widget.compoundbutton; import android.widget.compoundbutton.oncheckedchangelistener; import android.widget.listview; public class mainactivity extends actionbaractivity { listview lstaddress; context mcontext; list<string> arraddress; class myaddressadapter extends arrayadapter<string> { list<string> address; public myaddressadapter(context context, int resource, list<string> objects) { super(context, resource, objects); address=objects; system.out.println("---->"+address.size()); // todo auto-generated constructor stub } @override public view getview(int position, view convertview, viewgroup parent) { // todo auto-generated method stub // return super.getview(position, convertview, parent); view row=convertview; if(row==null) { layoutinflater inflator= getlayoutinflater(); row = inflator.inflate(r.layout.addresscheckboxlistitem, parent, false); checkbox ctv=(checkbox) row.findviewbyid(r.id.idaddresschecktextview); ctv.setoncheckedchangelistener(new oncheckedchangelistener() { @override public void oncheckedchanged(compoundbutton buttonview, boolean ischecked) { // todo auto-generated method stub system.out.println("position "+ ischecked); } }); //ctv.settext(arraddress[position]); system.out.println(address.get(position)); ctv.settext(address.get(position)); return row; } else { checkbox ctv=(checkbox) row.findviewbyid(r.id.idaddresschecktextview); ctv.setoncheckedchangelistener(new oncheckedchangelistener() { @override public void oncheckedchanged(compoundbutton buttonview, boolean ischecked) { // todo auto-generated method stub } }); system.out.println(address.get(position)); ctv.settext(address.get(position)); return row; } } } @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); lstaddress=(listview) findviewbyid(r.id.idaddresslist); mcontext=this; arraddress= new arraylist<string>(); cursor cursor = getcontentresolver().query(uri.parse("content://sms/inbox"), null, null, null, null); //arraddress=new string[cursor.getcount()]; int i=0; if (cursor.movetofirst()) { // must check result prevent exception { string msgdata = ""; for(int idx=0;idx<cursor.getcolumncount();idx++) { if(cursor.getcolumnname(idx).equals("address")) { string msgaddress=cursor.getstring(idx); //msgdata += " " + cursor.getcolumnname(idx) + ":" + cursor.getstring(idx); // system.out.println(msgaddress); //arraddress[i]=msgaddress; if(arraddress.contains(msgaddress)) { } else { arraddress.add(msgaddress); } } } // use msgdata i++; } while (cursor.movetonext()); system.out.println("address array "+arraddress.size()); lstaddress.setadapter(new myaddressadapter(mcontext, android.r.layout.simple_list_item_1, arraddress)); } else { // empty box, no sms } } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.main, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); if (id == r.id.action_settings) { return true; } return super.onoptionsitemselected(item); } }
its practice have model , process data in adapter
your model in adapter
class model { string name; string selected; }
your adapter getview
public view getview(int position, view convertview, viewgroup parent) { model model = getitem(position); viewholder holder = null; if (convertview == null) { layoutinflater inflater = this.mactivity.getlayoutinflater(); convertview = inflater.inflate(r.layout.row, null); holder = new viewholder(); holder.title = (textview) convertview.findviewbyid(r.id.title); holder.checkbox = (checkbox) convertview.findviewbyid(r.id.chbx); } else { holder = (viewholder) convertview.gettag(); } holder.title.settext(model.getname()); holder.checkbox.setchecked(model.isselected); holder.checkbox.setoncheckedchangelistener(this); holder.checkbox.settag(position) return convertview; } change selected value in onchangelistener of checkbox , make notifydatasetchange. @override public void oncheckedchanged(compoundbutton checkbox, boolean ischecked) { // todo auto-generated method stub int position = (integer)checkbox.gettag(); model model = getitem(position); if (ischecked) { model.isselected = true; } else { model.isselected = false; } mmodels.set(position,model); notifydatasetchanged(); }
Comments
Post a Comment