Getting duplicate values in ListView android while scrolling - android

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;
}

Related

Why Listview showing duplicate entries with custom adapter android?

I created custom adapter to bind with listview but it's showing duplicate songs in listview. please check out the code and let me know if anything is wrong
public class UnfavoriteSongAdapter extends BaseAdapter
{
private Activity activity;
private ArrayList<Media> data;
private static LayoutInflater inflater = null;
private Context context;
public UnfavoriteSongAdapter(Context context, Activity a, ArrayList<Media> d) {
activity = a;
this.context = context;
data = d;
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return data.size();
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
GenreFavoriteClickListner genreFavoriteClickListner;
public interface GenreFavoriteClickListner {
public void onFavoriteClickListner(int position,Media media);
}
public void setGenreFavoriteClickListner(GenreFavoriteClickListner genreFavoriteClickListner) {
this.genreFavoriteClickListner = genreFavoriteClickListner;
}
public static class ViewHolder {
public TextView textView_title, textView_artist, textView_time;
public CircleImageView circleImageView_albumphoto, circleImageView_favorite;
public ImageView imageView_play;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View vi = convertView;
final ViewHolder viewHolder;
try {
if (convertView == null) {
viewHolder = new ViewHolder();
final LayoutInflater inflater1 = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
vi = inflater1.inflate(R.layout.song_listitem_layout, null);
viewHolder.textView_title = (TextView) vi.findViewById(R.id.textView_song_songname);
viewHolder.textView_artist = (TextView) vi.findViewById(R.id.textView_song_artistname);
viewHolder.textView_time = (TextView) vi.findViewById(R.id.textView_song_time);
viewHolder.circleImageView_albumphoto = (CircleImageView) vi.findViewById(R.id.circleImageView_nowplaying_playing);
viewHolder.circleImageView_favorite = (CircleImageView) vi.findViewById(R.id.circleImageView_song_favorite);
viewHolder.imageView_play = (ImageView) vi.findViewById(R.id.imageView_song_playpause);
} else {
viewHolder = (ViewHolder) vi.getTag();
}
if (data.size() <= 0) {
//viewHolder.textview_albumtitle.setText("No Appointment");
} else {
final Media p = data.get(position);
viewHolder.textView_title.setText(p.getSongName());
viewHolder.textView_artist.setText(p.getArtistName());
long millis = Long.parseLong(p.getDuration());
String hms = ReusableModules.getCalculatedTime(millis);
viewHolder.textView_time.setText(hms);
String isplaying = p.getIsPlaying();
int playingid = R.mipmap.play;
if (isplaying != null) {
if(isplaying.contentEquals(Constants.ONE)){
playingid = R.mipmap.runing_music_play;
}
}
viewHolder.imageView_play.setImageResource(playingid);
ReusableModules.setFavoriteToggleButton(p.getIsFavorite(), viewHolder.circleImageView_favorite);
viewHolder.circleImageView_favorite.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (genreFavoriteClickListner != null) {
genreFavoriteClickListner.onFavoriteClickListner(position,p);
}
}
});
final String finalUrl = p.getAlbumUrl();
viewHolder.circleImageView_albumphoto.post(new Runnable() {
#Override
public void run() {
BaseActivity.imageLoader.displayImage(finalUrl,
viewHolder.circleImageView_albumphoto, SplashScreenActivity.displayImageOptions, new SimpleImageLoadingListener() {
#Override
public void onLoadingComplete(Bitmap loadedImage) {
}
});
}
});
vi.setTag(p);
}
} catch (Exception e) {
}
return vi;
}
}
Change in getView() method like this.
viewHolder = new ViewHolder();
if (convertView == null) {
final LayoutInflater inflater1 = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
vi = inflater1.inflate(R.layout.song_listitem_layout, null);
viewHolder.textView_title = (TextView) vi.findViewById(R.id.textView_song_songname);
viewHolder.textView_artist = (TextView) vi.findViewById(R.id.textView_song_artistname);
viewHolder.textView_time = (TextView) vi.findViewById(R.id.textView_song_time);
viewHolder.circleImageView_albumphoto = (CircleImageView) vi.findViewById(R.id.circleImageView_nowplaying_playing);
viewHolder.circleImageView_favorite = (CircleImageView) vi.findViewById(R.id.circleImageView_song_favorite);
viewHolder.imageView_play = (ImageView) vi.findViewById(R.id.imageView_song_playpause);
and remove this
else {
viewHolder = (ViewHolder) vi.getTag();
}
in last of getView() method
convertView.setTag(holder);
try this and tell me it works or not?
move
vi.setTag(viewholder);
inside
if(convert==null){
viewHolder = new ViewHolder();
final LayoutInflater inflater1 = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
vi = inflater1.inflate(R.layout.song_listitem_layout, null);
viewHolder.textView_title = (TextView) vi.findViewById(R.id.textView_song_songname);
viewHolder.textView_artist = (TextView) vi.findViewById(R.id.textView_song_artistname);
viewHolder.textView_time = (TextView) vi.findViewById(R.id.textView_song_time);
viewHolder.circleImageView_albumphoto = (CircleImageView) vi.findViewById(R.id.circleImageView_nowplaying_playing);
viewHolder.circleImageView_favorite = (CircleImageView) vi.findViewById(R.id.circleImageView_song_favorite);
viewHolder.imageView_play = (ImageView) vi.findViewById(R.id.imageView_song_playpause);
// here
vi.setTag(viewholder);
}
and remove this:
final String finalUrl = p.getAlbumUrl();
viewHolder.circleImageView_albumphoto.post(new Runnable() {
#Override
public void run() {
BaseActivity.imageLoader.displayImage(finalUrl,
viewHolder.circleImageView_albumphoto, SplashScreenActivity.displayImageOptions, new SimpleImageLoadingListener() {
#Override
public void onLoadingComplete(Bitmap loadedImage) {
}
});
}
});
remove this **vi.setTag(p);**
Add vi.setTag(p) inside :
if(convertView == null){
vi.setTag(p);
}
set tag for view holder if view holder null. Add last line of code
if (convertView == null) {
viewHolder = new ViewHolder();
final LayoutInflater inflater1 = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
vi = inflater1.inflate(R.layout.song_listitem_layout, null);
viewHolder.textView_title = (TextView) vi.findViewById(R.id.textView_song_songname);
viewHolder.textView_artist = (TextView) vi.findViewById(R.id.textView_song_artistname);
viewHolder.textView_time = (TextView) vi.findViewById(R.id.textView_song_time);
viewHolder.circleImageView_albumphoto = (CircleImageView) vi.findViewById(R.id.circleImageView_nowplaying_playing);
viewHolder.circleImageView_favorite = (CircleImageView) vi.findViewById(R.id.circleImageView_song_favorite);
viewHolder.imageView_play = (ImageView) vi.findViewById(R.id.imageView_song_playpause);
/************ Set holder with LayoutInflater ************/
vi.setTag( viewHolder );
}
//check you data size getCount,its a good practice. not in getview method.
#Override
public int getCount() {
if(data!=null && data.size()> 0)
return data.size();
else
return 0;
}
I suggest you to replace getview method and check you data once because your adapter code seems perfect.

Android ListView Multiple Row Give Views values in getView method

In getView method i try to set textview text.I have two different type of object. If i use "position" in parameter it gives "IndexOutOfBoundsException" error normally. How can i set text each of them appropriately?
Context context;
ArrayList<OfficialKanal> officialKanals;
ArrayList<NormalKanal> normalKanals;
ArrayList<Object> kanallar = new ArrayList();
int OFFICIAL_KANAL = 0;
int NORMAL_KANAL= 1;
LayoutInflater lala;
public KanalAdapter(Context context , ArrayList<OfficialKanal> officiallar , ArrayList<NormalKanal> normaller){
this.context = context;
officialKanals = officiallar;
normalKanals = normaller;
for(int i = 0 ; i < officiallar.size() ; i++){
kanallar.add(officiallar.get(i));
}
for(int i = 0 ; i < normaller.size() ; i++){
kanallar.add(normaller.get(i));
}
Log.i("tago" , "tagtag");
lala = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return kanallar.size();
}
public Object getItem(int i) {
return kanallar.get(i);
}
public long getItemId(int i) {
return i;
}
public int getItemViewType(int position) {
Object item = getItem(position);
if(item instanceof NormalKanal){
return NORMAL_KANAL;
}else if (item instanceof OfficialKanal){
return OFFICIAL_KANAL;
}
return -1;
}
#Override
public int getViewTypeCount() {
return 2;
}
public View getView(int position, View convertView, ViewGroup viewGroup) {
KanalHolder holder = null;
int pozisyon = 0;
Object currentKanal = getItem(position);
if(convertView==null) {
holder = new KanalHolder();
if (currentKanal instanceof OfficialKanal) {
convertView = lala.inflate(R.layout.officialkanal, null);
holder.image2 = (ImageView) convertView.findViewById(R.id.imageView5);
holder.tv3 = (TextView) convertView.findViewById(R.id.textView4);
holder.tv4 = (TextView) convertView.findViewById(R.id.textView8);
holder.buton2 = (Button) convertView.findViewById(R.id.button7);
holder.buton3 = (Button) convertView.findViewById(R.id.button8);
Log.i("tago", "tagtagatagtagtagatg");
}
if (currentKanal instanceof NormalKanal) {
convertView = lala.inflate(R.layout.normalkanal, null);
holder.image1 = (ImageView) convertView.findViewById(R.id.imageView5);
holder.tv1 = (TextView) convertView.findViewById(R.id.textView4);
holder.tv2 = (TextView) convertView.findViewById(R.id.textView8);
holder.buton1 = (Button) convertView.findViewById(R.id.button8);
Log.i("tago", "tagtagtag");
}
convertView.setTag(holder);
}else{
holder = (KanalHolder)convertView.getTag();
}
if(currentKanal instanceof OfficialKanal){
holder.tv3.setText(officialKanals.get(position).getKanaladi());
}
if(currentKanal instanceof NormalKanal){
holder.tv1.setText(normalKanals.get(position).getKanaladi());
}
return convertView;
}
static class KanalHolder{
public ImageView image1,image2;
public TextView tv1 , tv2,tv3,tv4;
public Button buton1,buton2,buton3;
}
issue is getCount getView will call according to size of kanallar
public int getCount() {
return kanallar.size();
}
but you getting value from officialKanals and normalKanals in getView
try like for experiment
if(currentKanal instanceof OfficialKanal){
holder.tv3.setText(kanallar.get(position).getKanaladi());
}
if(currentKanal instanceof NormalKanal){
holder.tv1.setText(kanallar.get(position).getKanaladi());
}

ExpandableListView ClassCastException when using two different ViewHolders for Group

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;
}
}

How can I pass data from container activity to custom adapter

I am in the following situation:
I have the default implementation of the navigation drawer from Android Studio with a navigationDrawerFragment.
I have a custom listView for this navigationDrawerFragment with a custom adapter.
Since I have different type of item in my listView, i use the classic holder pattern.
My problem is the following: I'd like to send data from my container activity (facebook profile picture and name) to the custom adapter to set the holder with these data.
how can I do that ? See the following code:
if (view == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
drawerHolder = new DrawerItemHolder();
view = inflater.inflate(layoutResID, parent, false);
drawerHolder.ItemName = (TextView) view
.findViewById(R.id.drawer_profileName);
drawerHolder.icon = (ProfilePictureView) view.findViewById(R.id.drawer_profileIcon);
drawerHolder.title = (TextView) view.findViewById(R.id.drawer_titleName);
drawerHolder.item = (TextView) view.findViewById(R.id.drawer_itemName);
drawerHolder.logOut = (TextView) view.findViewById(R.id.drawer_logOut);
drawerHolder.logOut.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
onLogoutButtonClicked();
}
});
drawerHolder.titleLayout = (LinearLayout) view.findViewById(R.id.titleLayout);
drawerHolder.itemLayout = (LinearLayout) view.findViewById(R.id.itemLayout);
drawerHolder.profileLayout = (LinearLayout) view.findViewById(R.id.profileLayout);
view.setTag(drawerHolder);
} else {
drawerHolder = (DrawerItemHolder) view.getTag();
}
DrawerItem dItem = (DrawerItem) this.drawerItemList.get(position);
if (dItem.isTitle()){
drawerHolder.titleLayout.setVisibility(LinearLayout.VISIBLE);
drawerHolder.itemLayout.setVisibility(LinearLayout.INVISIBLE);
drawerHolder.profileLayout.setVisibility(LinearLayout.INVISIBLE);
drawerHolder.title.setText(dItem.getTitle());
} else if (dItem.getImgResID() == 0) {
drawerHolder.titleLayout.setVisibility(LinearLayout.INVISIBLE);
drawerHolder.itemLayout.setVisibility(LinearLayout.VISIBLE);
drawerHolder.profileLayout.setVisibility(LinearLayout.INVISIBLE);
drawerHolder.item.setText(dItem.getItem());
} else {
drawerHolder.titleLayout.setVisibility(LinearLayout.INVISIBLE);
drawerHolder.itemLayout.setVisibility(LinearLayout.INVISIBLE);
drawerHolder.profileLayout.setVisibility(LinearLayout.VISIBLE);
}
return view;
}
I need in the else to set the data I passed to the custom adapter to the facebookProfilPicture.
Pass the data in the constructor form your activity to the adapter.
public class YourAdapter {
private final String facebookProfilePictureUrl;
private final String facebookProfileName;
public YourAdapter(String facebookProfilePictureUrl, String facebookProfileName) {
this.facebookProfilePictureUrl = facebookProfilePictureUrl;
this.facebookProfileName = facebookProfileName;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (view == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
drawerHolder = new DrawerItemHolder();
view = inflater.inflate(layoutResID, parent, false);
drawerHolder.ItemName = (TextView) view
.findViewById(R.id.drawer_profileName);
drawerHolder.icon = (ProfilePictureView) view.findViewById(R.id.drawer_profileIcon);
drawerHolder.title = (TextView) view.findViewById(R.id.drawer_titleName);
drawerHolder.item = (TextView) view.findViewById(R.id.drawer_itemName);
drawerHolder.logOut = (TextView) view.findViewById(R.id.drawer_logOut);
drawerHolder.logOut.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
onLogoutButtonClicked();
}
});
drawerHolder.titleLayout = (LinearLayout) view.findViewById(R.id.titleLayout);
drawerHolder.itemLayout = (LinearLayout) view.findViewById(R.id.itemLayout);
drawerHolder.profileLayout = (LinearLayout) view.findViewById(R.id.profileLayout);
view.setTag(drawerHolder);
} else {
drawerHolder = (DrawerItemHolder) view.getTag();
}
DrawerItem dItem = (DrawerItem) this.drawerItemList.get(position);
if (dItem.isTitle()){
drawerHolder.titleLayout.setVisibility(LinearLayout.VISIBLE);
drawerHolder.itemLayout.setVisibility(LinearLayout.INVISIBLE);
drawerHolder.profileLayout.setVisibility(LinearLayout.INVISIBLE);
drawerHolder.title.setText(dItem.getTitle());
} else if (dItem.getImgResID() == 0) {
drawerHolder.titleLayout.setVisibility(LinearLayout.INVISIBLE);
drawerHolder.itemLayout.setVisibility(LinearLayout.VISIBLE);
drawerHolder.profileLayout.setVisibility(LinearLayout.INVISIBLE);
drawerHolder.item.setText(dItem.getItem());
} else {
drawerHolder.titleLayout.setVisibility(LinearLayout.INVISIBLE);
drawerHolder.itemLayout.setVisibility(LinearLayout.INVISIBLE);
drawerHolder.profileLayout.setVisibility(LinearLayout.VISIBLE);
}
drawerHolder.title.setText(facebookProfileName);
// Load the profile image here.
return view;
}
}

ListView items repeated issue

I have a multicolumn_listview. Some rows repeat in listview. i looked at all solution about this problem.
listview items image
Listview height not wrap_content
My holder class static
I created all views in if(convertView==null) block in getview method.
but the problem hasn't solved yet.. please help me..
here is my adapter class;
public class ListViewAdapterCurrentList extends BaseAdapter
{
public ArrayList<HashMap<String, String>> list;
boolean isDetail = false;
private String currentNo;
private String currentCode;
private String currentName;
private String date;
private String status;
private Activity activity;
private Boolean isPlan = true;
public ListViewAdapterCurrentList(Activity activity, ArrayList<HashMap<String, String>> list, boolean isPlan) {
super();
this.activity = activity;
this.list = list;
this.isPlan = isPlan;
}
#Override
public int getCount()
{
return list.size();
}
#Override
public Object getItem(int position)
{
return list.get(position);
}
#Override
public long getItemId(int position)
{
return 0;
}
static class ViewHolder
{
ImageView currentDetail;
TextView currentNo;
TextView currentCode;
TextView currentName;
TextView date;
TextView durum;
LinearLayout linearLayoutCurrentBase;
LinearLayout linearLayoutCurrentDetail;
TextView currentAddress;
TextView currentPhone;
TextView currentManager;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
final ViewHolder holder;
LayoutInflater inflater = activity.getLayoutInflater();
final HashMap<String, String> map = list.get(position);
if (convertView==null)
{
Log.d("CL:getView()", "Fetching Row: " + position);
convertView = inflater.inflate(R.layout.currentlistitems, parent, false);
holder = new ViewHolder();
holder.currentDetail = (ImageView) convertView.findViewById(R.id.imageViewCurrentListDetail);
holder.currentNo = (TextView) convertView.findViewById(R.id.textViewCurrentListNo);
holder.currentCode = (TextView) convertView.findViewById(R.id.textViewCurrentListCode);
holder.currentName = (TextView) convertView.findViewById(R.id.textViewCurrentListName);
holder.date = (TextView) convertView.findViewById(R.id.textViewCurrentListDate);
holder.durum = (TextView) convertView.findViewById(R.id.textViewCurrentListStatus);
holder.linearLayoutCurrentBase = (LinearLayout) convertView.findViewById(R.id.layoutBaseCurrentList);
holder.linearLayoutCurrentDetail = (LinearLayout) convertView.findViewById(R.id.layoutDetailCurrentList);
holder.currentAddress = (TextView) convertView.findViewById(R.id.textViewCurrentAddress);
holder.currentPhone = (TextView) convertView.findViewById(R.id.textViewCurrentPhone);
holder.currentManager = (TextView) convertView.findViewById(R.id.textViewCurrentManager);
convertView.setTag(holder);
} else
{
holder = (ViewHolder) convertView.getTag();
}
currentNo = map.get("CariNo");
currentCode = map.get("CariKod");
currentName = map.get("CariUnvan");
date = map.get("Tarih");
status = map.get("Durum");
holder.currentDetail.setBackgroundResource(R.drawable.box_plus);
holder.currentNo.setText(currentNo);
holder.currentCode.setText(currentCode);
holder.currentName.setText(currentName);
holder.date.setText(date);
holder.durum.setText(status);
if (!isPlan)
{
holder.durum.setVisibility(View.GONE);
holder.date.setVisibility(View.GONE);
holder.currentName.setWidth(300);
}
initDetailInfo(holder.currentAddress, holder.currentPhone, holder.currentManager, currentNo);
holder.currentDetail.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
if (!isDetail)
{
holder.linearLayoutCurrentDetail.setVisibility(View.VISIBLE);
holder.currentDetail.setBackgroundResource(R.drawable.box_delete_expand);
isDetail = true;
} else
{
holder.linearLayoutCurrentDetail.setVisibility(View.GONE);
holder.currentDetail.setBackgroundResource(R.drawable.box_plus);
isDetail = false;
}
}
});
return convertView;
}
Edited since it apparently wasn't clear:
if (convertView==null)
{
Log.d("CL:getView()", "Fetching Row: " + position);
convertView = inflater.inflate(R.layout.currentlistitems, parent, false);
holder = new ViewHolder();
holder.currentDetail = (ImageView) convertView.findViewById(R.id.imageViewCurrentListDetail);
holder.currentNo = (TextView) convertView.findViewById(R.id.textViewCurrentListNo);
holder.currentCode = (TextView) convertView.findViewById(R.id.textViewCurrentListCode);
holder.currentName = (TextView) convertView.findViewById(R.id.textViewCurrentListName);
holder.date = (TextView) convertView.findViewById(R.id.textViewCurrentListDate);
holder.durum = (TextView) convertView.findViewById(R.id.textViewCurrentListStatus);
holder.linearLayoutCurrentBase = (LinearLayout) convertView.findViewById(R.id.layoutBaseCurrentList);
holder.linearLayoutCurrentDetail = (LinearLayout) convertView.findViewById(R.id.layoutDetailCurrentList);
holder.currentAddress = (TextView) convertView.findViewById(R.id.textViewCurrentAddress);
holder.currentPhone = (TextView) convertView.findViewById(R.id.textViewCurrentPhone);
holder.currentManager = (TextView) convertView.findViewById(R.id.textViewCurrentManager);
convertView.setTag(holder);
} else
{
holder = (ViewHolder) convertView.getTag();
}
right under the above before any work is done to them like adding children to the LinearLayouts:
holder.linearLayoutCurrentBase.removeAllViews();
holder.linearLayoutCurrentDetail.removeAllViews();
My guess is since you aren't doing that with the Viewgroups it never replaces the children. Let me know if that helps.
EDIT:
I just edited the answer for hopefully the last time. I do apologize for using the wrong method for removing the children of the ViewGroups. Now it should make sense. Look at my comment to understand why it duplicates your Views.

Categories

Resources