iT邦幫忙

0

android ArrayList列出資料問題

  • 分享至 

  • xImage
 private void display_bundle() {
        SQLiteDatabase db=DH.getWritableDatabase();
        ContentValues values=new ContentValues();
        ListView LV1=(ListView)findViewById(R.id.seeto);//讀取元件
        // 查詢資料庫並載入
        Cursor cursor=db.query("missing",new String[]{ "variety", "old ", "hair","where_from","flesh", "sex" },null,null,null,null,null);
       List<Map<String,Object>> items=new ArrayList<Map<String,Object>>();
       cursor.moveToFirst();
       //叫出資料庫的資料
        for(int i=0;i< cursor.getCount();i++){
           Map<String,Object> item=new HashMap<String,Object>();
           item.put("variety",cursor.getString(0));
           item.put("old",cursor.getString(1));
           item.put("hair",cursor.getString(2));
           item.put("where_from",cursor.getString(3));
           item.put("flesh",cursor.getString(4));
            item.put("sex",cursor.getString(5));
           items.add(item);//新增
           cursor.moveToNext();//移下一筆資料
        }
        SimpleAdapter SA=new SimpleAdapter(this,items,R.layout.people_look_for,new String[]{"variety","where_from","sex"},new int[]{R.id.place,R.id.variety_thing,R.id.sex});
        LV1.setAdapter(SA);
//     
        goLookfor_people = new ArrayList<>();
        goLookfor_people.add(new Lookfor_people("??","??","??","?" ));
        goLookfor_people.add(new Lookfor_people("高雄市","女","人類",R.drawable.dodo));
        goLookfor_people.add(new Lookfor_people("台南市","男","土狗",R.drawable.dodo));
        goLookfor_people.add(new Lookfor_people("高雄市","女","貴濱",R.drawable.dodo));
        goLookfor_people.add(new Lookfor_people("台東市","女","哈士奇",R.drawable.dog_1));
        goLookfor_people.add(new Lookfor_people("澎湖市","女","黃金",R.drawable.dog_2));
        goLookfor_people.add(new Lookfor_people("厚德市","女","娓娓",R.drawable.dog_3));
        RecyclerView myra =findViewById(R.id.recycle_id_people);
        RecyclerViewAdapter_lookfor_people myAdapter =new RecyclerViewAdapter_lookfor_people(this,goLookfor_people);
        myra.setLayoutManager(new GridLayoutManager(this,3));
        myra.setAdapter(myAdapter);

    }
```!

![https://ithelp.ithome.com.tw/upload/images/20181204/201096856gi8WaymWF.png](https://ithelp.ithome.com.tw/upload/images/20181204/201096856gi8WaymWF.png)
如goLookfor_people.add(new Lookfor_people("??","??","??","?" )); 我想使用資料庫裡的資料 不是假資料 我該怎麼打
看更多先前的討論...收起先前的討論...
ant1017 iT邦新手 2 級 ‧ 2018-12-03 15:15:23 檢舉
先想一下你想呈現的資料,透過簡單的迴圈撈資料,然後在用迴圈擺進去
抱歉 因為我就是不知道怎麼把資料庫對印的欄位 填到我要的地方
小魚 iT邦大師 1 級 ‧ 2018-12-03 21:08:45 檢舉
Android我們一般是不會直接連資料庫,
會透過一個API去抓資料庫的資料.
就是把 69行的資料庫資料 給到78行裡 謝謝
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 個回答

0
海綿寶寶
iT邦大神 1 級 ‧ 2018-12-04 10:13:26
最佳解答

https://ithelp.ithome.com.tw/upload/images/20181204/2000178725pXSqfOxg.png
你都會用上傳圖片的功能了
建議你使用左邊兩格的「程式碼」功能(紅色框框)
把程式碼貼上來
可能會有更多人回答你的問題

用你的程式改寫成下段
拿去改
看能不能適用

Cursor cursor=db.query("lookforpeople",new String[]{ "city", "gender", "variety"},null,null,null,null,null);
ArrayList goLookfor_people = new ArrayList<>();
cursor.moveToFirst();
//叫出資料庫的資料
for(int i=0;i< cursor.getCount();i++){
   goLookfor_people.add(new Lookfor_people(cursor.getString(0),cursor.getString(1),cursor.getString(2),R.drawable.dodo));//新增
   cursor.moveToNext();//移下一筆資料
}

好的謝謝

已成功感恩

我要發表回答

立即登入回答