Reputation: 2528
I'm having 4 groups of contacts(Type1,2,3,None). i want to load different images 3 for Type1,2,3, if contact belong to None then listview shouldn't contain any image.. This is my code
@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;
}
}
Problem with this is if i assign some contacts to Type 1 corresponding images for this type loaded correctly but when i scroll the list same image will be loaded to some unassigned also, is there any problem with my code please tell me
Upvotes: 0
Views: 401
Reputation: 39836
do the class Holder that the other question suggested. And do the Holder for every single adapter you ever create.
To answer your question, try this edit:
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);
}
Upvotes: 1
Reputation: 989
Try to make a holder class for your views. Then in the if(v==null) block, you can use setTag() and use getTag() in the else block. Here's some code.
public static class ViewHolder {
TextView contactNameText;
TextView contactNumberText;
ImageView contactImage;
}
This is a holder class that contains your views. The first part of the getView() method should then look like this:
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{
}
}
Upvotes: 1