简体   繁体   English

Android从AutoComplete中的Class结构中获取选定项的位置

[英]Android get selected item position from Class structure in AutoComplete

I'm trying to find Class structure position as an ArrayList from AutoComplete, for example I have this class structure: 我试图从AutoComplete的ArrayList中找到Class结构的位置,例如,我有这个class结构:

public class Clients {

    @DatabaseField(generatedId = true)
    public int id;

    @DatabaseField
    public int client_code;

    public int getClient_code() {
        return client_code;
    }
}

And I define ArrayList from this class as: 我从此类定义ArrayList为:

private ArrayList<Clients> customersList = new ArrayList<Clients>();

Now I have some data into this arrayList and I want to get selected position to find which client_code is selected. 现在我有一些数据到这个arrayList中,我想获得选择的位置以找到选择了哪个client_code

at_sender_package.setOnItemClickListener(new AdapterView.OnItemClickListener(){

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long rowId) {
        int selectedposition=position;
        Log.e("SELECTED ITEM: ", customersList.get(selectedposition).getClient_code()+"");
    }
});

Unfortunately I get this error: 不幸的是我得到这个错误:

java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0 at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251) at java.util.ArrayList.get(ArrayList.java:304) java.lang.IndexOutOfBoundsException:无效的索引0,在java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)的大小为0,在java.util.ArrayList.get(ArrayList.java:304)

my array list dont empty and i have 3 item into that and i dont have any problem to get selected text, i want to only get selected position not selected text 我的数组列表不为空,我有3个项目,我没有任何问题来获取所选文本,我只想获取所选位置而不是所选文本

for example : 例如 :

Log.e("POS: ", parent.getItemAtPosition(position) + "");

Result is: POS: Item 1) 结果是: POS: Item 1)

Here your list is empty and you are trying to get data from it. 在这里,您的列表为空,您正在尝试从中获取数据。

The proactive way to escape this error is to apply one check of list's length before using its elements. 避免此错误的主动方法是在使用列表元素之前对列表长度进行一次检查。

like : 喜欢 :

if(customersList.size() > 0) {
//do your stuff

}

You can read more about this error here : 您可以在此处阅读有关此错误的更多信息:

http://voidexception.weebly.com/array-index-out-of-bounds-exception.html http://voidexception.weebly.com/array-index-out-of-bounds-exception.html

java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0 java.lang.IndexOutOfBoundsException:无效的索引0,大小为0

means your arraylist, customersList is empty and you want to get item at position 0 which is invalid.Therefore first check customersList length as 表示您的arraylist, customersList为空,并且您想要在位置0处获得无效的项目,因此首先检查customersList长度为

if (customersList.size() > 0) {
    Log.e("SELECTED ITEM: ", customersList.get(selectedposition).getClient_code() + "");
} else {
    Log.e("SELECTED ITEM: ", "customersList is empty");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM