1

I have a listView, I'm wondering how to get the position of the item selected in the ListView so that I can use this code under the OnItemClick method.

string selected = listView.getItemPosition().toString()

So then I can use the if / else clause or switch to say:

if (selected.positionEquals("0")) {
        Intent i = new Intent(this, WebViewActivity.class);
        i.putExtra("keyHTML", "file:///android_asset/page1.html");
        startActivity(i);

I hope I made sense, please note, the listView.getItemPosition().toString() and selected.positionEquals was something I made up so you can get an idea of what I want.

Thanks!

EDIT

I just saw this from another question, I searched before asking this, but this just popped out. Do you think it will work in my case?

listView.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view,
        int position, long id) {

if(position == 1)
{

     Intent myIntent = new Intent(YourActivity.this, SecondActivity.class);
         startActivityForResult(myIntent, 0);
}

if(position == 2)
{

     Intent myIntent =  new Intent(YourActivity.this, ThirdActivity.class);
         startActivityForResult(myIntent, 0);
}
    }
  });

5 Answers5

3

in one of the parameter of onItemClick there is a position value (third argument) of the clicked item. you dont need to get it progromatically, just use that value to know

public void onItemClick(AdapterView<?> arg0, View v, int position, long arg3) { //check third argument it automatically get the selected item POSITION
    // TODO Auto-generated method stub
    Intent i;
    switch (position)
    {
    case 0: // first one of the list
        i = new Intent(this, anActivity.class);
        startActivity(i);
        break;
    case 1: // second one of the list.
        i = new Intent(this, anActivity2.class);
        startActivity(i);
        break;
       // and so on...
    }

}
Coderji
  • 7,655
  • 5
  • 37
  • 51
  • glad I helped you out, If you found it useful please, tick it as righ :) – Coderji Nov 18 '13 at 07:19
  • @Coderji I applied an adapter filter to my ListView, I found out that when the the ListView is filtered and the the right text is brought to the first position, when it is clicked on, it opens the activity for the first position, whereas the filtered is text had a position of 12. What do you think? – Oluleye IResþekt Idowu Nov 18 '13 at 14:42
  • Maybe you have missed something in the `getView` or `getItem` functions please refer to this [question](http://stackoverflow.com/questions/14562556/filtered-listview-onitemclick-returns-item-at-original-position) it has your same issue, hope it has your same solution. – Coderji Nov 18 '13 at 15:18
  • @Coderji Could you please be so gentle / helpful as to help me check it out here, thanks http://stackoverflow.com/questions/20052214/listview-positioning-after-filtering – Oluleye IResþekt Idowu Nov 18 '13 at 16:53
  • Ill do my best to help you. – Coderji Nov 18 '13 at 17:37
  • @Coderji Hello, I have been off the past two days, been busy with something else while I check the tutorial you told me to look out for. I have created the listView with a custom adapter, but now I am confused on how to set the onItemClick to it. Here = http://stackoverflow.com/questions/20127922/set-onitemclick-to-custom-adapter-listview – Oluleye IResþekt Idowu Nov 21 '13 at 17:30
  • hey I have implemented the filter and the text watcher and it worked I had to change couple of thing in your code, but using my data It worked. if you want I can send you the application to check it out you can email me. get into my profile here you will find my email. Ill go off to sleep now – Coderji Nov 21 '13 at 19:07
0

Try this code:

    listview.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                long arg3) {
            // TODO Auto-generated method stub

        }
    });

here "int position" will gives the selected item index value.

android_dev
  • 1,477
  • 12
  • 18
0

please see the code below :-

listView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int index,
                    long arg3) {
                if (index == 0)) {
        Intent i = new Intent(this, WebViewActivity.class);
        i.putExtra("keyHTML", "file:///android_asset/page1.html");
        startActivity(i);
            }
        });
Rahul Gupta
  • 5,275
  • 8
  • 35
  • 66
  • Very useful.. Used this...it prevents me having to stress cuz I have the if statements I want in another activity, I just copy paste and replace, just need to fix in the numbers, thanks. – Oluleye IResþekt Idowu Nov 18 '13 at 10:32
0

Using Below code Toast has print the position of selected item :-

listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(getApplicationContext(),"Position of Selected Item is :-"+position,Toast.LENGTH_LONG).show(); 
}
});
Mitul Gedeeya
  • 886
  • 1
  • 10
  • 20
0

yes working for you this way

    listView.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    if(position == 1)
    {

         Intent myIntent = new Intent(YourActivity.this, SecondActivity.class);
         startActivityForResult(myIntent, position);
    }

    if(position == 2)
    {

         Intent myIntent =  new Intent(YourActivity.this, ThirdActivity.class);
             startActivityForResult(myIntent, position);
    }

 }    
 });

Also switch to will work for you, and if you want compare using String like

string selected = listView.getItemPosition().toString()

then do like

 if (selected.equalsIgnoreCase("0")){

  // do work here 

   }else if(selected.equalsIgnoreCase("1")){

      // do work here 
  }

end so on.

Ankitkumar Makwana
  • 3,475
  • 3
  • 19
  • 45