什么是列出SQLite中Android的所有行的最好方法? [英] What is the best way to list all rows in Sqlite Android ?

查看:155
本文介绍了什么是列出SQLite中Android的所有行的最好方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前使用以下code通过一些列的所有项目 Arrayadapter

I'm currently using the following code to pass all items in some columns to 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);
    }

有没有更好的方法来做到这一点?

Is there a better method to accomplish this?

推荐答案

在我的应用我somethink使用这样的:

in my app i use somethink like this:

    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的进入适配器;)
像这样的:

and then, you can put arraylist into adapter ;) like this:

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

这是我的简单的适配器:

this is my simple 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;
    }
}

这篇关于什么是列出SQLite中Android的所有行的最好方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆