问题
I am a New Android Developer and when I click on one check-box multiple check-boxes are checked in multiple rows, I am using a custom adapter, please can anyone help me?
My Contact List is following ..
public class ContactList extends Activity {
ListView lvContacts;
List<String> listname;
List<String> listnumber;
SimpleCursorAdapter adapter;
ArrayList<PhoneList> arr;
ArrayList<PhoneList> arrnumber;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String[] projection = new String[] {
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER,
};
Cursor c1 = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,projection,null,null, null);
// Let activity manage the cursor
//startManagingCursor(c1);
listname = new ArrayList<String>();
listnumber = new ArrayList<String>();
while (c1.moveToNext())
{
String name=c1.getString(c1.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String number = c1.getString(c1.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
listname.add(name);
listnumber.add(number);
}
c1.close();
// while(c1.moveToNext())
// {
// //Log.d("fghfh", ""+c1.getColumnIndex(Phone.NUMBER));
// String name=c1.getString(c1.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
// String number=c1.getString(c1.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
// //System.out.println(".................."+number);
// Log.d("Contacts : ",name);//+":"+number);
//}
Object[] obj=listname.toArray();
Object[] objnumber=listnumber.toArray();
// arrnumber=new ArrayList<PhoneList>();
arr=new ArrayList<PhoneList>();
String[] da=new String[obj.length];
String[] danumber=new String[objnumber.length];
for(int i=0; i<obj.length; i++)
{
danumber[i]=(String)objnumber[i];
da[i]=(String)obj[i];
Toast.makeText(this, "Humayoon zoom"+obj[i], Toast.LENGTH_SHORT).show();
arr.add(new PhoneList(da[i],danumber[i]));
}
ListView listView = (ListView) findViewById(R.id.lvContacts);
CustomAdapter adpttt=new CustomAdapter(ContactList.this,R.layout.contacts_list_item,arr);
listView.setAdapter(adpttt);
}
}
My Custom Adapter code is following
public class CustomAdapter extends ArrayAdapter<PhoneList> {
int inflatr;
Context ctxt;
ArrayList<PhoneList> data=new ArrayList<PhoneList>();
public CustomAdapter(Context context, int resource, ArrayList<PhoneList> arr) {
super(context, resource, arr);
this.inflatr = resource;
this.ctxt = context;
this.data= arr;
}
@Override
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
UserHolder holder = null;
View row = convertView;
if(convertView==null)
{
LayoutInflater inflater = ((Activity) ctxt).getLayoutInflater();
row = inflater.inflate(inflatr, parent, false);
holder = new UserHolder();
holder.textName=(TextView)row.findViewById(R.id.lblName);
holder.stnumber=(TextView)row.findViewById(R.id.mobilenum);
holder.checked=(CheckBox)row.findViewById(R.id.check);
// holder.btnEdit = (ImageButton) row.findViewById(R.id.atomPay_removePay);
row.setTag(holder);
}
else
{
holder = (UserHolder) row.getTag();
}
// if(bulkflag)
// {
// holder.checked.setVisibility(View.VISIBLE);
// }
// else
// {
// holder.checked.setVisibility(View.GONE);
// }
// holder.checked.setOnClickListener(new OnClickListener() {
//
// public void onClick(View v) {
//
// if(holder.checked.isChecked())
// {
// selectedIds.add(recIdArr.get(reportslistview.getPositionForView(cBox)));
// //System.out.println("position "+reportslistview.getPositionForView(cBox));
// }
// else
// {
// selectedIds.remove(recIdArr.get(reportslistview.getPositionForView(cBox)));
// }
// }
// });
PhoneList dta=data.get(position);
holder.checked.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
int position = (Integer) v.getTag();
data.get(position).setselect(!data.get(position).getSelect());
notifyDataSetChanged();
}
});
holder.checked.setChecked(dta.getSelect());
//holder.checked.setTag(position);
Toast.makeText(ctxt, "Humayoon zoooommmm"+dta.getNumber(), Toast.LENGTH_SHORT).show();
for(int i=1; i<=256; i++)
{
holder.textName.setText(dta.getName());
}
for(int j=1; j<=256; j++)
{
holder.stnumber.setText(dta.getNumber());
}
// holder.btnEdit.setOnClickListener(new OnClickListener() {
//
// @Override
// public void onClick(View v) {
// Toast.makeText(ctxt, "Humayoon Siddiqueeeeeeeeeeeeeeeeeee"+dta.getName(), Toast.LENGTH_SHORT).show();
// Intent moreIntent=new Intent(getContext(),ContactList.class);
// String tName=dta.getName();
// moreIntent.putExtra("Template",tName);
// v.getContext().startActivity(moreIntent);
// // ctxt.startActivity(ctxt,ContactList.class);
// }
// });
return row;
}
@Override
public int getCount() {
// // TODO Auto-generated method stub
return data.size();
}
static class UserHolder {
TextView textName;
TextView textAddress;
TextView textLocation;
ImageButton btnEdit;
Button btnDelete;
TextView stnumber;
CheckBox checked;
}
}
My phone List class is following...
package com.example.sqliteapplication;
public class PhoneList {
String name;
String number;
boolean isSelected;
public PhoneList(String string,String number) {
super();
this.name = string;
this.number=number;
}
public void setselect(Boolean b)
{
this.isSelected=b;
}
public boolean getSelect()
{
return isSelected;
}
public String getName() {
return name;
}
public void setNumber(String number)
{
this.number=number;
}
public String getNumber()
{
return number;
}
public void setName(String name) {
this.name = name;
}
}
回答1:
you need set checkBox
value in getView
method like:
holder.checked.setChecked(dta.getSelect());
and you need set listener or checkChangeListener for that, for updating Select
value
so your getView method must be like:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
UserHolder holder = null;
View row = convertView;
if(convertView==null)
{
// yourCode
}
else
{
// your code
}
holder.checked.setOnClickListener(this);
holder.checked.setChecked(dta.getSelect());
holder.checked.setTag(position); // you need this for getting position in onClick method
// other code
and onClick method:
@Override
public void onClick(View v) {
int position = (Integer) v.getTag();
data.get(position).setselect(
!data.get(position).getSelect());
notifyDataSetChanged();
}
来源:https://stackoverflow.com/questions/23606086/multiple-checkboxes-checked-when-one-check-a-one-check-box