1. Download our Official Android App: Forums for Android!

Apps How to get CursorAdapter to Display ALL Columns from ListView??

Discussion in 'Android Development' started by ac4android, Sep 1, 2016.

  1. ac4android

    ac4android Well-Known Member
    Thread Starter
    Rank:
    None
    Points:
    53
    Posts:
    198
    Joined:
    Nov 3, 2015

    Nov 3, 2015
    198
    29
    53
    I am trying to print the values from several columns in my SQLite table by plugging my CursorAdapter into my ListView, like this:
    Code (Text):
    1.  
    2.   import android.widget.ListView;
    3. ...
    4.   ListView listProjectmanagers = getListView();
    5.   try{
    6.   SQLiteOpenHelper assetlocationDBhelper = new AssetLocationDBHelper(this);
    7.   SQLiteDatabase db = assetlocationDBhelper.getWritableDatabase();
    8.   Cursor cursor = db.query("PROJECTS",
    9.   new String[]{"_id", "Projectname", "Managername"},
    10.   null,  null,  null,  null,  null);
    11.   CursorAdapter listAdapter = new SimpleCursorAdapter(this,
    12.   android.R.layout.simple_list_item_1,
    13.   cursor,
    14.   new String[]{"Projectname", "Managername"},
    15.   new int[]{android.R.id.text1},
    16.   0);
    17.   // ...plug it into the outlet
    18.   listProjectmanagers.setAdapter(listAdapter);
    19.   //cursor.close();
    20.   //db.close();
    21.   } catch(SQLiteException sx) {
    22.   Toast toast = Toast.makeText(this, "ERROR Database unavailable", Toast.LENGTH_SHORT);
    23.   toast.show();
    24.   }
    25.   }
    26.  
    Currently it is returning only the first field (Projectname) from the cursor, and not the others.

    Is there a quick fix to this or do I need to create my own adapter class?

    I am importing "android.widget.ListView" because I don't have to code a layout file :)
     

    Advertisement

    #1 ac4android, Sep 1, 2016
    Last edited: Sep 1, 2016

Share This Page

Loading...