我正在用数据库名称填充 ListView 。这一切都很好。单击具有名称的 Listitem 后,我想设置 onListItemClick
以在另一个 Activity 中填写 TextView 。这就是我被困的地方。
这是我正在运行的 ListView Activity 的代码:
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();
}
}
}
除了名称之外,该表还包含“地址”和“电话”列。我想使用以下 xml 将此数据填充到另一个 Activity 的 TextView “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 的 Activity 中,我必须使用什么代码。非常感谢您的帮助!
最佳答案
如果你想在列表项上设置监听器,你需要扩展数组适配器并将监听器代码放在适配器中:
public class SettingsListAdapter extends ArrayAdapter<String> {
@Override
public View getView(int position, View convertView, ViewGroup parent){
// put your listener in here
mybutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// do work here
}
}
}
换句话说,simplelistadapter 不足以做你想做的事,你需要创建自己的。
编辑
这是创建您自己的自定义 ArrayAdapter 的一个很好的示例:Custom Array Adapter Tutorial
关于android - 由带有 onListItemClick 的 sql db 填充的 ListView 以填充其他 Activity 中的 TextView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12770206/