我正在使用联系人列表的复选框创建列表视图,我有一个按钮来选择联系人列表视图的所有复选框。当我设置for(int i = 0; i< 5; i ++)时,它将选择6个复选框并正常工作..但是当设置lv.getcount();它显示错误....我认为它显示只有getview设置值.....因为我也在适配器中使用getview ....我怎么能解决这个问题请建议我.... ??
public class Contacts extends Activity implements CompoundButton.OnCheckedChangeListener{
String name, phoneNo;
List<ContactItem> contectItem;
ArrayList<String> valuesList = new ArrayList<String>();
ListView lv;
CompoundButton b1;
String[] sender = null;
boolean flag = true;
int i = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contacts);
lv = (ListView) findViewById(R.id.contactList);
lv.setTranscriptMode(ListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);
method();
}
public void method() {
// ProgressDialog pd = new ProgressDialog(Contacts.this);
// pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);
//pd.setTitle("Please wait");
// pd.setMessage("Loading Contacts...");
// pd.show();
contectItem = new ArrayList<ContactItem>();
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
null, ContactsContract.Contacts.HAS_PHONE_NUMBER + "= 1", null, "UPPER(" + ContactsContract.Contacts.DISPLAY_NAME + ")ASC");
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
Cursor pCur = cr.query
(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ? ",
new String[]{id}, null);
while (pCur.moveToNext()) {
int i = 0;
phoneNo = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
//Toast.makeText(Contacts.this, "Name: " + name + ", Phone No: " + phoneNo, Toast.LENGTH_LONG).show();
ContactItem item = new ContactItem(phoneNo, name);
contectItem.add(item);
}
pCur.close();
contactAdpter adpter = new contactAdpter(this, R.layout.contact_list, contectItem);
lv.setAdapter(adpter);
}
}
}
// pd.dismiss();
}
public void back (View v) {
super.onBackPressed();
finish();
}
public void select_all (View v) {
if (lv.getCount() > 0) {
for (int i = 0; i <lv.getCount(); i++ ) {
View view = lv.getChildAt(i);
CheckBox chk = (CheckBox)view.findViewById(R.id.checkbox1);
chk.setChecked(true);
}
}
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int pos = lv.getPositionForView(buttonView);
if (pos != ListView.INVALID_POSITION) {
ContactItem p = contectItem.get(pos);
p.setSelected(isChecked);
if (isChecked) {
if (p.isSelected()) {
valuesList.add(p.getNumber());
}
} else {
valuesList.remove(p.getNumber());
}
}
}
答案 0 :(得分:0)
尝试在select_all方法中使用“lv.getAdapter()。getCount()”instade of“lv.getCount()”来获取listview项的计数。
答案 1 :(得分:0)
试试这种方式
获取listview的孩子
for (int i = 0; i < listView.getAdapter().getCount(); i++) {
View view = listView.getChildAt(i);
}