how to get a list item position by clicking the button inside it?

前端 未结 8 622
余生分开走
余生分开走 2020-12-09 16:48

actually I\'ve read some previous questions about this...

this is the code that I use

auto = (ListView)findViewById(R.id.auto);
String[] projection =         


        
8条回答
  •  执念已碎
    2020-12-09 17:45

    Very Simple. In your Adapter class getView method change int position to final int position.

    public View getView(final int position, View convertView, ViewGroup parent) {
    
    LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View row = inflater.inflate(yourcustomizeditemview.xml, parent,
                    false);
    Button btn= (Button) row.findViewById(R.id.btn);
    btn.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
    
                    Toast.makeText(context, " "+position, Toast.LENGTH_SHORT).show();
    
                }
            });
    return row;
        }
    

    now on Button click Toast shows the position of item in which Button is present.

提交回复
热议问题