ListView items repeated issue

310 Views Asked by At

I have a multicolumn_listview. Some rows repeat in listview. i looked at all solution about this problem.

listview items image

  1. Listview height not wrap_content
  2. My holder class static
  3. 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;
    }
1

There are 1 best solutions below

15
On BEST ANSWER

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.