0

I created a custom listview with one Arraylist of String and I haven't added any Duplicate Value in that but when i run it in my phone the elements after number 9 in the list start to repeat .. so how can i avoid this duplicates.
here are my java codes..

ListDetails.java

package com.android.mezohn.unl.model;    
import com.android.mezohn.unl.R;    
import java.util.ArrayList;
import java.util.HashSet;

public class ListDetails {    
    public static ArrayList<model> getList() {
        ArrayList<model> SystemAuditList = new ArrayList<>();
        SystemAuditList.add(new model("S1", "Access Control"));
        SystemAuditList.add(new model("S2", "Contractor Safety"));
        SystemAuditList.add(new model("S3", "PTW Standard"));
        SystemAuditList.add(new model("S4", "Incident Investigation"));
        SystemAuditList.add(new model("S5", "Technology and Subtle change"));
        SystemAuditList.add(new model("S6", "Work at height"));
        SystemAuditList.add(new model("S7", "Confined Spaces"));
        SystemAuditList.add(new model("S8", "Emergency Preparedness and EPI"));
        SystemAuditList.add(new model("S9", "Effluent Treatment and Waste Management "));
        SystemAuditList.add(new model("S10", "Storage and Handling of Hazardous chemicals"));
        SystemAuditList.add(new model("S11", "Portable Electrical Appliances"));
        SystemAuditList.add(new model("S12", "Machine Guarding"));
        SystemAuditList.add(new model("S13", "Lifting Tools"));
        SystemAuditList.add(new model("S14", "Canteen"));
        SystemAuditList.add(new model("S15", "Personal Change Management"));
        SystemAuditList.add(new model("S16", "Hazard Checklist"));
        SystemAuditList.add(new model("S17", "Hot Work"));
        SystemAuditList.add(new model("S18", "Ladder Safety Checklist"));
        SystemAuditList.add(new model("S19", "LOTO Checklist"));
        SystemAuditList.add(new model("S20", "SBO Checklist"));
        SystemAuditList.add(new model("S21", "Vehicle Marshal system Checklist"));
        SystemAuditList.add(new model("S22", "Gate,Drain and Shutter"));
        return SystemAuditList;            
    } 
}

model.java

package com.android.mezohn.unl.model;


public class model {

    private String Auditcode;
    private String SystemAudit;



    public model(String Auditcode, String SystemAudit ) {
        this.Auditcode = Auditcode;
        this.SystemAudit = SystemAudit;

    }

    public String getAuditcode() { return Auditcode; }

    public void setAuditcode(String Auditcode) {
        this.Auditcode = Auditcode;
    }


    public String getSystemAudit() { return SystemAudit; }

    public void setSystemAudit(String SystemAudit) {
        this.SystemAudit = SystemAudit;
    }
}

SystemAuditAdapter.java

    package com.android.mezohn.unl.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.android.mezohn.unl.R;
import com.android.mezohn.unl.model.model;

import java.util.ArrayList;



public class SystemauditAdapter extends BaseAdapter {

    private Context context;
    private ArrayList<model> models;


    public SystemauditAdapter(Context context, ArrayList<model> models) {
        this.context = context;
        this.models = models;
    }

    @Override
    public int getCount() {

        return models.size();
    }

    @Override
    public Object getItem(int position) {

        return models.get(position);
    }

    @Override
    public long getItemId(int position) {

        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if(convertView ==null){
            convertView = View.inflate(context, R.layout.list_view,null);

            TextView code = convertView.findViewById(R.id.cardcode);
            TextView title = convertView.findViewById(R.id.cardTitle);
            model model = models.get(position);
            code.setText(model.getAuditcode());
            title.setText(model.getSystemAudit());


        }

        return convertView;
    }
}
mezohn adhikari
  • 87
  • 1
  • 3
  • 12

2 Answers2

1

Create an inner class called ViewHolder like below:

static class ViewHolder {
    TextView code;
    TextView title;
}

Then modify your adapters getView() method like below:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    Model model = models.get(position);
    ViewHolder holder = null;
    if(convertView ==null){
        holder = new ViewHolder();
        convertView = View.inflate(context, R.layout.list_view,null);
        holder.code = convertView.findViewById(R.id.cardcode);
        holder.title = convertView.findViewById(R.id.cardTitle);
        converView.setTag(holder);    
    }else{
        holder = (ViewHolder)converView.getTag();
    }
    holder.code.setText(model.getAuditcode());
    holder.title.setText(model.getSystemAudit());

    return convertView;
}
Mohit Charadva
  • 2,555
  • 1
  • 22
  • 30
-2

If you do not want duplicates on your list you could use HashSet. ArrayList allow us store duplicate Object on it.

To remove duplicates object on your Arraylist you could do this:

ArrayList<model> SystemAuditList = new ArrayList<>();
SystemAuditList.add(new model("S1", "Access Control"));
SystemAuditList.add(new model("S2", "Contractor Safety"));
SystemAuditList.add(new model("S3", "PTW Standard"));
SystemAuditList.add(new model("S4", "Incident Investigation"));
SystemAuditList.add(new model("S5", "Technology and Subtle change"));
SystemAuditList.add(new model("S6", "Work at height"));
SystemAuditList.add(new model("S7", "Confined Spaces"));
SystemAuditList.add(new model("S8", "Emergency Preparedness and EPI"));
SystemAuditList.add(new model("S9", "Effluent Treatment and Waste Management "));
SystemAuditList.add(new model("S10", "Storage and Handling of Hazardous chemicals"));
SystemAuditList.add(new model("S11", "Portable Electrical Appliances"));
SystemAuditList.add(new model("S12", "Machine Guarding"));
SystemAuditList.add(new model("S13", "Lifting Tools"));
SystemAuditList.add(new model("S14", "Canteen"));
SystemAuditList.add(new model("S15", "Personal Change Management"));
SystemAuditList.add(new model("S16", "Hazard Checklist"));
SystemAuditList.add(new model("S17", "Hot Work"));
SystemAuditList.add(new model("S18", "Ladder Safety Checklist"));
SystemAuditList.add(new model("S19", "LOTO Checklist"));
SystemAuditList.add(new model("S20", "SBO Checklist"));
SystemAuditList.add(new model("S21", "Vehicle Marshal system Checklist"));
SystemAuditList.add(new model("S22", "Gate,Drain and Shutter"));

Set<String> uniqueList = new HashSet<>();
uniqueList.addAll(SystemAuditList);
Log.d("unique list count", String.valueOf(uniqueList.size()));
SystemAuditList.clear(); // clear the list
SystemAuditList.addAll(uniqueList); // add unique list to the Arraylist

return SystemAuditList;
Cak Bud
  • 190
  • 2
  • 9