2012-04-15 87 views
0

应用程序读取JSON数据。然后它将它列入清单视图(正确),但在按下一个项目后,我会始终得到相同的值显示。在我认为是问题的代码下方,但找不到一个。android发送intent额外

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

回答

1

更改此:

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

要这样:

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

感谢它的工作(我会在7分钟内接受答案)也许你可以解释它有什么区别? – LTnewbie 2012-04-15 11:52:03

+0

单击某个项目时,其视图将返回为** onItemClick **中的参数,因此您需要使用该视图执行findViewById以获取选定行的TextView – waqaslam 2012-04-15 11:53:59

0

尝试

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

,而不是

String fn = ((TextView)findViewById(R.id.course)).getText().toString(); 
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);  
}