简体   繁体   中英

Custom ArrayAdapter, listView, button

I try to capture the click of the button in MainActivity but this didn't work with me. How to do it. I put toast in my getView it seems to work, but how to catch the click in MainActivity.

@Override
public View getView(final int position, View convertView, ViewGroup parent){


    LayoutInflater inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);


    rowView = inflater.inflate(R.layout.gestionmaps, parent, false);


    textView   = (TextView) rowView.findViewById(R.id.textView02);
    btn2       = (Button) rowView.findViewById(R.id.spinner02);
    btn        = (Button) rowView.findViewById(R.id.del02);

    textView.setText(values.get(position));

    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {




            Toast.makeText(context, "btn "+position, Toast.LENGTH_SHORT).show();

        }
    });

    btn2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Toast.makeText(context, "btn0 "+position, Toast.LENGTH_SHORT).show();

        }
    });

return rowView;

}

Your code is not working because when you click button1 and button2 , the clicks are consumed by List Item

Solution :

@Override
public View getView(final int position, View convertView, ViewGroup parent){


    LayoutInflater inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);


    rowView = inflater.inflate(R.layout.gestionmaps, parent, false);


    textView   = (TextView) rowView.findViewById(R.id.textView02);
    btn2       = (Button) rowView.findViewById(R.id.spinner02);
    btn        = (Button) rowView.findViewById(R.id.del02);

    textView.setText(values.get(position));

    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {




            Toast.makeText(context, "btn "+position, Toast.LENGTH_SHORT).show();

        }
    });

    btn2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Toast.makeText(context, "btn0 "+position, Toast.LENGTH_SHORT).show();

        }
    });
rootView.setOnClickListener(new OnClickListener() {

       @Override
      public void onClick(View view) {
       // put logs here to check if it is executed when you click outside button area
      }

    });
return rowView;

}

In order to have onClick of your button in YourActivity add this android:onClick line to your Button xml file.

list_item.xml

<Button
    android:onClick="onClickButtonOne"
    .../>

Activity.java

public void onClickButtonOne(View v) {
    // button one onclick here.
    int position = listview.getPositionForView(v);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM