gpt4 book ai didi

android - ListView 始终具有相同的 Intent

转载 作者:行者123 更新时间:2023-11-29 01:34:49 25 4
gpt4 key购买 nike

我有一个从 ASyncTask 更新的 ListView,适配器会正确更新列表,但是我的 setOnItemClickListener 始终具有相同的数据。

这是填充 ListView 的循环代码

        if (JsonStr != null) {
//Get list of courses
JSONObject json = new JSONObject(JsonStr);
JSONObject Username = json.getJSONObject(InputUsername);
JSONObject Courses = Username.getJSONObject("Courses");
JSONObject CoursesItems = Courses.getJSONObject("items");

//Iterate around all courses
Iterator<String> i = CoursesItems.keys();

while (i.hasNext()) {
final String key = i.next();
JSONObject Course = (JSONObject) CoursesItems.get(key);

//Create a course entry
Map<String, String> ListEntry = new HashMap<>(2);

ListEntry.put("code", key);

if (Course.has("m_name")) {
String CourseName = (String) Course.get("m_name");
ListEntry.put("title", CourseName);
}

if (Course.has("original_source")) {
String Source = (String) Course.get("original_source");
ListEntry.put("source", Source);
}

JSONObject Units = null;
if (Course.has("Units")) {
Units = Course.getJSONObject("Units");
ListEntry.put("Units", Units.toString());
}


ListEntries.add(ListEntry);

final JSONObject finalUnits = Units;
runOnUiThread(new Runnable() {
public void run() {
SimpleAdapter adapter = new SimpleAdapter(
getApplicationContext(), //Activity
ListEntries, //List of pairs
R.layout.mymodules__list_item, //Target Parent Layout
new String[]{"title", "code", "source"}, //Key for pairs
new int[]{R.id.list_item_alert_textview,
R.id.list_item_date_textview,
R.id.list_item_source_textview}); //target items

ListView lv = (ListView) findViewById(R.id.listview);
lv.setAdapter(adapter);

lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(getApplicationContext(), CourseActivity.class);
intent.putExtra("Data", finalUnits.toString());
startActivity(intent);
}
});

}
});
}
}

最佳答案

'finalUnits' 是最终值。将监听器和数据(列表单元)移动到内部适配器。并在 bindview 方法中设置监听器。

{

Intent i = new Intent(context,CourseActivity.class);

i.putExtra("数据",list.get(位置).toString());

启动 Activity (i);

关于android - ListView 始终具有相同的 Intent ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28986491/

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