ArrayAdapter

时间:2018-02-15 06:41:03

标签: android

当我尝试通过以下代码获取项目索引时。

Company company = getDefaultCompany();
companyArrayAdapter.getPosition(company);

我总是得到-1的结果。我不明白什么是错的? 因为

companyArrayAdapter also have type Company.
private ArrayAdapter<Company> companyArrayAdapter;

接下来,您可以看到公司类声明。

@DatabaseTable(tableName=Company.TABLE_NAME)
public class Company {
    public static final String TABLE_NAME = "company";

    @DatabaseField(id = true, columnName = "id")
    private UUID id;

    @DatabaseField(canBeNull=false)
    private String name;

    @DatabaseField
    private  String address;

    @DatabaseField
    private  String phone;

    @ForeignCollectionField(eager = false)
    private ForeignCollection<Contract> contracts;

    public  Company(){

    }
}

2 个答案:

答案 0 :(得分:0)

ArrayAdapter使用List.indexOf()方法,它无法比较您的自定义Company类对象,并始终返回&#34; Not Found&#34; index(-1)。

因此,您应该覆盖扩展getPosition()的自定义适配器中的ArrayAdapter方法:

@Override
public int getPosition(@Nullable Company company) {
    /* here write logic of finding your company in companies list and retur index*/
     return index;
}

P.S 如果你有自定义适配器的片段,我会给你更详细的答案。

答案 1 :(得分:0)

你可以遍历列表循环

var list=adapter.your_list

var toMatch=yourObject

for((index,elem) in list.withIndex()){
if(elem.someUniqueProperty == toMatch.someUniqueProperty){
var needed_index=index
}
}