I am getting a ClassCastException when using two different ViewHolders
for my Groups an in ExpandableListView.
I have a separate ViewHolder for Group in position 0. The rest of the Groups use a different one.
This error happens after I already create the views by scrolling down and scroll back up to the Group in position 0.
I know my issue is in this line:
vhPropertyInfo = (ViewHolderPropertyInfo) convertView.getTag();
When I scroll back to Group position 0, it tries to cast (ViewHolderPropertyInfo) to the convertView that is not null, but it is actually (ViewHolderGroup) type. How do I resolve this?
#Override
public View getGroupView(int groupPosition, boolean isExpandable, View convertView, ViewGroup parent) {
if (groupPosition == 0) {
ViewHolderPropertyInfo vhPropertyInfo;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.row_property_info, null);
vhPropertyInfo = new ViewHolderPropertyInfo();
vhPropertyInfo.mTvName = (TextView) convertView.findViewById(R.id.tv_name);
vhPropertyInfo.mTvCountry = (TextView) convertView.findViewById(R.id.tv_country);
vhPropertyInfo.mTvCity = (TextView) convertView.findViewById(R.id.tv_city);
vhPropertyInfo.mTvDistrict = (TextView) convertView.findViewById(R.id.tv_district);
vhPropertyInfo.mTvPostalCode = (TextView) convertView.findViewById(R.id.tv_postal_code);
vhPropertyInfo.mTvStreet = (TextView) convertView.findViewById(R.id.tv_street);
vhPropertyInfo.mTvSubStreet = (TextView) convertView.findViewById(R.id.tv_sub_street);
convertView.setTag(vhPropertyInfo);
}
else {
vhPropertyInfo = (ViewHolderPropertyInfo) convertView.getTag();
}
vhPropertyInfo.mTvName.setText("House Lannister");
vhPropertyInfo.mTvCountry.setText("Westeros");
vhPropertyInfo.mTvCity.setText("Kings Landing");
vhPropertyInfo.mTvDistrict.setText("West Side");
vhPropertyInfo.mTvPostalCode.setText("12345");
vhPropertyInfo.mTvStreet.setText("123 Kings Lane");
vhPropertyInfo.mTvSubStreet.setText("Hut 23");
return convertView;
}
else {
ViewHolderGroup vhGroup;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.row_property_group, null);
vhGroup = new ViewHolderGroup();
vhGroup.mTvName = (TextView) convertView.findViewById(R.id.tv_name);
convertView.setTag(vhGroup);
}
else {
vhGroup = (ViewHolderGroup) convertView.getTag();
}
// Group 0 is property info so have to subtract one position
vhGroup.mTvName.setText(getGroup(groupPosition));
return convertView;
}
}
public final class ViewHolderGroup {
public TextView mTvName;
}
public final class ViewHolderPropertyInfo {
public TextView mTvName;
public TextView mTvCountry;
public TextView mTvCity;
public TextView mTvDistrict;
public TextView mTvPostalCode;
public TextView mTvStreet;
public TextView mTvSubStreet;
}
EDIT 1: So I fixed the problem, but checking the instance of convertView.getTag() if it was the same type of ViewHolder type, but my solution doesn't seem very elegant. Anyone know a way to do this cleaner?
#Override
public View getGroupView(int groupPosition, boolean isExpandable, View convertView, ViewGroup parent) {
if (groupPosition == 0) {
ViewHolderPropertyInfo vhPropertyInfo;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.row_property_info, null);
vhPropertyInfo = new ViewHolderPropertyInfo();
vhPropertyInfo.mTvName = (TextView) convertView.findViewById(R.id.tv_name);
vhPropertyInfo.mTvCountry = (TextView) convertView.findViewById(R.id.tv_country);
vhPropertyInfo.mTvCity = (TextView) convertView.findViewById(R.id.tv_city);
vhPropertyInfo.mTvDistrict = (TextView) convertView.findViewById(R.id.tv_district);
vhPropertyInfo.mTvPostalCode = (TextView) convertView.findViewById(R.id.tv_postal_code);
vhPropertyInfo.mTvStreet = (TextView) convertView.findViewById(R.id.tv_street);
vhPropertyInfo.mTvSubStreet = (TextView) convertView.findViewById(R.id.tv_sub_street);
convertView.setTag(vhPropertyInfo);
}
else {
if (convertView.getTag() instanceof ViewHolderPropertyInfo) {
vhPropertyInfo = (ViewHolderPropertyInfo) convertView.getTag();
}
else {
convertView = mInflater.inflate(R.layout.row_property_info, null);
vhPropertyInfo = new ViewHolderPropertyInfo();
vhPropertyInfo.mTvName = (TextView) convertView.findViewById(R.id.tv_name);
vhPropertyInfo.mTvCountry = (TextView) convertView.findViewById(R.id.tv_country);
vhPropertyInfo.mTvCity = (TextView) convertView.findViewById(R.id.tv_city);
vhPropertyInfo.mTvDistrict = (TextView) convertView.findViewById(R.id.tv_district);
vhPropertyInfo.mTvPostalCode = (TextView) convertView.findViewById(R.id.tv_postal_code);
vhPropertyInfo.mTvStreet = (TextView) convertView.findViewById(R.id.tv_street);
vhPropertyInfo.mTvSubStreet = (TextView) convertView.findViewById(R.id.tv_sub_street);
convertView.setTag(vhPropertyInfo);
}
}
vhPropertyInfo.mTvName.setText("House Lannister");
vhPropertyInfo.mTvCountry.setText("Westeros");
vhPropertyInfo.mTvCity.setText("Kings Landing");
vhPropertyInfo.mTvDistrict.setText("West Side");
vhPropertyInfo.mTvPostalCode.setText("12345");
vhPropertyInfo.mTvStreet.setText("123 Kings Lane");
vhPropertyInfo.mTvSubStreet.setText("Hut 23");
return convertView;
}
else {
ViewHolderGroup vhGroup;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.row_property_group, null);
vhGroup = new ViewHolderGroup();
vhGroup.mTvName = (TextView) convertView.findViewById(R.id.tv_name);
convertView.setTag(vhGroup);
}
else {
if (convertView.getTag() instanceof ViewHolderGroup) {
vhGroup = (ViewHolderGroup) convertView.getTag();
}
else {
convertView = mInflater.inflate(R.layout.row_property_group, null);
vhGroup = new ViewHolderGroup();
vhGroup.mTvName = (TextView) convertView.findViewById(R.id.tv_name);
convertView.setTag(vhGroup);
}
}
// Group 0 is property info so have to subtract one position
vhGroup.mTvName.setText(getGroup(groupPosition));
return convertView;
}
}
I found a more elegant way to do my solution. It is to do the convertView.getTag() checking the instanceof the ViewHolder before the check if convertView == null.
#Override
public View getGroupView(int groupPosition, boolean isExpandable, View convertView, ViewGroup parent) {
if (groupPosition == 0) {
ViewHolderPropertyInfo vhPropertyInfo;
if (convertView != null && !(convertView.getTag() instanceof ViewHolderPropertyInfo)) {
convertView = null;
}
if (convertView == null) {
convertView = mInflater.inflate(R.layout.row_property_info, null);
vhPropertyInfo = new ViewHolderPropertyInfo();
vhPropertyInfo.mTvName = (TextView) convertView.findViewById(R.id.tv_name);
vhPropertyInfo.mTvCountry = (TextView) convertView.findViewById(R.id.tv_country);
vhPropertyInfo.mTvCity = (TextView) convertView.findViewById(R.id.tv_city);
vhPropertyInfo.mTvDistrict = (TextView) convertView.findViewById(R.id.tv_district);
vhPropertyInfo.mTvPostalCode = (TextView) convertView.findViewById(R.id.tv_postal_code);
vhPropertyInfo.mTvStreet = (TextView) convertView.findViewById(R.id.tv_street);
vhPropertyInfo.mTvSubStreet = (TextView) convertView.findViewById(R.id.tv_sub_street);
convertView.setTag(vhPropertyInfo);
}
else {
vhPropertyInfo = (ViewHolderPropertyInfo) convertView.getTag();
}
vhPropertyInfo.mTvName.setText("House Lannister");
vhPropertyInfo.mTvCountry.setText("Westeros");
vhPropertyInfo.mTvCity.setText("Kings Landing");
vhPropertyInfo.mTvDistrict.setText("West Side");
vhPropertyInfo.mTvPostalCode.setText("12345");
vhPropertyInfo.mTvStreet.setText("123 Kings Lane");
vhPropertyInfo.mTvSubStreet.setText("Hut 23");
return convertView;
}
else {
ViewHolderGroup vhGroup;
if (convertView != null && !(convertView.getTag() instanceof ViewHolderGroup)) {
convertView = null;
}
if (convertView == null) {
convertView = mInflater.inflate(R.layout.row_property_group, null);
vhGroup = new ViewHolderGroup();
vhGroup.mTvName = (TextView) convertView.findViewById(R.id.tv_name);
convertView.setTag(vhGroup);
}
else {
vhGroup = (ViewHolderGroup) convertView.getTag();
}
// Group 0 is property info so have to subtract one position
vhGroup.mTvName.setText(getGroup(groupPosition));
return convertView;
}
}
Related
Whenever I scroll down, it shows me duplicate values.I have used setTag() method to avoid duplicate values but still duplicate values are there. Please provide help. I have added my Adapter class as follows :
CustomAdapter.java
#Override
public int getCount() {
return searchVedBeanList.size();
}
#Override
public Object getItem(int position) {
if (searchVedBeanList != null && searchVedBeanList.size() != 0 && searchVedBeanList.size() > position) {
return searchVedBeanList.get(position);
} else {
return null;
}
}
#Override
public long getItemId(int position) {
return position;
}
#SuppressWarnings("deprecation")
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
final VedBean vedBean = (VedBean) getItem(position);
if (convertView == null) {
LayoutInflater li = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
holder = new ViewHolder();
convertView = li.inflate(R.layout.search_ved_list_item, parent, false);
holder.mantraSansTextView = (TextView) convertView.findViewById(R.id.mantraSansTextView);
holder.vedTextView = (TextView) convertView.findViewById(R.id.vedTextView);
holder.commentratorTextView = (TextView) convertView.findViewById(R.id.commentratorTextView);
holder.rishiDevtaLinearLayout = (LinearLayout) convertView.findViewById(R.id.rishiDevtaLinearLayout);
holder.rishiTextView = (TextView) convertView.findViewById(R.id.rishiTextView);
holder.devtaTextView = (TextView) convertView.findViewById(R.id.devtaTextView);
// initialize Atharvaved components
holder.atharvavedVedLinearLayoutSearch = (LinearLayout) convertView.findViewById(R.id.atharvavedVedLinearLayoutSearch);
holder.kandTextViewAtharvaSearch = (TextView) convertView.findViewById(R.id.kandTextViewAtharvaSearch);
holder.suktaTextViewAtharvaSearch = (TextView) convertView.findViewById(R.id.suktaTextViewAtharvaSearch);
holder.mantraNumTextViewAtharvaSearch = (TextView) convertView.findViewById(R.id.mantraNumTextViewAtharvaSearch);
// initialize Yajurved components
holder.yajurVedLinearLayoutSearch = (LinearLayout) convertView.findViewById(R.id.yajurVedLinearLayoutSearch);
holder.adhyayTextViewYajurSearch = (TextView) convertView.findViewById(R.id.adhyayTextViewYajurSearch);
holder.mantraNumTextViewYajurSearch = (TextView) convertView.findViewById(R.id.mantraNumTextViewYajurSearch);
// initialize Samved components
holder.samVedLinearLayoutSearch = (LinearLayout) convertView.findViewById(R.id.samVedLinearLayoutSearch);
holder.archikTextViewSamSearch = (TextView) convertView.findViewById(R.id.archikTextViewSamSearch);
holder.adhyayTextViewSamSearch = (TextView) convertView.findViewById(R.id.adhyayTextViewSamSearch);
holder.dashatiTextViewSamSearch = (TextView) convertView.findViewById(R.id.dashatiTextViewSamSearch);
holder.mantraNumTextViewSamSearch = (TextView) convertView.findViewById(R.id.mantraNumTextViewSamSearch);
// initialize Rigved components
holder.rigVedLinearLayoutSearch = (LinearLayout) convertView.findViewById(R.id.rigVedLinearLayoutSearch);
holder.mandalTextViewRigSearch = (TextView) convertView.findViewById(R.id.mandalTextViewRigSearch);
holder.suktaTextViewRigSearch = (TextView) convertView.findViewById(R.id.suktaTextViewRigSearch);
holder.mantraNumTextViewRigSearch = (TextView) convertView.findViewById(R.id.mantraNumTextViewRigSearch);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
char charArr[] = vedBean.getMantraSans().toCharArray();
for (int i = 0; i < charArr.length; i++) {
holder.mantraSansTextView.setText(holder.mantraSansTextView.getText().toString() + charArr[i]);
}
String vedStr = "<b>"+UtilityConstant.STR_VED+" : "+"</b>"+UtilityConstant.getVedValueFromId(vedBean.getVedId());
holder.vedTextView.setText(Html.fromHtml(vedStr));
String commentatorStr = "<b>"+UtilityConstant.STR_COMMENTATOR+" : "+"</b>"+UtilityConstant.getCommentratorValueFromId(vedBean.getCommentratorId());
holder.commentratorTextView.setText(Html.fromHtml(commentatorStr));
holder.rishiTextView.setVisibility(View.GONE);
return convertView;
}
I have a ListView that is correctly populated with an array on create, but when I scroll, the data changes and is all out of order. I don't know wether the problem is in my CustomListViewAdapter or in my DetailsPage. Here's the For Loop that I am using to generate the textViews:
if (currentObject.setTime != null && currentObject.setName != null) {
String[] temporaryNames = currentObject.setName;
int totalNames = (currentObject.setTime.length / currentObject.setName.length);
for (int i = 1; i < totalNames; i++) {
currentObject.setName = ArrayUtils.addAll(currentObject.setName,temporaryNames);
}
}
listView.setAdapter(new BusRouteCustomListViewAdapter(this, currentObject.setName, currentObject.setTime));
and here is the code for my customListViewAdapter:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
Holder holder = new Holder();
View v = convertView;
if (v == null)
if (locations[position] != null && times[position] != null) {
v = inflater.inflate(R.layout.busdetailrow, null);
holder.tv1 = (TextView) v.findViewById(R.id.text);
holder.tv2 = (TextView) v.findViewById(R.id.text2);
holder.tv1.setText(locations[position]);
holder.tv2.setText(times[position]);
} else {
v = inflater.inflate(R.layout.null_row, null);
}
return v;
}
You replace the code with these lines of code
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final Holder holder;
View v = convertView;
if (v == null)
{
holder = new Holder();
v = inflater.inflate(R.layout.busdetailrow, null);
holder.tv1 = (TextView) v.findViewById(R.id.text);
holder.tv2 = (TextView) v.findViewById(R.id.text2);
v.setTag(holder);
} else {
holder = (Holder) v.getTag();
}
holder.tv1.setText(locations[position]);
holder.tv2.setText(times[position]);
return v;
}
Use your getView() like below.
#Override
public View getView(int position, View convertView, ViewGroup parent) {
Holder holder = new Holder();
if (convertView == null){
if (locations[position] != null && times[position] != null) {
convertView = inflater.inflate(R.layout.busdetailrow, null);
holder.tv1 = (TextView) v.findViewById(R.id.text);
holder.tv2 = (TextView) v.findViewById(R.id.text2);
holder.tv1.setText(locations[position]);
holder.tv2.setText(times[position]);
} else {
}
convertView.setTag(holder);
}else{
holder = (Holder) convertView.getTag();
}
return convertView;
}
You didnt implement design holder probably. make following changes to your getView then it will work fine
#Override
public View getView(int position, View convertView, ViewGroup parent) {
Holder holder = new Holder();
if (convertView == null){
if (locations[position] != null && times[position] != null) {
convertView = inflater.inflate(R.layout.busdetailrow, null);
holder.tv1 = (TextView) v.findViewById(R.id.text);
holder.tv2 = (TextView) v.findViewById(R.id.text2);
holder.tv1.setText(locations[position]);
holder.tv2.setText(times[position]);
}
else
{
}
convertView.setTag(holder);
}else{
holder = (Holder) convertView.getTag();
}
return convertView;
}
I have created an adapter that feeds a list view. It is working ok but is a little jenky so I am trying to use the Convert View to avoid inflating a view on every getView call. I changed the getview method to the following
#Override
public View getView(int position, View convertView, ViewGroup parent) {
Suggestion suggestion = getItem(position);
int type = getItemViewType(position);
ImageView imageView;
TextView titleTv;
ImageView checkBox;
switch (type) {
case Suggestion.CONTACT_SUGGESTION_ID:
ContactSuggestion contactSuggestion = (ContactSuggestion) suggestion;
String id = contactSuggestion.getId();
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.contact_history_item, parent, false);
}
imageView = (ImageView) convertView.findViewById(R.id.imageView);
PhotoRetreiver photoRetreiver = (PhotoRetreiver) imageView.getTag();
if (photoRetreiver != null) {
photoRetreiver.cancel(true);
}
if (images.containsKey(id)) {
imageView.setImageBitmap(images.get(id));
} else {
photoRetreiver = new PhotoRetreiver(context, id, imageView);
imageView.setTag(photoRetreiver);
photoRetreiver.execute();
}
titleTv = (TextView) convertView.findViewById(R.id.titleTv);
titleTv.setText(suggestion.getTitle());
checkBox = (ImageView) convertView.findViewById(R.id.favouriteIv);
checkBox.setOnClickListener(this);
checkBox.setId(position);
if (suggestion.isFavourite()) {
checkBox.setBackgroundResource(R.drawable.search_fav_active);
checkBox.setTag(true);
} else {
checkBox.setTag(false);
}
break;
case Suggestion.GOOGLE_SUGGESTION_ID:
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.google_history_item, parent, false);
}
imageView = (ImageView) convertView.findViewById(R.id.imageView);
titleTv = (TextView) convertView.findViewById(R.id.titleTv);
imageView.setImageResource(resources.getIdentifier(suggestion.getInActiveImageResource(), "drawable", "com.allryder.android"));
titleTv.setText(suggestion.getTitle());
checkBox = (ImageView) convertView.findViewById(R.id.favouriteIv);
checkBox.setOnClickListener(this);
checkBox.setId(position);
if (suggestion.isFavourite()) {
checkBox.setBackgroundResource(R.drawable.search_fav_active);
checkBox.setTag(true);
} else {
checkBox.setTag(false);
}
break;
case Suggestion.STATION_SUGGESTION_ID:
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.station_history_item, parent, false);
}
StationSuggestion stationSuggestion = (StationSuggestion) suggestion;
StationView stationView = (StationView) convertView.findViewById(R.id.stationView);
stationView.setStationTypes(stationSuggestion.getStationTypes());
titleTv = (TextView) convertView.findViewById(R.id.titleTv);
titleTv.setText(suggestion.getTitle());
checkBox = (ImageView) convertView.findViewById(R.id.favouriteIv);
checkBox.setOnClickListener(this);
checkBox.setId(position);
if (suggestion.isFavourite()) {
checkBox.setBackgroundResource(R.drawable.search_fav_active);
checkBox.setTag(true);
} else {
checkBox.setTag(false);
}
break;
case Suggestion.CURRENT_LOCATION_SUGGESTION_ID:
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.current_location_item, parent, false);
}
titleTv = (TextView) convertView.findViewById(R.id.titleTv);
titleTv.setText(suggestion.getTitle());
break;
}
TextView detailTv = (TextView) convertView.findViewById(R.id.detailTv);
detailTv.setText(suggestion.getDescription());
convertView.setTag(type);
return convertView;
}
Then overwrote the following to methods so the adapter knows which view type is should pass into the getview method
#Override
public int getViewTypeCount() {
return 4;
}
#Override
public int getItemViewType(int position) {
return getItem(position).getTypeId();
}
However I receive a a null pointer on this line
checkBox.setOnClickListener(this);
So checkbox is not instantiated.
You can see that I added a tag to the convert view this was just to check that the tag would equal the type when convert view is not null. And it always is.
Why the hell can the adapter not find checkbox?
I have Listview with multiple views.Tried to set the selection on second item using following code.But the view is always null.Please help me.Due to character limit I can't attach the layouts used for the listview
View view = ListView.getChildAt(0);
int top = (view == null) ? 0 : view.getTop();
// restore list
ListView.setSelectionFromTop(ListView.getFirstVisiblePosition() + 1, top);
Adapter
#Override
public int getItemViewType(int position) {
if (Details.get(position).getView_type() == 1)
return ITEM_TYPE_ONE;
else if (Details.get(position).getView_type() == 0)
return ITEM_TYPE_THREE;
else
return ITEM_TYPE_TWO;
}
#Override
public int getViewTypeCount() {
return 3;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
vi = convertView;
int type = getItemViewType(position);
if (convertView == null) {
holder = new ViewHolder();
switch (type) {
case ITEM_TYPE_ONE:
vi = inflater.inflate(R.layout.feed_detail_list_item_selected,
null);
holder.name = (TextView) vi
.findViewById(R.id.list_item_name_txt);
holder.profilepic = (ImageView) vi
.findViewById(R.id.list_item_profile_img);
break;
case ITEM_TYPE_TWO:
vi = inflater.inflate(R.layout.listlastrow, null);
holder.name = (TextView) vi.findViewById(R.id.msg);
break;
case ITEM_TYPE_THREE:
vi = inflater
.inflate(R.layout.feed_details_parent_layout, null);
holder.name = (TextView) vi.findViewById(R.id.ownerName);
holder.profilepic = (ImageView) vi
.findViewById(R.id.thumb_image);
holder.VoiceBar = (SeekBar) vi
.findViewById(R.id.message_seekbar);
holder.voice_bar_layout = (LinearLayout) vi
.findViewById(R.id.seekbar_layout);
break;
}
vi.setTag(holder);
} else {
holder = (ViewHolder) vi.getTag();
}
if (type == ITEM_TYPE_TWO) {
holder.name.setTypeface(tfNormal);
((LinearLayout) vi.findViewById(R.id.ll))
.setBackgroundColor(mContext.getResources().getColor(
R.color.white));
if (Details.size() == 2) {
holder.name.setVisibility(View.VISIBLE);
holder.name.setText(mContext.getResources().getString(
R.string.no_replies));
} else {
holder.name.setVisibility(View.GONE);
}
}
}
return vi;
}
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
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);
}
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{
}
}