2013-01-23 92 views
-1

我有一個裝滿物品的ListView。我想設置一個onClickListener以顯示有關點擊項目的詳細信息。我如何確定在onClickListener內點擊了哪個項目? Viewv有很多可用的方法,但我沒有看到任何與點擊對象有關的方法。如何知道哪個列表項被點擊?

//populate the activity list 
    ListView teamsListView = (ListView) activity.findViewById(R.id.teamsListView); 
    ArrayList<HashMap<String, String>> listData = new ArrayList<HashMap<String, String>>(); 
    HashMap<String, String> listItem; 
    for (TeamSet teamSet : response.getTeamSetList()) { 
     listItem = new HashMap<String, String>(); 
      listItem.put("name", teamSet.getName()); 
      //listItem.put("teamCount", Integer.toString(teamSet.getTeams().size())); 

     listData.add(listItem); 
    } 
    teamsListView.setAdapter(
      new SimpleAdapter(
        context, 
        listData, 
        R.layout.teams_list_row, 
        new String[]{"name"}, 
        new int[]{R.id.teamsTeamListName} 
      ) 
    ); 

    //show details on a team 
    TextView team = (TextView) activity.findViewById(R.id.teamsTeamListName); 
    team.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      //how do I know which item was clicked? I want to load more details on the item... 
     } 
    }); 
+1

[OnItemClickListener](http://developer.android.com/reference/android/widget/AdapterView.OnItemClickListener.html)? – keyser

+0

我喜歡我如何得到2票反對提出問題...在這個網站上的一些人...... – Webnet

+0

我認爲這是因爲你沒有顯示任何研究工作 – keyser

回答

4

您使用OnItemClickListener不是onCLickListener

你做這樣的事情:

final ListView lv = (ListView) findViewById(R.id.ListView01); 

lv.setOnItemClickListener(new OnItemClickListener() { 
     public void onItemClick(AdapterView<?> myAdapter, View myView, int myItemInt, long mylng) { 
     String selectedFromList =(String) (lv.getItemAtPosition(myItemInt)); 

     }     
}); 

希望這有助於。

相關問題