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
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 に答える
1

これらのリンクが役に立ちます。

Android、ListView、およびデータベース

SQLiteDatabase からのデータの ListView - Android

于 2012-05-21T11:04:53.570 に答える
1

SimpleCursorAdapterdatabaseからデータを取得し、に表示するために使用できます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

arrayList にデータを格納し、basedapter を使用してレイアウトを膨張させることができます

于 2012-05-21T11:07:54.503 に答える
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 に答える