Android: Linking a simple cursor to listview

半世苍凉 提交于 2019-12-12 01:38:33

问题


I've got the basic premise of how to get data from a sqlite db, and i've got it to log an item returned to the logcat. However, I can't seem to work out the best way to output that data in to a listview.

At first I thought i'd put the data in to an array, and setup a listview using that array, however from looking around you can link the Cursor directly as a datasource to a listview but I can't quite get my head around it.

Here is my MainActivity (once i've worked it out a bit more, i'd put the sql in to it's own helper class, but for now it's all from the main activity)

My main activity is:

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

       SQLiteDatabase db = openOrCreateDatabase("MyDB", MODE_PRIVATE, null);
       Cursor c = db.rawQuery("SELECT * FROM MyTable", null);
       c.moveToFirst();
       Log.e("TomDebug", c.getString(c.getColumnIndex("FirstName")));
       db.close();

    }

}

My layout activiy_main.xml is:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="fill" >

    <ListView
        android:id="@+id/derooms"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" >
    </ListView>

</RelativeLayout>

My table has three columns, but for now i'd be happy with it just spewing out all the FirstNames in the database to a listview

Tom


回答1:


It seems you haven't coded any line to your listview, your question should not be answered because of your weak effort in contribution. Anyway, here is the demo code (not exaclty do all your requirement) for the answer:

(1) Create list_view_item.xml to show your information in the list view, for e.g: a to show out your data field.

(2) Create DataBoundAdapter to bound to your result of the DB cursor:

public class DataBoundAdapter extends CursorAdapter 
{
    Context _context;

    public DataBoundAdapter(Context context, Cursor c, boolean autoRequery) {
        super(context, c, autoRequery);
        _context = context;
    }

    @Override
    public void bindView(View view, Context c, Cursor cur) 
    {
        // TODO: handle data when binding to your list view
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) 
    {
        int item_view_id = R.layout.list_view_item;

        //inflate item view to list view holder
        LinearLayout holderView = new LinearLayout(_context);
        String inflaterName = Context.LAYOUT_INFLATER_SERVICE;
        LayoutInflater inflater = (LayoutInflater) _context.getSystemService(inflaterName);
        inflater.inflate(item_view_id, holderView, true);

        return holderView;
    }
}

(3) In MainActivity.onCreate(..):

ListView myListView = (ListView)findViewById(R.id.derooms);
DataBoundAdapter dbAdapter = new DataBoundAdapter(this, your_db_cursor, true);
myListView.setAdapter(dbAdapter);



回答2:


You need an CursorAdapter. see http://developer.android.com/reference/android/widget/CursorAdapter.html

you can use a SimpleCursorAdapter to begin. If you are targetting 11+, you'll want to take a look at http://developer.android.com/reference/android/content/CursorLoader.html



来源:https://stackoverflow.com/questions/12263095/android-linking-a-simple-cursor-to-listview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!