【问题标题】:ListView always has the same intentListView 始终具有相同的意图
【发布时间】:2015-05-13 05:38:32
【问题描述】:

我有一个从 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);
                                }
                            });

                        }
                    });
                }
        }

【问题讨论】:

  • 你能看到列表中的更新值吗???
  • 是的,它会更新 UI,但所有列表项都具有相同的意图数据
  • 好吧,您将提供一个最终成员字段作为额外的。你想做什么?

标签: android listview android-intent android-listview


【解决方案1】:

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

{

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

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

startActivity(i);

}

【讨论】:

    【解决方案2】:
    just replace by this code onitemclick            
           lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                                      @Override
    
           public void onItemClick(AdapterView<?>parent, View view, int position, long id) {
    
            String value = (new ArrayList<String>(ListEntry.values())).get(position);
                Intent intent = new Intent(getApplicationContext(), CourseActivity.class);
                                                intent.putExtra("Data", value);
                                                startActivity(intent);
                                            }
                                        });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-26
      • 2018-08-07
      • 2018-06-26
      • 2013-01-08
      • 1970-01-01
      • 1970-01-01
      • 2022-01-06
      • 1970-01-01
      相关资源
      最近更新 更多