1

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:

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.

Mr.Nois
  • 45
  • 3
  • 14

2 Answers2

1

I guess you have a ListView which you want to add listener to. In that case, in onItemClick(AdapterView adapterView, View view, int i, long l) method, third parameter int i holds the position.

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. 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.

colens
  • 467
  • 3
  • 12
1

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. you can use this selectedItemPosition wherever you want.

Sachin Rajput
  • 4,326
  • 2
  • 18
  • 29
  • i get the item when i click on it on the log cat, i want to store the viriable position and try to use it in a different activity – Mr.Nois Dec 05 '17 at 21:23
  • check the modified code , it works , hope it solve your problem and get your upvote. – Sachin Rajput Dec 06 '17 at 04:40
  • glad it works for you Phill, yes you need to declare your item global if you want to use that outside the scope of this function – Sachin Rajput Dec 11 '17 at 06:27