我是一名新的Android开发人员,当我点击一个复选框时,多行检查多个复选框,我使用自定义适配器,请任何人帮助我吗?
我的联系人列表正在关注..
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);
}
}
我的自定义适配器代码如下
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;
}
}
答案 0 :(得分:1)
您需要在checkBox
方法中设置getView
值,例如:
holder.checked.setChecked(dta.getSelect());
你需要设置listener或checkChangeListener来更新Select
值
所以你的getView方法必须像:
@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
和onClick方法:
@Override
public void onClick(View v) {
int position = (Integer) v.getTag();
data.get(position).setselect(
!data.get(position).getSelect());
notifyDataSetChanged();
}