如何在SQLite数据库中填充android studio中的listview?

时间:2015-03-23 22:30:27

标签: android sqlite

我需要在android studio中创建一个listview,并用我在SQLite Studio中创建的数据库中的数据填充它。所以我需要在listview中显示三个内容,这三个内容在我的数据库中:图像,描述和基本上一些文本。我一直在观看一些视频,并且明白你需要创建一个数据库处理程序类,但这并不能直接连接到SQLite中的数据库,如果这样做,那么你需要创建表,但我的表已经创建并且包含数据。我只需要连接到该数据库,查询它,并从中填充listview,那么有谁知道我可以做到这一点吗?

2 个答案:

答案 0 :(得分:1)

ListView需要ListAdapter才能访问数据模型。

SQL查询的结果可以在Cursor中返回。

因此,为了连接这两个概念,有一个类CursorAdapter可以使用Cursor并创建一个ListAdapter,可以由ListView

使用

使用CursorAdapter搜索一些示例代码,您将看到如何完成此操作。

答案 1 :(得分:0)

这是怎么回事?

class MyCursorAdapter extends CursorAdapter {  
    Context  context=null;  
    int viewResId;  
    public MyCursorAdapter(Context context, int resource, Cursor cursor) {  
        super(context,cursor);  
        viewResId=resource;  
    }  
    public View newView(Context context, Cursor cursor, ViewGroup parent) {  

        TextView view =null;  
        LayoutInflater vi = null;  
        vi =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
        view =(TextView)vi.inflate(viewResId, parent, false);  
        //v =(TextView)vi.inflate(textViewResourceId,null);  
        Log.i("CursorAdapter","newView"+view);  
        return view;  
    }  
    @Override  
    public void bindView(View view, Context context, Cursor cursor) {  
        Log.i("hubin","bind"+view);  
        TextView nameView = (TextView) view;  
        // Set the name  
        nameView.setText(cursor  
            .getString(cursor.getColumnIndex("DISPLAY_NAME")));  
   }  
 }