初学者使用列表适配器/ SQLite数据库

时间:2010-01-16 15:44:57

标签: android

我的编程知识很少,几天前我发现了Java和Android SDK。

我有一个SQLiteDatabase,一个SQLiteOpenHelper和一个正常工作的Cursor (意思是我认为我理解如何使用SQLiteOpenHelper创建/打开数据库,在我的数据库上进行查询并获得游标)

目前,我将i + labName + labCity包装到单个结果字符串中,并显示为单个TextView R.id.label

是否可以将labName绑定到R.id.label,并将labCity绑定到另一个TextView R.id.label2?

以下是我需要帮助的ListActivity的代码:

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

我想修改listing.xml:

<?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>

我的GestionDB类很简单,如果数据库不存在,只需定义一些列,以及一些变量。

1 个答案:

答案 0 :(得分:0)

您应该使用CursorAdapter,也许是SimpleCursorAdapterHere is an example