android - ListView 始终具有相同的 Intent

标签 android listview android-intent android-listview

我有一个从 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/

相关文章:

android - 三个按钮中只有一个起作用

android - Icepick 坏了吗?

android - 智能转换为 'Int' 是不可能的,因为 'mViewModel.counter.value' 是一个复杂的表达式

c# - 如何从 Xamarin Forms ListView 中删除项目?

java - 突出显示 ListView 选择

android - 为什么在调试时 intent.getExtras().mMap 为空?

android - 何时在 Android 中使用 Intent.ACTION_SEND 删除与外部应用共享的文件

java - 在android中从web服务加载图像时如何处理java.io.FileNotFoundException

Android Studio ListView 自定义适配器无数据显示

java - 取消注册 c2dm