0

我有一个包含项目的列表视图。根据单击了哪个 ListViewItem,我想打开一个 Activity 并将数据传递给它。但是如何获取单击了哪个 ListViewItem 的位置?

这是我的代码

public class tutorialActivity extends Activity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tutorial);
        registerClickCallBack();
        ListView listView = (ListView) findViewById(R.id.tutorialList);

        String tutorialTitle1 = getResources().getString(R.string.tutorial1_title);
        String tutorialTitle2 = getResources().getString(R.string.tutorial2_title);
        String tutorialTitle3 = getResources().getString(R.string.tutorial3_title);
        String tutorialTitle4 = getResources().getString(R.string.tutorial4_title);
        String tutorialTitle5 = getResources().getString(R.string.tutorial5_title);
        String tutorialTitle6 = getResources().getString(R.string.tutorial6_title);
        String tutorialTitle7 = getResources().getString(R.string.tutorial7_title);
        String tutorialTitle8 = getResources().getString(R.string.tutorial8_title);
        String tutorialTitle9 = getResources().getString(R.string.tutorial9_title);
        String tutorialTitle10 = getResources().getString(R.string.tutorial10_title);
        String tutorialTitle11 = getResources().getString(R.string.tutorial11_title);
        String tutorialTitle12 = getResources().getString(R.string.tutorial12_title);
        String tutorialTitle13 = getResources().getString(R.string.tutorial13_title);
        String tutorialTitle14 = getResources().getString(R.string.tutorial14_title);

        String[] values = new String[] { tutorialTitle1, tutorialTitle2, tutorialTitle3, tutorialTitle4, tutorialTitle5, tutorialTitle6, tutorialTitle7, tutorialTitle8, tutorialTitle9, tutorialTitle10, tutorialTitle11, tutorialTitle12, tutorialTitle13, tutorialTitle14};

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, values);
        listView.setAdapter(adapter);
    }

    private void registerClickCallBack() {
        ListView list = (ListView) findViewById(R.id.tutorialList);
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View viewClicked,int position, long id) {
            //How do I retrieve this position that has been clicked?
        }
    });
}
}
4

5 回答 5

2

在 onCreate 之外声明值

String[] values;

然后只需为其分配一个值:

values = new String[] { tutorialTitle1, ... };

然后在onItemClick获取字符串中使用values[position];

并根据该字符串开始活动。

于 2013-06-10T22:27:05.907 回答
1

使用 String selectedItem = (String) list.getSelectedItem(); 获取选定的字符串。您从方法 public void onItemClick(AdapterView parent, View viewClicked,int position, long id) 获得的单击项目的位置

于 2013-06-10T22:31:54.240 回答
1

使用 getCheckedItemPosition() 来确定所选项目的位置,假设您有choiceMode:single。

http://developer.android.com/reference/android/widget/AbsListView.html#getCheckedItemPosition()

要简单地获取在 onClick 方法中被点击的项目的位置,那已经为您准备好了。

private void registerClickCallBack() {
        ListView list = (ListView) findViewById(R.id.tutorialList);
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View viewClicked,int position, long id) {
            //How do I retrieve this position that has been clicked?
            int thePositionThatWasClicked = position;
        }
    });
于 2013-06-10T22:54:37.333 回答
0

不知道你在问什么,在你有位置的回调方法中。

于 2013-06-10T22:29:43.817 回答
0

容易的。参考这里:http: //developer.android.com/reference/android/widget/AdapterView.html#getSelectedItemPosition()

getSelectedItemPosition() 

:)

于 2013-06-10T22:38:52.880 回答