Get listview item position on button click

前端 未结 5 1811
囚心锁ツ
囚心锁ツ 2020-11-27 05:07

This is my listview click event:

 lv1.setOnItemClickListener(new OnItemClickListener() {
       @Override
            public void onItemClick(AdapterView<         


        
相关标签:
5条回答
  • 2020-11-27 05:41

    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.

    0 讨论(0)
  • 2020-11-27 05:51

    // 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;
    
            }
        });
    
    0 讨论(0)
  • 2020-11-27 05:56

    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();
        });
    
    0 讨论(0)
  • 2020-11-27 05:59

    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();
    });
    
    0 讨论(0)
  • 2020-11-27 06:05

    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.

    0 讨论(0)
提交回复
热议问题