subburaj
subburaj

Reputation: 161

Listview from database

In my application I have 1 database, and I need to display the data from that database using a ListView. Anybody please help me.

This is how I get data from database:

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

Upvotes: 1

Views: 319

Answers (5)

colymore
colymore

Reputation: 12316

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

Upvotes: 1

sachi
sachi

Reputation: 2132

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

}

Upvotes: 1

Harsh Dev Chandel
Harsh Dev Chandel

Reputation: 763

You can storw the data in an arrayList and and inflate thelayout using basedapter

Upvotes: 1

Avadhani Y
Avadhani Y

Reputation: 7626

SimpleCursorAdapter can be used to get the data from the database and display in the ListView.

Here is the sample code for implementing your requirement :

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

Upvotes: 1

Nishant
Nishant

Reputation: 32222

Your case suits SimpleCursorAdapter with ListView For more details check this link

Upvotes: 1

Related Questions