简体   繁体   English

如何在ListView onItemClickListener中获取所选项目的位置

[英]How to get the position of a selected item in ListView, onItemClickListener

I'm trying to get the position of a selected item like this: 我正在尝试获取所选项目的位置,如下所示:

//more code (adapter settings etc)
List.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                    String selected_item= String.valueOf(adapterView.getItemAtPosition(i));
                    position=(Integer)List.getTag(); //without this line it doesn't crash
                    Toast.makeText(Main2Activity.this,"Please Log-In"+selected_item+"Thesi :"+String.valueOf(position),Toast.LENGTH_SHORT).show();
                    Intent toy = new Intent(Main2Activity.this,Main3Activity.class);
                    startActivity(toy);
                }
            });

It results on crashing my app. 结果导致我的应用崩溃

Goal: i would like to have in a public variable ( position ) the position of a selected item 目标:我希望在公共变量( 位置 )中具有选定项目的位置

Logcat: Logcat:

12-03 18:16:31.858 4421-4421/gr.aegean.icsd.myapplication W/art: Before Android 4.1, method int android.support.v7.widget.ListViewCompat.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView

Let me mention that i'm new with android studio. 让我提一下,我是android studio的新手。

I guess you have a ListView which you want to add listener to. 我想你有一个ListView要添加侦听器。 In that case, in onItemClick(AdapterView adapterView, View view, int i, long l) method, third parameter int i holds the position. 在这种情况下,在onItemClick(AdapterView adapterView,View view,int i,long l)方法中,第三个参数int i保持位置。

Edit: If you want to use int i in another activity, the easiest way is to declare a static class variable in the same activity where the setOnItemclick is. 编辑:如果要在另一个活动中使用int i,最简单的方法是在setOnItemclick所在的同一活动中声明一个静态类变量。 For example: 例如:

class MainActivity extends Activity{
    // variable accessible from anywhere in your package
    static int global_int;

    // other usual code...

    // your existing code
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                String selected_item= String.valueOf(adapterView.getItemAtPosition(i));
                position=(Integer)List.getTag(); //without this line it doesn't crash
                Toast.makeText(Main2Activity.this,"Please Log-In"+selected_item+"Thesi :"+String.valueOf(position),Toast.LENGTH_SHORT).show();
                Intent toy = new Intent(Main2Activity.this,Main3Activity.class);
                startActivity(toy);

                // saving "i"
                global_int=i;
            }
        });

}

Then from any other place in your code, other activity, etc. you access it like this: 然后从代码中的任何其他位置,其他活动等进行访问,如下所示:

int get_global_int=MainActivity.global_int;

There are other ways like saving it in SharedPreferences, but this is the easiest one. 还有其他方法,例如将其保存在SharedPreferences中,但这是最简单的方法。

try this it will give you the selected item value and its position. 尝试此操作,它将为您提供选定的项目值及其位置。

listview.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
    @Override
    public void onItemClick(AdapterView<?> arg0, View view, int position, long arg3)
    {
        String selectedFromList =(String) (listview.getItemAtPosition(position).toString());
        Log.d("Selected: ",  selectedFromList );


       int selectedItemPosition = listview.getItemAtPosition(position));

        Log.d("Selected item position :- ", selectedItemPosition);

    }
});

and YOU CAN STORE THIS ITEM POSITION in an int variable , see above modified code , i stored the selected item position in an int variable selectedItemPosition. 并且您可以将此项目位置存储在int变量中,请参阅上面的修改代码,我将所选项目的位置存储在int变量selectedItemPosition中。 you can use this selectedItemPosition wherever you want. 您可以随时随地使用此selectedItemPosition

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM