如何获取ArrayList索引值?

时间:2018-01-16 06:22:00

标签: android listview

我有一个包含单例数组列表的应用程序,并且具有listview的Main活动我试图获取singleton ArrayList的值并匹配listview的值,如果匹配则我想自动选择该索引。我该怎么做

单例ArrayList类的代码

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);
}

将来自singleton的值与listview值匹配的代码: -

 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);

                }
            }
        }
    }

项目点击代码: - 当我第一次单击项目时,它将被选中并在第二次单击相同项目时将被取消选择。

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

3 个答案:

答案 0 :(得分:3)

应该有替补。

替换

String name =

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

同时替换

if (count!=0){

if (count!=0 && count<=dataSet.size()){

答案 1 :(得分:2)

您可以使用

获取任何索引的数组列表的值

ArrayList.get(indexValue)

在您的代码中,您可以执行以下操作:

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);

                }
            }
        }
    }

希望这会对你有所帮助。

答案 2 :(得分: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);
            }
        }
    }
}

在adition中,你不应该每次都在for循环中获取实例。只需拨打一次,然后多次使用

<强>更新

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);
    }
}

除了你需要在按下时更改isPressed

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;
}