gpt4 book ai didi

android - 在 Android 中如何将 ListAdapter 值传递给 onItemClick?

转载 作者:行者123 更新时间:2023-11-29 00:36:07 25 4
gpt4 key购买 nike

我的应用程序上有一个正常运行的搜索界面。现在,我正在尝试使每个搜索结果列表项都可点击并显示在新 Activity 中。我让它们可以点击,但我不知道如何将 ListAdapter 值(记录)传递到 onItemClick 方法中。

到目前为止,这是我的代码:

private void showResults(String query) {
Cursor cursor = DBHelper.searchDB(query);
startManagingCursor(cursor);
String[] searchFrom = new String[] { DBAdapter.KEY_MAKE,
DBAdapter.KEY_YEAR, DBAdapter.KEY_MAKE,
DBAdapter.KEY_MODEL };
int[] displayHere = new int[] { R.id.rDateTV, R.id.rYearTV,
R.id.rMakeTV, R.id.rModelTV };

final SimpleCursorAdapter records = new SimpleCursorAdapter(this,
R.layout.record_2, cursor, searchFrom, displayHere);

setListAdapter(records);
DBHelper.close();

// --- Click on list item ---


ListView clickList = getListView();
clickList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//--- here's the problem: how do I pass the ListAdapter values into onItemClick. the below doesn't like .getText()
String sYear = (view.findViewById(R.id.rYearTV)).getText().toString();
String sMake = (view.findViewById(R.id.rMakeTV)).getText().toString();
String sModel = (view.findViewById(R.id.rModelTV)).getText().toString();

// -- then pass these strings to an intent to launch a new activity




}
});


// --- END click on list item ----

}

有什么想法吗?我是这里的菜鸟,所以如果你能给我看代码,那就太棒了!

最佳答案

OnItemClickListener.onItemClick 中调用 parent.getItemAtPosition(position)。该方法返回设置为给定位置的光标。然后从该游标获取数据。不要在 OnItemClickListener.onItemClick 内关闭光标。

例如:

ListView clickList = getListView(); 
clickList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Cursor c = (Cursor)parent.getItemAtPosition(position)
String sMake = c.getString(c.getColumnIndexOrThrow(DBAdapter.KEY_MAKE));//get data from cursor

...
}
});

关于android - 在 Android 中如何将 ListAdapter 值传递给 onItemClick?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12590339/

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