我從數據庫的名稱填充列表視圖。這一切工作正常。點擊名稱後,我想設置onListItemClick
填寫其他活動的文字瀏覽。那是我卡住的地方。由sql db填充的listview與onListItemClick填充其他活動中的文本
這裏我的列表視圖活動這是工作代碼:
public class DataListView extends ListActivity {
private ArrayList<String> results = new ArrayList<String>();
private SQLiteDatabase newDB;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.hotelslist);
openAndQueryDatabase();
displayResultList();
}
private void displayResultList() {
ListView hotelslist = (ListView) findViewById(android.R.id.list);
hotelslist.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, results));
getListView().setTextFilterEnabled(true);
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Toast.makeText(getBaseContext(), l.getItemAtPosition(position).toString(), Toast.LENGTH_SHORT).show();
}
private void openAndQueryDatabase() {
try {
DataBaseHelper newDB = new DataBaseHelper(this, "mydatabase.sqlite");
SQLiteDatabase sdb = newDB.getReadableDatabase();
Cursor c = sdb.rawQuery("SELECT name FROM hotel ORDER BY name ASC", null);
if (c != null) {
if (c.moveToFirst()) {
int i = 0;
do {
i++;
String name = c.getString(c.getColumnIndex("name"));
results.add(name);
}while (c.moveToNext());
}
}
} catch (SQLiteException se) {
Log.e(getClass().getSimpleName(), "Could not create or Open the database");
} finally {
if (newDB != null)
newDB.close();
}
}
}
除了表還包含「地址」和「電話」列名。這個數據我想在textviews「addressfill」,並與下面的XML另一活動的「telephonefill」需要填補:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<TextView
android:id="@+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/address"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Address" />
<TextView
android:id="@+id/addressfill"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/telephone"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Telephone number" />
<TextView
android:id="@+id/telephonefill"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
我想讀到很多的教程在那裏,很多帖子在這裏,但沒有得到周圍發現正確的答案。
我需要什麼代碼才能用於onListItemClick
以及我使用我提交的xml的活動。非常感謝幫助!
謝謝。那爲名字起作用。我怎麼能爲表格的其他列數據如地址和電話? – SunnySonic
謝謝。我去了你的第一個選項,並保存了我創建的另一個數組中的所有字符串。那完美的工作。 – SunnySonic