我如何知道点击了哪个列表项?

时间:2013-01-23 00:23:35

标签: java android

我有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 个答案:

答案 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));

      }                 
});

希望这会有所帮助。