Android的:链接一个简单的光标移动到列表视图

问题描述:

我得如何从一个SQLite数据库获取数据的基本premise,我已经得到了它记录的项目回到logcat的。不过,我似乎无法在一个列表视图的最佳方式制定出输出数据。

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.

起初,我以为我把数据放到一个数组,并设置一个ListView使用数组,但是从寻找你周围可以直接作为数据源到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.

下面是我的MainActivity(一旦我的工作了多一点,我把SQL在它的自己的助手,但现在它的主要活动是所有)

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)

我的主要活动是:

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();

    }

}

我的布局activiy_main.xml是:

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>

我的表有三列,但现在我很乐意与它刚刚喷涌而出,在数据库中的所有FirstNames到Li​​stView

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

汤姆

看来你没有codeD的任一行以您的列表视图,你的问题不应该,因为你的贡献微弱的努力回答。总之,这里是演示code(不exaclty做你的所有要求)的答案:

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)创建 list_view_item.xml 来显示在列表视图的信息,例如:a。要显示出你的数据字段

(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)创建 DataBoundAdapter 来绑定到你的数据库游标的结果是:

(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) MainActivity.onCreate(..)

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