开发者社区> 问答> 正文

列出 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);
}

展开
收起
a123456678 2016-07-18 14:10:20 1699 0
1 条回答
写回答
取消 提交回答
  • 在我的应用程序中我使用:

     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;
        }
    }
    2019-07-17 19:57:49
    赞同 展开评论 打赏
问答分类:
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
2022 DTCC-阿里云一站式数据库上云最佳实践 立即下载
云时代的数据库技术趋势 立即下载
超大型金融机构国产数据库全面迁移成功实践 立即下载