0

应用读取 JSON 数据。然后它会将它放到列表视图中(正确),但是在按下一个项目后,我总是会得到相同的值。在我认为是问题但我找不到的代码下方。

try{
    JSONArray jArray = new JSONArray(result);
    for(int ii=0;ii<jArray.length();ii++){
            JSONObject json_data = jArray.getJSONObject(ii);
            courseName = json_data.getString("fullname");

            HashMap<String,String> map = new HashMap<String, String>();
            map.put("fullname", courseName);
            myCoursesList.add(map);
    }
} catch(JSONException e){
    Log.e("log_tag", "Error parsing data "+e.toString());
}
ListAdapter adapter = new SimpleAdapter(this, myCoursesList,R.layout.my_courses_layout,
        new String[] {"fullname"}, new int[] { R.id.course});

setListAdapter(adapter);
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener(){
    public void onItemClick(AdapterView<?> parent, View view, int position, long id){
        String fn = ((TextView)findViewById(R.id.course)).getText().toString();

        Intent in = new Intent(getApplicationContext(), courseActivity.class);
        in.putExtra("fullname", fn);
        startActivity(in);
    }
});
4

3 回答 3

1

我是一个普通的适配器/列表视图,我想你会想做这样的事情。

public void onItemClick(AdapterView<?> parent, View view, int position, long id){
     String fn = adapter.get(position);
     Intent in = new Intent(getApplicationContext(), courseActivity.class);
     in.putExtra("fullname", fn);
     startActivity(in);      
}
于 2012-04-15T11:52:58.243 回答
1

改变这个:

String fn = ((TextView)findViewById(R.id.course)).getText().toString();

对此:

String fn = ((TextView)view.findViewById(R.id.course)).getText().toString();
于 2012-04-15T11:48:42.290 回答
0

尝试

String fn = ((TextView)view.findViewById(R.id.course)).getText().toString();

代替

String fn = ((TextView)findViewById(R.id.course)).getText().toString();
于 2012-04-15T11:49:00.470 回答