O_Casper_ne 2013-02-25 08:47 采纳率: 7.7%
浏览 2150
已采纳

列出 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);
    }
  • 写回答

2条回答 默认 最新

  • yongyong_21 2013-02-25 09:50
    关注

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

     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条)

报告相同问题?

悬赏问题

  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建