簡體   English   中英

如何從ArrayList中分離數據<Object>

[英]how to separate data from a ArrayList<Object>

我有一個從sqlite查詢返回ArrayList的方法,只有1行,這個數組必須有3個字符串; 名稱,描述和照片(這是一個補丁字符串)。 但我不知道如何分離數據。

public ArrayList<Object> getLugarPorNombre(String nombre)
{

    ArrayList<Object> rowArray = new ArrayList<Object>();
    Cursor cursor;

    try
    {

        cursor = db.query
        (
                TABLE_NAME,
                new String[] { TABLE_ROW_ID, CNOMBRE, CDESC, CFOTO },
                CNOMBRE + "=" + nombre,
                null, null, null, null, null
        );

        //movemos el puntero a la primera posicion
        cursor.moveToFirst();

        // Si hay datos los añado al array que sera devuelto
        if (!cursor.isAfterLast())
        {
            do
            {
                rowArray.add(cursor.getLong(0));
                rowArray.add(cursor.getString(1));
                rowArray.add(cursor.getString(2));
                rowArray.add(cursor.getString(3));
            }
            while (cursor.moveToNext());
        }

        cursor.close();
    }
    catch (SQLException e) 
    {
        Log.e("Error obteniendo datos de lugar", e.toString());
        e.printStackTrace();
    }

    return rowArray;
}

   ayudabbdd = new DataBaseHelper(this);
    ArrayList<Object> datosLugar = ayudabbdd.getLugarPorNombre(nombreClick);

我可以從單獨的字符串中獲取數組列表或描述中的名稱嗎?

你的意思是這樣的:

String name = (String) datosLugar.get(1);
String description = (String) datosLugar.get(2);

您必須確保元素1和2始終是字符串,否則您將獲得ClassCastException。 如果你想讓它更干凈,你可以為你的返回類型創建一個自己的類。

當然你可以。但是你的ArrayList有4個項目,而不是你在上面的問題中提到的3個項目。你必須像這樣玩ArrayList

String name=datosLugar.get(1).toString();
String description=datosLugar.get(2).toString();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM