程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> listview-列出 Sqlite 中的內容

listview-列出 Sqlite 中的內容

編輯:編程綜合問答
列出 Sqlite 中的內容

我使用如下的代碼想把一些列中所有的 items 傳遞到 arrayadapter。有什麼好方法能實現?

private void TestListAll(){
        //Displays the whole list.
        cursor = db.rawQuery("SELECT _id, firstName, lastName, title FROM employee WHERE firstName || ' ' || lastName LIKE ?", 
                  new String[]{"%" + "" + "%"});
        adapter = new SimpleCursorAdapter(
                this, 
                R.layout.emp_list_item, 
                cursor, 
                new String[] {"firstName", "lastName", "title"}, 
                new int[] {R.id.firstName, R.id.lastName, R.id.title});
        setListAdapter(adapter);
    }

最佳回答:


在我的應用程序中我使用:

 public ArrayList<HashMap<String, ?>> selectLastPlayed() {
    ArrayList<HashMap<String, ?>> list = new ArrayList<HashMap<String, ?>>();
    Cursor cursor = this.db.query("employee Order BY ID DESC",
            new String[] { "*" }, null, null, null, null, null);
    if (cursor.moveToFirst()) {
        do {
            HashMap<String, Object> temp = new HashMap<String, Object>();
            temp.put("Icon", cursor.getString(1));
            temp.put("Title", cursor.getString(2));
            list.add(temp);
        } while (cursor.moveToNext());
    }
    if (cursor != null && !cursor.isClosed()) {
        cursor.close();
    }
    return list;
}

然後,你可以把 arraylist 放入 adapter 中:

SimpleAdapter adapter = new MySimpleAdapter(this, selectLastSearch(), R.layout.custom_row_view, new String[] { "Icon", "Icon" }, new int[] { R.id.textView1123, R.id.imageView12 });

adapter:

 public class MySimpleAdapter extends SimpleAdapter {
    Context localcontext = null;
    public MySimpleAdapter(Context context,List<? extends Map<String, ?>> data, int resource, String[] from, int[] to) {
        super(context, data, resource, from, to);
        localcontext = context;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = super.getView(position, convertView, parent);
        return view;
    }
}
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved