Android ListView始终具有相同的意图

Android ListView始终具有相同的意图,android,listview,android-intent,android-listview,Android,Listview,Android Intent,Android Listview,我有一个从ASyncTask更新的ListView,适配器正确地更新列表,但是我的setOnItemClickListener在intent中始终具有相同的数据 这是填充ListView的循环的代码 if (JsonStr != null) { //Get list of courses JSONObject json = new JSONObject(JsonStr); JSONOb

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

                        }
                    });
                }
        }
if(JsonStr!=null){
//获取课程列表
JSONObject json=新的JSONObject(JsonStr);
JSONObject用户名=json.getJSONObject(InputUsername);
JSONObject课程=Username.getJSONObject(“课程”);
JSONObject CoursesItems=Courses.getJSONObject(“项目”);
//迭代所有课程
迭代器i=courseItems.keys();
while(i.hasNext()){
最终字符串键=i.next();
JSONObject Course=(JSONObject)courseItems.get(key);
//创建课程条目
Map ListEntry=新的HashMap(2);
输入(“代码”,键);
如果(课程名称(“m_name”)){
String CourseName=(String)Course.get(“m_name”);
ListEntry.put(“标题”,CourseName);
}
如果(当然有(“原始来源”)){
字符串源=(字符串)Course.get(“原始_源”);
ListEntry.put(“源”,source);
}
JSONObject Units=null;
如果(课程有(“单元”)){
单位=Course.getJSONObject(“单位”);
put(“Units”,Units.toString());
}
添加(ListEntry);
最终JSONObject finalUnits=单位;
runOnUiThread(新的Runnable(){
公开募捐{
SimpleAdapter适配器=新SimpleAdapter(
getApplicationContext(),//活动
ListEntries,//对的列表
R.layout.mymodules\u\u列表\u项,//目标父布局
新字符串[]{“title”,“code”,“source”},//成对的键
新建int[]{R.id.list_item_alert_textview,
R.id.list\u item\u date\u textview,
R.id.list_item_source_textview});//目标项
ListView lv=(ListView)findViewById(R.id.ListView);
低压设置适配器(适配器);
lv.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
Intent Intent=新的Intent(getApplicationContext(),CourseActivity.class);
intent.putExtra(“数据”,finalUnits.toString());
星触觉(意向);
}
});
}
});
}
}

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

{

意图i=新意图(上下文、CourseActivity.class)

i、 putExtra(“数据”,list.get(position.toString())

星触觉(i)

}

只需单击此代码即可替换
lv.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
public void onItemClick(AdapterViewparent、View、int位置、长id){
字符串值=(新的ArrayList(ListEntry.values()).get(位置);
Intent Intent=新的Intent(getApplicationContext(),CourseActivity.class);
意向。额外(“数据”,值);
星触觉(意向);
}
});

您能在列表中看到更新后的值吗???是的,它会更新UI,但所有列表项都具有相同的意图数据。好的,您提供了一个最终成员字段作为额外字段。你想干什么?
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);
                                        }
                                    });