ListView始终具有相同的意图

时间:2015-03-11 12:20:05

标签: android listview android-intent android-listview

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

这是填充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);
                                }
                            });

                        }
                    });
                }
        }

2 个答案:

答案 0 :(得分:1)

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

{

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

i.putExtra( “数据”,list.get(位置)的ToString());

startActivity(ⅰ);

}

答案 1 :(得分:0)

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);
                                        }
                                    });