gpt4 book ai didi

android - 为 ListView 添加监听器

转载 作者:行者123 更新时间:2023-11-30 04:13:59 24 4
gpt4 key购买 nike

我已经从 SQLite 数据库创建了一个 ListView,但我一直在研究如何为每个 ListView 项目添加一个监听器,以便在单击一个项目时我可以显示另一个页面,其中包含有关该项目的更多信息。数据库只是一个示例。任何帮助将不胜感激。

public class Database extends ListActivity {

private final String SAMPLE_DB_NAME = "myFriendsDb";
//private final String SAMPLE_TABLE_NAME = "friends";

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);


ArrayList<String> results = new ArrayList<String>();
SQLiteDatabase db = null;

try {
db = this.openOrCreateDatabase(SAMPLE_DB_NAME, MODE_PRIVATE, null);

db.execSQL("CREATE TABLE IF NOT EXISTS people" +
" (LastName VARCHAR, FirstName VARCHAR," +
" Country VARCHAR, Age INT(3));");

db.execSQL("INSERT INTO people" +
" Values ('Jones','Bob','UK',30);");
db.execSQL("INSERT INTO people" +
" Values ('Smith','John','UK',40);");
db.execSQL("INSERT INTO people" +
" Values ('Thompson','James','UK',50);");

Cursor c = db.rawQuery("SELECT FirstName, LastName FROM people", null);

if (c != null ) {
if (c.moveToFirst()) {
do {
String firstName = c.getString(c.getColumnIndex("FirstName"));
String lastName = c.getString(c.getColumnIndex("LastName"));
results.add("" + firstName + " " + lastName);
}while (c.moveToNext());
}
}

this.setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,results));

} catch (SQLiteException se ) {
Log.e(getClass().getSimpleName(), "Could not create or Open the database");
} finally {
if (db != null)
db.execSQL("DELETE FROM people");
db.close();
}
}

最佳答案

有很多方法可以解决您的问题。一种可能的解决方案是:您只需要在 ListActivity 中实现 protected 方法 onListItemClick(ListView l, View v, int position, long id)。

public class Database extends ListActivity {

//YOUR CODE ABOVE HERE...

public static final String SHOWITEMINTENT_EXTRA_FETCHROWID = "fetchRow";
public static final int ACTIVITY_SHOWITEM = 0; /*Intent request user index*/

@Override
protected void onListItemClick(ListView l, View v, int position, long id){
/*
position variable holds the position of item you clicked...
do your stuff here. If you want to send to another page, say another activity
that shows your stuff, you can always use an intent
example:
*/
Intent tmpIntent = new Intent(this, YourActivityForShowingItem.class);
tmpIntent.putExtra(SHOWITEMINTENT_EXTRA_FETCHROWID, position);
startActivityForResult(tmpIntent, ACTIVITY_SHOWITEM);

}
}

或者,您可以使用 getListView() 访问 listActivity 的 ListView,并调用监听器或上下文菜单的 setter ,就像您对常规 ListView 对象所做的那样。例如,这个使用这种方法设置监听器的函数:

private void setMyListListener(){
getListView().setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> a, View v, int position, long id){
/*same fake code as above for calling another activity using an intent:*/
Intent tmpIntent = new Intent(this, YourActivityForShowingItem.class);
tmpIntent.putExtra(SHOWITEMINTENT_EXTRA_FETCHROWID, position);
startActivityForResult(tmpIntent, ACTIVITY_SHOWITEM);
}
});
}

如果您希望在整个 Activity 期间以相同的方式配置点击监听器,则可以在之后由您的 onCreate(...) 函数调用此函数。

关于android - 为 ListView 添加监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10359825/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com