データベースの名前からリストビューを埋めています。それはすべて正常に機能しています。名前のリストアイテムをクリックした後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();
}
}
}
名前の他に、テーブルには「address」列と「telephone」列も含まれています。このデータは、次のxmlを使用して別のアクティビティのテキストビュー「addressfill」および「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を使用するアクティビティで、およびでどのコードを使用する必要がありますか。ヘルプは非常にありがたいです!