【发布时间】:2017-09-25 21:23:13
【问题描述】:
我的listview 的每一行都有一个checkbox。
listview 中的每一行还显示一个电话号码,位于checkbox 的左侧。
我希望 checkbox 仅在我的 listview 的某些行中可见,其中电话号码 (phoneNumberofContact) 也是匹配联系人的行(电话号码是 MatchingContactsAsArrayList 数组中的一个列表。
但是我所有的复选框都是不可见的。你能告诉我如何正确吗?
这是我的自定义适配器中的getView():
@Override
public View getView(int i, View convertView, ViewGroup viewGroup) {
System.out.println("getView number is :" + i + "convertView is : " + convertView);
//we're naming our convertView as view
// View view = convertView;
ViewHolder viewHolder = null;
if (convertView == null) {
//if there is nothing there (if it's null) inflate the view with the layout
LayoutInflater li = (LayoutInflater) _c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = li.inflate(R.layout.phone_inflate_listview, null);
viewHolder = new ViewHolder();
viewHolder.phone = (TextView) convertView.findViewById(R.id.no);
viewHolder.check = (CheckBox) convertView.findViewById(R.id.checkBoxContact);
// viewHolder.check.setVisibility(View.GONE);
//remember the state of the checkbox
viewHolder.check.setOnCheckedChangeListener((NewContact) _c);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
// store the holder with the view
final SelectPhoneContact data = (SelectPhoneContact) theContactsList.get(i);
//in the listview for contacts, set the number
viewHolder.phone.setText(data.getPhone());
////*********************
//for every value in the allPhonesofContacts array list, call it phoneNumberofContact
for (int number = 0; number < allPhonesofContacts.size(); number++) {
phoneNumberofContact = allPhonesofContacts.get(number);
System.out.println("SelectPhoneContactAdapter: phoneNumberofContact : " + phoneNumberofContact);
//if a phone number is in our array of matching contacts
if (MatchingContactsAsArrayList.contains(phoneNumberofContact))
{
viewHolder.check.setVisibility(View.VISIBLE);
}
else {
viewHolder.check.setVisibility(View.INVISIBLE);
}
}
System.out.println("SelectPhoneContactAdapter: allPhonesofContacts : " + allPhonesofContacts.size());
viewHolder.check.setChecked(data.isSelected());
viewHolder.check.setTag(data);
// Return the completed view to render on screen
return convertView;
}
这是我的模型:
public class SelectPhoneContact {
String phone;
public String getPhone() {return phone;}
public void setPhone(String phone) {
this.phone = phone;
}
//*****************************************
//this is for the checkbox
boolean selected = false;
public boolean isSelected() {
return selected;
}
public void setSelected(boolean selected){
this.selected=selected;
}
}
【问题讨论】:
-
我的蜘蛛感觉在这条线上刺痛
MatchingContactsAsArrayList.contains(phoneNumberofContact) -
我在另一个活动中有该行,它按预期工作。
-
我不知道您在“for”循环中要做什么。
allPhonesofContacts和data之间没有联系。但是,查看代码,只有当allPhonesofContacts的最后一个条目在MatchingContactsAsArrayList中时才能看到检查。它一直循环到最后一个没有break的条目,所以最后一个条目决定检查是可见还是不可见。 -
@Emma 在 for 循环中,我试图查看该人手机上的哪些联系人也在使用我的应用程序,然后在这些联系人旁边放置一个复选框。老实说,我发现整个 getView() 事情很混乱。但是,根据您的评论,我已将代码更改为
for (int number = 0; number < MatchingContactsAsArrayList.size(); number++) {...if (MatchingContactsAsArrayList.contains(data.getPhone())),并且我更接近我希望实现的目标。
标签: android listview custom-adapter