我有ListView
个项目。我想设置一个onClickListener
来显示有关所点击项目的详细信息。如何确定在onClickListener
中点击了哪个项目? View
或v
有许多方法可供我查看,并且看不到任何与点击对象相关的内容。
//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...
}
});
答案 0 :(得分: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));
}
});
希望这会有所帮助。