1

在我的应用程序中,我有 1 个数据库,我需要使用 ListView 显示该数据库中的数据。任何人请帮助我。

这就是我从数据库中获取数据的方式:

db.open();
    Cursor c = db.getAllTitles();
    if (c.moveToFirst()) {
       do {
            //in this i have to display the data
            DisplayTitle(c)
       } while (c.moveToNext());
    }           
4

6 回答 6

1

SimpleCursorAdapter您的案例适合ListView 更多详细信息,请查看此链接

于 2012-05-21T11:01:43.950 回答
1

这些链接应该对您有所帮助。

Android、ListView 和数据库

来自 SQLiteDatabase 的数据的 ListView - Android

于 2012-05-21T11:04:53.570 回答
1

SimpleCursorAdapter可用于从 中获取数据database并显示在 中ListView

这是实现您的要求的示例代码:

  int[] names = new int[] {R.id.Full_Name,R.id.name};
 private static final String fields[] = {"DatabaseColumn_Name1", "DatabaseColumn_Name2"}; 
  ListView = (ListView)findViewById(R.id.list1);
  DataBaseHelper myDbHelper = new DataBaseHelper(null);
  myDbHelper = new DataBaseHelper(this);
  String sql ="SELECT STATEMENT";
  Cursor cdata = myDbHelper.getView(sql);
  if (cdata != null) 
{
    cdata.moveToFirst();
    while (cdata.isAfterLast() == false) {
        String tx = (cdata.getString(2));
        cdata.moveToNext();
}
startManagingCursor(cdata); 
CursorAdapter adaptr = new MyCursorAdapter( 
            getApplicationContext(), R.layout.listview1, cdata, fields, names);
于 2012-05-21T11:06:14.607 回答
1
private void fillData() {
    Cursor notesCursor = mDbHelper.fetchAllNotes();
    startManagingCursor(notesCursor);


    // Create an array to specify the fields we want to display in the list (only TITLE)
    String[] from = new String[]{NoteDb.KEY_TITLE, NoteDb.KEY_SDATE, NoteDb.KEY_STIME};

    // and an array of the fields we want to bind those fields to (in this case just text1)
    int[] to = new int[]{R.id.text1 , R.id.dateInNotes, R.id.timeInNotes};

    // Now create a simple cursor adapter and set it to display
    SimpleCursorAdapter notes = 
        new SimpleCursorAdapter(this, R.layout.notes_row, notesCursor, from, to);
    setListAdapter(notes);

    //notesCursor.close();

}
于 2012-05-21T11:12:18.313 回答
1

您可以将数据存储在 arrayList 中,并使用 basedapter 扩展布局

于 2012-05-21T11:07:54.503 回答
1
nombresC = (Cursor) ayudabbdd.getCursorNombres();   
        startManagingCursor(nombresC);
        nombresC.moveToFirst();

        //Para crear un simpleCursorAdapter necesitamos
        //Contexto this
        //Layour donde se mostrara el resultado, generalmente un textview
        //Cursor 
        //Cual sera el campo que recibiremos de la BBDD
        //Donde tenemos que poner esa informacion, generalmente el ID correspondiente al textvies del layour del segundo parametro



        String[] deNombre = new String[]{DataBaseHelper.CNOMBRE};
        int[] aNombre = new int[]{R.id.nombreLugar};
        lugaresNombre = new SimpleCursorAdapter(this, R.layout.entrada_lista, nombresC, deNombre, aNombre);
        setListAdapter(lugaresNombre);
        listaview= getListView();
于 2012-05-21T12:35:52.240 回答