I have an app which contains singleton array list and have the Main activity with listview I am trying to get a value of singleton ArrayList and match with a value of listview and if match then I want to auto select that index. How do I do that
code of singleton ArrayList class
private static NameSingleton ourInstance;
private ArrayList<String> list = null;
public static NameSingleton getInstance() {
if(ourInstance == null)
ourInstance = new NameSingleton();
return ourInstance;
}
private NameSingleton() {
list = new ArrayList<>();
}
// retrieve array from anywhere
public ArrayList<String> getArray() {
return this.list;
}
//Add element to array
public void addToArray(String value) {
list.add(value);
}
public void removeArray(String value){
list.remove(value);
}
code for matching value from singleton with listview value:-
count = NameSingleton.getInstance().getArray().size();
if (count!=0){
for (int i=0;i<count;i++){
//Here i want to get value from singleton arraylist class
String name =
//here i want to match that value from listview value if matched then auto select that index in listview
for (int j=0;i<dataSet.size();j++){
UserListingModel model = dataSet.get(j);
if (model.getName().equals(name)){
onIconClicked(j);
}
}
}
}
code for item click:- When i click item 1st time it will selected and on click same item second time it will unselected.
boolean isPressed = true;
if (isPressed){
Log.e(TAG,"onceClicked");
UserListingModel model = dataSet.get(position);
String name = model.getName();
Log.e(TAG,"Name"+name);
NameSingleton.getInstance().addToArray(name);
isPressed=false;
}else {
Log.e(TAG,"AgainClicked");
UserListingModel model = dataSet.get(position);
Log.e(TAG,"Removed data"+model);
NameSingleton.getInstance().removeArray(model.getName());
}
listview?array.contains()or you can hold selected position in other array. If clicked position is in your selectedPositions array you can remove that position from array and unselect the item. if clicked position is not in your selectedPositions array you can add that position to array and select the item.