3 回答

TA貢獻1828條經驗 獲得超4個贊
由于您的 arraylist 是 categoryoptions_class 類型,但您想要通過 index of 獲取的是一個字符串,因此它顯然會返回 -1(未找到)。這是您可以嘗試的東西:
如果您的 categoryoptions_class 包含字符串類型的數據成員,并且您想要比較與該數據成員匹配的對象,則遍歷 arraylist 并訪問每個對象的該數據成員,然后在匹配時 breakout else 打印 -1。
否則,如果您的 categoryoptions_class 沒有將任何數據成員作為字符串進行比較,那么您將需要構造一個 categoryoptions_class 的對象,然后您可以使用 indexOf 選項。

TA貢獻1797條經驗 獲得超6個贊
這里:
int sa = Arrays.asList(arrayList).indexOf(a);
那根本沒有意義。即arrayList
聲明為:
ArrayList<categoryoptions_class> arrayList;
所以,您正在做的是:將 aArrayList<categoryoptions_class>
變成 a List<ArrayList<categoryoptions_class>>
。然后您詢問該列表是否包含字符串。
某物列表的列表肯定不會包含String 對象。
不幸的是,您的其余代碼非常混亂,以至于我無法告訴您必須如何準確地“搜索”您的arrayList
對象。

TA貢獻2039條經驗 獲得超8個贊
int index= 0;
for (categoryoptions_class categoryoptions_class : arrayList) {
if (categoryoptions_class.getCategory_opt_name().equals(a)) {
index= arrayList.indexOf(categoryoptions_class);
Log.d("Position", mArrayList.indexOf(products) + "");
}
}
添加回答
舉報