我有4組聯繫人(Type1,2,3,無)。我想加載不同的圖像3 Type1,2,3,如果接觸屬於無則列表視圖不應包含任何圖片。這是我的代碼動態加載圖像到列表視圖
@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相應的圖像爲這種類型加載正確,但當我滾動列表相同的圖像將加載到一些未分配也,有沒有問題與我的代碼請告訴我
由於其做工精細.. – Pradeep