检索单击的ListViewItem的位置

时间:2013-06-10 22:23:40

标签: android android-listview listviewitem

我有一个包含项目的列表视图。根据单击的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?
        }
    });
}
}

5 个答案:

答案 0 :(得分:2)

声明onCreate

之外的值
String[] values;

然后只为其分配一个值:

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

然后在onItemClick中使用values[position];

获取字符串

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

答案 1 :(得分:1)

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

答案 2 :(得分: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;
        }
    });

答案 3 :(得分:0)

不确定你要求的是什么,在你有位置的回调方法中。

答案 4 :(得分:0)