Get listview item position on button click
Asked Answered
F

5

36

This is my listview click event:

 lv1.setOnItemClickListener(new OnItemClickListener() {
       @Override
            public void onItemClick(AdapterView<?> a, View v, int position, long id) { 
                Object o = lv1.getItemAtPosition(position);
                ItemDetails obj_itemDetails = (ItemDetails)o;
                Toast.makeText(ListViewImagesActivity.this, "You have chosen : " + " " + obj_itemDetails.getName(), Toast.LENGTH_LONG).show();

        }  
 });

And this is my button click event:

btnNxt = (Button) findViewById(R.id.btnNext);
 btnNxt.setOnClickListener(new OnClickListener() {
 @Override
 public void onClick(View arg0) {
  //Here I need to get that position
});

Now I need to get the position of the clicked listview item..I have declared postion globally but that always gives me position 1.

Can anyone say me how do I get the positon ?

Fairground answered 12/12, 2013 at 11:6 Comment(5)
Where have you declared the position globally?Diagenesis
@RGraham-Before the onCreate methodFairground
Have you declared position=1 globally like this?Terris
@piyush- I have set it as jst int and havent specified any valueFairground
@downvoter-Care to comment.Simply downvoting dosent makes senseFairground
P
74

do you execute this

    btnNxt = (Button) findViewById(R.id.btnNext);
 btnNxt.setOnClickListener(new OnClickListener() {
 @Override
 public void onClick(View arg0) {
  //Here I need to get that position
});

inside the getView method? if so it's very easy

     btnNxt = (Button) findViewById(R.id.btnNext);
     btnNxt.setTag(position);
     btnNxt.setOnClickListener(new OnClickListener() {
     @Override
     public void onClick(View arg0) {
      int position=(Integer)arg0.getTag();
    });
Perch answered 12/12, 2013 at 11:17 Comment(3)
Hello sir, Please tell me how can i get the position of particular element without clicking on that element just basis of element name .Slipknot
@NikhilSingh you should open a question on your own, or use the search function. Anyway, the method public View getView(int position, View convertView, ViewGroup parent) of Adapter class already containts a parameter indicating the item position.Palpebrate
Using tag is a bad idea. For instance, you can't call setTag on an ImageView if you're loading with Glide.Ison
W
33

I know I'm bit late with the answer, but what about this:

private OnClickListener myButtonClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
    View parentRow = (View) v.getParent();
    ListView listView = (ListView) parentRow.getParent();
    final int position = listView.getPositionForView(parentRow);
  }
};

in this case you don't need to set tags and so on.

Welker answered 30/7, 2015 at 15:9 Comment(7)
hii, I want to know the position of particular element in list view without clicking on that element on the basis of element.Slipknot
OMG, you are a life saver. Tomorrow I have a deadline for my app at work, and just today I realised, I wasn't getting the right position and spent whole day. Thank you)Kozhikode
Thank you @PromotionArtwork :) , and what about me ? :DWelker
This helped me a lot. I were using the TAGs for something else and wasn't able to find good solution.Jackpot
After 3 hours of trying you helped :D ThanksDecorate
This is also in Swift (iOS) the best way to get a button's click in a ListViewPriggish
Not certain, but this might be incorrect if you have added a header view to the listRoxy
J
7

If you are using ViewHolder make sure you use setTag() like this.

    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;


        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mLayoutInflater.inflate(R.layout.custom_row, parent, false);
            holder.btnNxt = (Button) convertView.findViewById(R.id.btnNext);
            holder.btnNxt.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    int position=(Integer)arg0.getTag();
                });
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
          holder.btnNxt.setTag(position);   // <---- setTag() here 

            return convertView;
        }

        private class ViewHolder {
            Button btnNxt;
        }
    }

If you setTag() before your setOnClickListener() line, you will have problem when you scroll your listview.

Jayme answered 1/5, 2015 at 2:11 Comment(0)
B
0

// use "top" to get listview items top position

// Listview items top position

    int top;
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View lView, final int pos, long id) {
            top = lView.getTop();
            if (top < 1) top = 1;
        }
    });

// Or // Listview items top position Under CustomAdapter's getView method

    int top;
    final View finalConvertView = convertView;
    convertView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            top = finalConvertView.getTop();

            if (top < 1) top = 1;

        }
    });
Bony answered 12/12, 2013 at 11:7 Comment(0)
A
0

declare a global variable and in onItemClick() method of lisview set the view position as variable's value. now use same variable in click method of button.

int global_position =0; // your global variable 

 lv1.setOnItemClickListener(new OnItemClickListener() {
       @Override
            public void onItemClick(AdapterView<?> a, View v, int position, long id) { 
               global_position =position;
        }  
 });

btnNxt = (Button) findViewById(R.id.btnNext);
 btnNxt.setOnClickListener(new OnClickListener() {
 @Override
 public void onClick(View arg0) {
    Toast.makeText(<context>,""+global_position,500).show();
});
Astrobiology answered 12/12, 2013 at 11:16 Comment(3)
it will toast only last clicked item position !!Astrobiology
did you tried it? does it works? i want to use this approach because i don't want to put the position on view tagAggappera
Can any body tell how can i find the position of particular element without clicking on that element, on the basis of element name .Slipknot

© 2022 - 2024 — McMap. All rights reserved.