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

1 回答 1

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

      }                 
});

希望这有帮助。

于 2013-01-23T00:27:25.000 回答