Мои знания в области программирования невелики, я открыл для себя Java и Android SDK несколько дней назад.
У меня SQLiteDatabase, SQLiteOpenHelper и курсор работают правильно
(значит, я думаю, что понял, как создать / открыть БД с помощью 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();
И листинг.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 прост, просто определите несколько столбцов, если база данных не существует, вместе с некоторыми переменными.