Extended baseadapter using convertview's recycle logic causes list items TextView setText method to be called twice (overlaying text) in listview after scrolling view off screen and then back. Inflating convertview each time getview is called removes this issue. Any help would be appreciated.
adapter = new BaseAdapter() {
@Override
public int getCount() {
return comments.length();
}
@Override
public JSONObject getItem(int i) {
try {
return comments.getJSONObject(i);
} catch (JSONException e) {
logger.e(String.valueOf(e));
return new JSONObject();
}
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public View getView(final int i, View convertView, final ViewGroup parent) {
Holder holder;
if(convertView==null){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.comment_row_layout, parent, false);
holder = new Holder(convertView);
convertView.setTag(holder);
}else{
holder = (Holder) convertView.getTag();
}
try {
JSONObject entry = this.getItem(i);
holder.profileName.setText(entry.getString("title"));
final String facebookUrl = entry.getString("profileLink");
holder.profileName.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
context.sendBroadcast(new Intent("vibrate"));
try {
int versionCode = context.getPackageManager().getPackageInfo("com.facebook.katana", 0).versionCode;
if (versionCode >= 3002850) {
Uri uri = Uri.parse("fb://facewebmodal/f?href=" + facebookUrl);
startActivity(new Intent(Intent.ACTION_VIEW, uri));
} else {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("fb://page/336227679757310")));
}
} catch (PackageManager.NameNotFoundException e) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(facebookUrl)));
}
}
});
Glide.with(comment_dialog_fragment.this).load(entry.getString("pictureLink")).into(holder.profilePic);
final int reply_int = entry.getInt("replies");
if (reply_int != 0) holder.replies.setText(reply_int + " Replies");
switch (entry.getString("type")) {
case "text":
TextView textView = new TextView(context);
textView.setTextColor(Color.WHITE);
textView.setGravity(Gravity.LEFT);
textView.setText(entry.getString("content"));
holder.frame.addView(textView);
break;
case "image":
final String imageLink = SITE_URL + entry.getString("content");
RoundedImageView imageView = new RoundedImageView(context);
imageView.setScaleType(ImageView.ScaleType.FIT_START);
imageView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
holder.frame.addView(imageView);
Glide.with(comment_dialog_fragment.this).load(imageLink).into(imageView);
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
context.sendBroadcast(new Intent("vibrate"));
Intent open = new Intent(Intent.ACTION_VIEW).setDataAndType(Uri.parse(imageLink), "image/*");
if (open.resolveActivity(context.getPackageManager()) != null) {
startActivity(open);
}
}
});
break;
}
} catch (JSONException e) {
logger.e(String.valueOf(e));
}
return convertView;
}
};
private class Holder {
TextView profileName, replies;
RoundedImageView profilePic;
FrameLayout frame;
Holder(View v) {
profileName = (TextView) v.findViewById(R.id.profile_name);
frame = (FrameLayout) v.findViewById(R.id.content);
replies = (TextView) v.findViewById(R.id.replies);
profilePic = (RoundedImageView) v.findViewById(R.id.profile_pic);
}
}