public class GuestsListViewAdapter extends BaseAdapter { Typeface avenirnextbold; Typeface avenirnextregular; LinearLayout guests_cancel_linear; ImageView guests_cancel_imageView; String text0; String text1; String text2; String text3; String text4; Context context; LayoutInflater inflater; private List<guestList> worldpopulationlist2 = null; private ArrayList<guestList> arraylist; public GuestsListViewAdapter(Context context,List<guestList> worldpopulationlist) { this.context = context; this.worldpopulationlist2 = worldpopulationlist; inflater = LayoutInflater.from(context); this.arraylist = new ArrayList<guestList>(); this.arraylist.addAll(worldpopulationlist); } public class ViewHolder { TextView rank; TextView country; TextView population; ImageView flag; } @Override public int getCount() { return worldpopulationlist2.size(); } @Override public Object getItem(int position) { return worldpopulationlist2.get(position); } @Override public long getItemId(int position) { return position; } public View getView(final int position, View view, ViewGroup parent) { final ViewHolder holder; if (view == null) { holder = new ViewHolder(); view = inflater.inflate(R.layout.guests_listview_item, null); // Locate the TextViews in listview_item.xml holder.rank = (TextView) view.findViewById(R.id.name); holder.country = (TextView) view.findViewById(R.id.surname); holder.population = (TextView) view.findViewById(R.id.count_guests); guests_cancel_linear = (LinearLayout)view.findViewById(R.id.guests_cancel_linear); guests_cancel_imageView = (ImageView)view.findViewById(R.id.guests_cancel_imageView); } else { holder = (ViewHolder) view.getTag(); } try { holder.rank.setText(worldpopulationlist2.get(position).getName() + " "); holder.country.setText(worldpopulationlist2.get(position).getSurname()+ " "); if(worldpopulationlist2.get(position).getName().equals("app") || worldpopulationlist2.get(position).getName().equals("app")) { view.setVisibility(View.GONE); view.setMinimumHeight(0); ViewGroup.LayoutParams params = view.getLayoutParams(); params.height = 0; } double i = worldpopulationlist2.get(position).getGuests(); int b = (int) i; if(b == 1) { holder.population.setText(text1 + String.valueOf(worldpopulationlist2.get(position) .getGuests())+ text2); int px = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 0, view.getResources().getDisplayMetrics()); guests_cancel_linear.setMinimumHeight(px); guests_cancel_linear.setMinimumWidth(px); guests_cancel_imageView.setMaxHeight(px); guests_cancel_imageView.setMaxWidth(px); } else if(b == 2 || b ==3 || b ==4) { holder.population.setText(text1 + String.valueOf(worldpopulationlist2.get(position) .getGuests())+ text3); int px = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 0, view.getResources().getDisplayMetrics()); guests_cancel_linear.setMinimumHeight(px); guests_cancel_linear.setMinimumWidth(px); guests_cancel_imageView.setMaxHeight(px); guests_cancel_imageView.setMaxWidth(px); } else if(b == 0) { holder.population.setText(""); int px = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 0, view.getResources().getDisplayMetrics()); guests_cancel_linear.setMinimumHeight(px); guests_cancel_linear.setMinimumWidth(px); guests_cancel_imageView.setMaxHeight(px); guests_cancel_imageView.setMaxWidth(px); } else if(b == -1) { guests_cancel_imageView.setBackgroundResource(R.drawable.guest_cancel); } else { holder.population.setText(text0 + text1 + String.valueOf(worldpopulationlist2.get(position) .getGuests())+ text4); int px = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 0, view.getResources().getDisplayMetrics()); guests_cancel_linear.setMinimumHeight(px); guests_cancel_linear.setMinimumWidth(px); guests_cancel_imageView.setMaxHeight(px); guests_cancel_imageView.setMaxWidth(px); } } } catch (Exception exp){} return view; } 

}

Why does item repeat in listView?

    1 answer 1

    Found the answer here .

    It is necessary to add these 2 methods to the Adapter:

     @Override public int getViewTypeCount() { return getCount(); } @Override public int getItemViewType(int position) { return position; }