在列表视图中获取所选项目

时间:2012-10-03 12:52:13

标签: android android-listview

我在视图翻录器中使用了一个列表视图,我希望当用户选择一个列表项时,它应该在用户选择其他列表项时保持选中状态或返回到“Listview”活动。如果可能的话? / p>

2 个答案:

答案 0 :(得分:2)

list.setOnItemClickListener(new OnItemClickListener() {

       public void onItemClick(AdapterView<?> arg0,
                    View arg1, int position, long arg3)
        {
            String selectedItem = list.getSelectedItem().toString();    
        });

此处,selectedItem包含您在列表中选择的内容。

答案 1 :(得分:1)

试试这个

simpleAdapter = new SimpleAdapter(this, docList, R.layout.your_layout,
            new String[] { "UserName", "Status"}, new int[] {
                    R.id.username, R.id.status});
    listView.setAdapter(simpleAdapter);
    listView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {

            registerForContextMenu(listView);
            listView.showContextMenu();

            tv = (TextView) arg1.findViewById(R.id.username);
            getListName = tv.getText().toString();               

            tv1 = (TextView) arg1.findViewById(R.id.status);
            getFriendUserId = tv1.getText().toString();  
        }
    });