我有4组联系人(Type1,2,3,None)。我想为Type1,2,3加载不同的图像3,如果联系人属于None,那么listview不应包含任何图像..这是我的代码
@Override
public View getView(int position, View convertView,
ViewGroup parent) {
// return super.getView(position, convertView, parent);
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater) ContactsListActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.contacts_list_row_view, null);
}
try {
contactsData = (ContactsItem) getItem(position);
} catch (Exception e) {
}
if (null != contactsData){
final CheckBox contactsSelectedCheck = (CheckBox) v.findViewById(R.id.contact_selected_check);
TextView contactNameText = (TextView) v.findViewById(R.id.contact_name_text);
TextView contactNumberText = (TextView) v.findViewById(R.id.contact_number_text);
ImageView contactImage = (ImageView) v.findViewById(R.id.contact_image);
contactNameText.setText(contactsData.getContactName());
contactNumberText.setText(contactsData.getContactNumber());
if(contactNameText != null && contactNumberText != null){
if(contactsData.getContactProfileType() == DBConstants.TYPE_1){ contactImage.setImageDrawable(getResources().getDrawable(R.drawable.icon_1));
} else if(contactsData.getContactProfileType() == DBConstants.TYPE_2){ contactImage.setImageDrawable(getResources().getDrawable(R.drawable.icon_2));
} else if(contactsData.getContactProfileType() == DBConstants.TYPE_3){ contactImage.setImageDrawable(getResources().getDrawable(R.drawable.icon_3));
}else{
}
}
if (selectedContactsTable.containsKey(contactsData.getContactNumber())) {
contactsSelectedCheck.setChecked(true);
} else {
contactsSelectedCheck.setChecked(false);
}
contactsSelectedCheck.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (contactsSelectedCheck.isChecked()) {
LinearLayout r_layout = (LinearLayout) v.getParent();
TextView contactName = (TextView) r_layout.getChildAt(1);
TextView contactNumber = (TextView) r_layout.getChildAt(2);
selectedContactsTable.put(contactNumber.getText().toString(), contactName.getText().toString());
}else{
LinearLayout r_layout = (LinearLayout) v.getParent();
TextView contactNumber = (TextView) r_layout.getChildAt(2);
selectedContactsTable.remove(contactNumber.getText().toString());
}
}
});
}
return v;
}
}
问题是,如果我将一些联系人分配给类型1对应的图像,这个类型正确加载但是当我滚动列表时,同一图像也会被加载到一些未分配的,我的代码是否有任何问题请告诉我
答案 0 :(得分:1)
尝试为您的观点制作一个持有者类。然后在if(v == null)块中,您可以使用setTag()并在else块中使用getTag()。这是一些代码。
public static class ViewHolder {
TextView contactNameText;
TextView contactNumberText;
ImageView contactImage;
}
这是包含您的观点的持有者类。 getView()方法的第一部分应如下所示:
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater) ContactsListActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.contacts_list_row_view, null);
holder = new ViewHolder();
holder.contactNameText = (TextView) v.findViewById(R.id.contact_name_text);
holder.contactNumberText = (TextView) v.findViewById(R.id.contact_number_text);
holder.contactImage = (ImageView) v.findViewById(R.id.contact_image);
v.setTag(holder);
}
else{
holder = (ViewHolder) v.getTag();
}
try {
contactsData = (ContactsItem) getItem(position);
} catch (Exception e) {
}
if (null != contactsData){
final CheckBox contactsSelectedCheck = (CheckBox) v.findViewById(R.id.contact_selected_check);
holder.contactNameText.setText(contactsData.getContactName());
holder.contactNumberText.setText(contactsData.getContactNumber());
if(contactNameText != null && contactNumberText != null){
if(contactsData.getContactProfileType() == DBConstants.TYPE_1){ holder.contactImage.setImageDrawable(getResources().getDrawable(R.drawable.icon_1));
} else if(contactsData.getContactProfileType() == DBConstants.TYPE_2){ holder.contactImage.setImageDrawable(getResources().getDrawable(R.drawable.icon_2));
} else if(contactsData.getContactProfileType() == DBConstants.TYPE_3){ holder.contactImage.setImageDrawable(getResources().getDrawable(R.drawable.icon_3));
}else{
}
}
答案 1 :(得分:1)
做另一个问题建议的班主持人。并为您创建的每个适配器执行Holder。
要回答您的问题,请尝试以下修改:
contactImage.setVisibility(View.Visible);
if(contactsData.getContactProfileType() == DBConstants.TYPE_1){ contactImage.setImageDrawable(getResources().getDrawable(R.drawable.icon_1));
} else if(contactsData.getContactProfileType() == DBConstants.TYPE_2){ contactImage.setImageDrawable(getResources().getDrawable(R.drawable.icon_2));
} else if(contactsData.getContactProfileType() == DBConstants.TYPE_3){ contactImage.setImageDrawable(getResources().getDrawable(R.drawable.icon_3));
}else{
contactImage.setVisibility(View.GONE);
}