开发者

Begginner working with ListAdapters / SQLite Database

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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜