tags:

views:

142

answers:

1

Hello,

My programming knowledge is slim, i discovered Java and the Android SDK some days ago.

I have a SQLiteDatabase, a SQLiteOpenHelper, and a Cursor working properly ( means i think i understood how to create/open a DB using a SQLiteOpenHelper, make queries on my DB and get a cursor )

currently, i'm wrapping i + labName + labCity into a single results String, and display into a single TextView R.id.label

Is it possible to bind labName to R.id.label, and bind labCity to another TextView R.id.label2 ?

Here is the code of my ListActivity i need help with :

public class MainLabList extends ListActivity implements OnItemClickListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_lab_list);

    // DB
    GestionDB gdb = new GestionDB(this, GestionDB.DATABASE_NAME, null, GestionDB.DATABASE_VERSION);
    SQLiteDatabase theDB = gdb.getWritableDatabase();

    // Cursor
    String[] columns = {GestionDB.LAB_NAME, GestionDB.LAB_ADDRESS_CITY};
    Cursor c =
        theDB.query(GestionDB.TABLE_NAME, columns, null, null, null, null, null);

    ////////
    this.setTitle(" " + c.getCount() + " Labs in Local Database");
    ArrayList<String> results = new ArrayList<String>();
    int i = 0; 
    c.moveToFirst();

    /* Loop through all Results */ 
    do { 
         i++;         
         String labName = c.getString(c.getColumnIndex(GestionDB.LAB_NAME));
         String labCity = c.getString(c.getColumnIndex(GestionDB.LAB_ADDRESS_CITY)); 

         /* Add current Entry to results. */ 
         results.add("" + i + ": " + labName 
                        + " (" + labCity +  ")"); 
    } while (c.moveToNext());

    //need rework               
    ListView lvLabListing = (ListView) findViewById(android.R.id.list);
    ArrayAdapter<String> labListAdapter = new ArrayAdapter<String>(this,
            R.layout.listing, R.id.label, results );
    lvLabListing.setAdapter(labListAdapter);
    lvLabListing.setOnItemClickListener(this);
    //

    // don't forget to close the database
    gdb.close();

And the listing.xml i would like to modify :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_height="wrap_content" android:layout_width="fill_parent">
<ImageView android:id="@+id/ImageView01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/icon"></ImageView>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/label"></TextView>
</LinearLayout>

My GestionDB class is simple, just define some columns if the database doesn't exist, along with some variables.

A: 

You should use a CursorAdapter, perhaps a SimpleCursorAdapter, for this. Here is an example.

CommonsWare
Your source example was helpful.i used a SimpleCursorAdapter, but was stuck on some Exception : java.lang.IllegalArgumentException: column '_id' does not exist.This exception comes if your Query doesn't have the _ID column.Thanks alot for my first question :) solved
Dullahx
Yeah, that's one of the limitations of the `CursorAdapter` family -- you have to have a column in the result set named `_ID` that can be converted into a `long`.
CommonsWare