如何从自定义数组列表中获取特定字符串的索引

我想获取函数返回值的索引,但它只给我 -1 值。


ArrayList<categoryoptions_class> arrayList;

rb = new RadioButton[arrayList.size()];


            for (int i = 0; i < arrayList.size(); i++) {

                rb[i] = new RadioButton(detail_activity_rb.this);

                rb[i].setPadding(5,5,5,5);

                rb[i].setId(arrayList.get(i).getId());

                rb[i].setTag(arrayList.get(i).getValue());

                rb[i].setText(arrayList.get(i).getCategory_opt_name());


                String a = checkdata(cat_id, rb[i].getId(), i);

// this 'a' is returning perfect value but when i am trying to get the index of the value in a it gives me only -1; 


int sa = Arrays.asList(arrayList).indexOf(a);

Toast.makeText(detail_activity_rb.this, sa + " :val", 

Toast.LENGTH_LONG).show();


                //  int sa = i;

                if (i == sa) {

                    rb[i].setChecked(true);

                }


                rg.setPadding(5,5,5,5);

                rg.addView(rb[i]);

                checkpos(i,i);

}


 public String checkdata(int cat_ids, int optids, int ia) {

    String a = cat_ids + "";

    String b = optids + "";

    String c  = ia + "";

    String aa = a + b;

    Cursor cursor;

   // Toast.makeText(getApplicationContext(),aa,Toast.LENGTH_LONG).show();

    if(cat_type.equals("rb1"))

    {

         cursor = dbController.getdata("SELECT * FROM store_values WHERE catopt_id = '"+cat_ids+"';");

    }

    else

    {

         cursor = dbController.getdata("SELECT * FROM store_values WHERE catopt_id = '"+aa+"';");

    }


    //   arrayList.clear();

    if (cursor.getCount() != 0)

    {

        String val = "";

        while (cursor.moveToNext())

        {

            val = cursor.getString(8);

        }

        //Toast.makeText(detail_activity_rb.this,"H",Toast.LENGTH_LONG).show();

        return val;

    }

    else

    {

        return "not";

    }


}


眼眸繁星
浏览 107回答 3
3回答

明月笑刀无情

由于您的 arraylist 是 categoryoptions_class 类型,但您想要通过 index of 获取的是一个字符串,因此它显然会返回 -1(未找到)。这是您可以尝试的东西:如果您的 categoryoptions_class 包含字符串类型的数据成员,并且您想要比较与该数据成员匹配的对象,则遍历 arraylist 并访问每个对象的该数据成员,然后在匹配时 breakout else 打印 -1。否则,如果您的 categoryoptions_class 没有将任何数据成员作为字符串进行比较,那么您将需要构造一个 categoryoptions_class 的对象,然后您可以使用 indexOf 选项。

FFIVE

这里:int&nbsp;sa&nbsp;=&nbsp;Arrays.asList(arrayList).indexOf(a);那根本没有意义。即arrayList声明为:ArrayList<categoryoptions_class>&nbsp;arrayList;所以,您正在做的是:将 aArrayList<categoryoptions_class>变成 a&nbsp;List<ArrayList<categoryoptions_class>>。然后您询问该列表是否包含字符串。某物列表的列表肯定不会包含String 对象。不幸的是,您的其余代码非常混乱,以至于我无法告诉您必须如何准确地“搜索”您的arrayList对象。

largeQ

int index= 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (categoryoptions_class categoryoptions_class : arrayList) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (categoryoptions_class.getCategory_opt_name().equals(a)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; index= arrayList.indexOf(categoryoptions_class);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.d("Position", mArrayList.indexOf(products) + "");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java