簡體   English   中英

如何從ArrayList的ListView上獲取String值 <String[]> ?

[英]How do I get String values on listview from the ArrayList<String[]>?

嗨,我之前曾問過如何更新listview並得到使用ArrayAdapter的建議。我關注的是現在的問題是它正在更新listviews但未包含某些對象的期望值。

我的代碼是這樣的:

Cursor cursor = db.rawQuery("SELECT count(*) FROM BOOKMARKS ", null);
        //if any contact found moveToFirst of that contacts or points to 1st email 
        cursor.moveToFirst();
        //checking columns exhist or not
        if (cursor.getInt(0) > 0)
        {
            //get all the contacts from table 'BOOKMARKS'
            c = db.rawQuery("SELECT * FROM BOOKMARKS", null);
            //get the index of mentiond and required columns 
            ID = c.getColumnIndex("ID");
            booktitl = c.getColumnIndex("Booktiltle");

            audiotime=c.getColumnIndex("BookmarkTime");

            // Check result.
            c.moveToFirst();
            if (c != null) 
            {
                // Loop through all Results
                do 
                {
                    cont = new String[3];
                    cont[0] = Integer.toString(c.getInt(ID));
                    cont[1] = c.getString(booktitl);
                    cont[2] = c.getString(audiotime);
                    audiobooks.add(cont);

                }
                while(c.moveToNext());

c.close();
                db.close();//close db resources 


                ArrayAdapter<String[]> arrayAdapter1=new ArrayAdapter<String[]>(this,  R.layout.row3, R.id.itemr1,audiobooks);
                lv.setAdapter(arrayAdapter1);

我如何在listview上獲取字符串值?實際上是從數據庫中獲取並存儲到Arraylist中的值。目前,我正在listview上顯示一些對象值,但我想顯示“有聲讀物”保存的內容。 請幫我解決。

您不能給ArrayAdapter類型String[] ,因為要給它提供一個名為有聲書的List

另外,我不確定標准ArrayAdapter可以處理數據類型List<String[]> 它可能不起作用。 如果它不起作用,則必須通過擴展它來實現自己的ArrayAdapter實現。

如果我理解正確,則想用數據庫中每一行的數據填充ListView。 您必須創建一個新的Audiobook類型的類,其成員變量與數據庫行相對應。 然后在res / layouts文件夾中創建一個必須顯示對象數據的布局。 創建一個新的擴展ArrayAdapter類的類,在其中您可以使用創建的布局為視圖填充視圖,並在子視圖中填充listview。 然后在您的主機中將新適配器設置為列表。

ArrayAdapter應該看起來像這樣:

public class AudiobookAdapter extends ArrayAdapter<Audiobook> {
     //  constructor

     //  here you populate each view and put it in listview
     public View getView (int position, View convertView, ViewGroup parent)
{
    }
}

這是起點,您應該閱讀有關擴展arrayadapters和自定義ListView的更多信息。

暫無
暫無

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

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