I have a problem dealing with the duplication of my ListView
. As you can see on the screenshot, I have a spinner
that contains all of the items needed. when the user clicks that specific item, it will add on my ListView
. but the problem is the data being duplicated.
This is the Image:
SCENARIO: pretend this is the list items
A
B
C
D
If I click "A". It will add on my ListView
right?. and click "B". so the data on my ListView
is: A and B. If I click again "A", It will add which it has the current Item on my ListView
. I need to restrict the duplication that is happening right now.
This is the code when I click my Spinner
public void addViolationListenerOnSpinnerItemSelection() {
spinner_violation.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent2, View view, int position2, long id) {
if (position2 != 0) {
vio_val = String.valueOf(spinner_violation.getSelectedItem());
adapter = new ViolationAdapter(getContext(), generateData());
lv_inspector_violation.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
this is the generateData()
private ArrayList<InspectorViolationItem> generateData() {
items.add(new InspectorViolationItem(emp_val, vio_val));
HashSet<InspectorViolationItem> hs = new HashSet<>();
hs.addAll(items);
items.clear();
items.addAll(hs);
return items;
}
the InspectorViolationItem
is just a Getters
and Setters
so I think that don't need to post the code.
And lastly, This the Adapter that I've used:
public class ViolationAdapter extends ArrayAdapter<InspectorViolationItem> {
Context c;
private ArrayList<InspectorViolationItem> itemsArrayList;
public ViolationAdapter( Context context, ArrayList<InspectorViolationItem> itemsArrayList) {
super(context, R.layout.item_violation, itemsArrayList);
this.c = context;
this.itemsArrayList = itemsArrayList;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// 1. Create inflater
LayoutInflater inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// 2. Get rowView from inflater
View rowView = inflater.inflate(R.layout.item_violation, parent, false);
// 3. Get the two text view from the rowView
TextView labelView = (TextView) rowView.findViewById(R.id.txt_violation_employee);
TextView valueView = (TextView) rowView.findViewById(R.id.txt_violation_violation);
// 4. Set the text for textView
labelView.setText(itemsArrayList.get(position).getEmployee());
valueView.setText(itemsArrayList.get(position).getViolation());
// 5. retrn rowView
return rowView;
}
}