1

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());
    }
7
  • Why are you populating array? When matching method is triggered ? Commented Jan 16, 2018 at 7:12
  • I only want to auto select item if macthed Commented Jan 16, 2018 at 7:16
  • do you want selectable items in your listview ? Commented Jan 16, 2018 at 7:18
  • I don't want to populate array second time...How do i do that Commented Jan 16, 2018 at 7:21
  • it was not the answer what i asked but. I think you don't want to populate array if checked name already in the array. If i am right, you can check if array contains that value. with 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. Commented Jan 16, 2018 at 7:27

3 Answers 3

3

There should be replacements.

Replace

String name =

with

String name =NameSingleton.getInstance().getArray().get(i);

Also replace

if (count!=0){

with

if (count!=0 && count<=dataSet.size()){
Sign up to request clarification or add additional context in comments.

1 Comment

your answer make sense
2

You can get the value of array list for any index by using

ArrayList.get(indexValue)

In your code you can do the following:

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 = NameSingleton.getInstance().getArray().get(i);
            //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);

                }
            }
        }
    }

Hope this helps you.

Comments

1
ArrayList<String> nameSingletonList = NameSingleton.getInstance().getArray();
count = nameSingletonList.size();
if (count!=0){
    for (int i=0;i<count;i++){
        //Here i want to get value from singleton arraylist class
        String name =nameSingletonList.get(i);
        //here i want to match that value from listview value if matched then auto select that index in listview
        for (int j=0;j<dataSet.size();j++){
            UserListingModel model = dataSet.get(j);
            if (model.getName().equals(name)){
                onIconClicked(j);
            }
        }
    }
}

In adition you shouldn't get instance every time in your for loop. Just call once then use it multiple time

UPDATE

public void addToArray(String value) {
    if(list !=null && !list.contains(value)){
        list.add(value);
    }
}

public void removeArray(String value){
    if(list !=null && list.contains(value)){
       list.remove(value);
    }
}

Beside you need to change isPressed when it is pressed

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());
    isPressed=false;
}

12 Comments

java.lang.IndexOutOfBoundsException: Invalid index 12, size is 12
UserListingModel model = dataSet.get(j);
did you use "<" or "<=" in your loop ?
for (int j=0;i<dataSet.size();j++){ UserListingModel model = dataSet.get(j); if (name.equals(model.getName())){ onIconClicked(j); } }
you should change it to for(int j = 0; j<dataSet.size();j++) not i<dataSet.size()
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.