Answer the question
In order to leave comments, you need to log in
Why is there a duplicate item in the listview?
There is an adapter CardsAdapter:
public class CardsAdapter extends ArrayAdapter<CardView>{
private String server_ip;
public ArrayList<CardView> data;
Context mContext;
public static final int TEXT_BLOCK = 0;
public static final int TEXT_BLOCK_EXTENDED = 1;
public static final int TEXT_BLOCK_EXTENDED_URL = 2;
public static final int IMAGE_BLOCK = 3;
public static final int IMAGE_BLOCK_URL = 4;
public static final int VIDEO = 5;
public CardsAdapter(Context context, ArrayList<CardView> objects, String _server_ip) {
super(context, 0, objects);
this.server_ip = _server_ip;
this.data = objects;
mContext = context;
}
public int getItemViewType(int position) {
return getItem(position).getType();
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
int viewType = getItemViewType(position);
CardView current = getItem(position);
if( convertView == null){
switch (viewType){
case TEXT_BLOCK:
convertView = getConvertView(R.layout.card_text_block, parent);
((TextView)convertView.findViewById(R.id.card_text_block_text)).setText(current.getString("text"));
((TextView)convertView.findViewById(R.id.card_text_block_header)).setText(current.getString("header"));
break;
case TEXT_BLOCK_EXTENDED:
convertView = getConvertView(R.layout.card_text_block_extended, parent);
((TextView)convertView.findViewById(R.id.card_text_block_extended_text)).setText(current.getString("text"));
((TextView)convertView.findViewById(R.id.card_text_block_extended_header)).setText(current.getString("header"));
String thumbnail_url = String.format("http://%s:9998/%s", server_ip, current.getString("thumbnail"));
new DownloadImageTask((ImageView)convertView.findViewById(R.id.card_text_block_extended_thumbnail)).execute(thumbnail_url);
Log.d("SUPER_TAG", thumbnail_url);
break;
}
}
return convertView;
}
private View getConvertView(int layout_id, ViewGroup parent){
return LayoutInflater.from(mContext).inflate(layout_id, null);
}
}
Answer the question
In order to leave comments, you need to log in
You have a condition: if( convertView == null), because the elements on the screen are only filled when they are created for the first time. The ListView reuses rows instead of recreating each time, i.e. if there are 50 lines in the list, and 5 lines fit on the screen, then 7 lines will be created and when leaving the screen they will be used for other elements. In this case getView will be called and convertView will not be null but will be filled with the old data.
Therefore, data must be written to the view every time getView is called.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question