简体   繁体   中英

Button click handling in Android ListView

Help with the processing of clicks on the buttons that are sets in the ListView through the adapter. ListAdapter adaptiruy = new SimpleAdapter(this, pizzaNames, R.layout.adapter_backet, from, to); listView.setAdapter(adaptiruy);

enter image description here

here is the adapter code:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:background="@drawable/for_bucket_adapter"
    android:gravity="center"
    android:layout_marginTop="8sp">

    <TextView
        android:id="@+id/textok1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="22sp"
        android:textColor="@color/white"
        android:layout_gravity="center"
        android:gravity="center"/>

    <TextView
        android:id="@+id/textok2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/white"
        android:textSize="22sp"/>

    <ImageButton
        android:layout_gravity="center"
        android:layout_width="22sp"
        android:layout_height="22sp"
        android:layout_marginStart="5sp"
        android:background="@drawable/plusss"/>

    <Button
        android:id="@+id/minus"
        android:layout_gravity="center"
        android:layout_width="22sp"
        android:layout_height="5sp"
        android:layout_marginStart="5sp"
        android:background="@drawable/minuss"
        android:onClick="Click"/>

</LinearLayout>

and activity code:

public class BasketPage extends Activity {
        List<Map<String, String>> pizzaNames = new ArrayList<>();

        public String name;
        public String phoneNumber;
        public String street;
        public String home;
        public String porch;
        public String level;
        public String apprt;
        public String comment;
        public boolean b;
        //private Button b = (Button)findViewById(R.id.minus);


        WorkBD workBD;
        MainActivity mainActivity;
        public ListView listView;

        public int [] PizzaMass = new int [5];
        int indexhelper = -1;



        public void creator() {
            System.out.println(MainActivity.pepperoni.getCount());
            if (MainActivity.pepperoni.getCount() != 0) {
                Map<String, String> map = new HashMap<>();
                map.put("text1", "Пепперони ");
                map.put("text2", MainActivity.pepperoni.getCount() + " шт.");
                pizzaNames.add(map);
                PizzaMass [0] = ++indexhelper;
            }
            if (MainActivity.calzone.getCount() != 0) {
                Map<String, String> map = new HashMap<>();
                map.put("text1", "Кальцоне ");
                map.put("text2", MainActivity.calzone.getCount() + " шт.");
                pizzaNames.add(map);
                PizzaMass [1] = ++indexhelper;
            }
            if (MainActivity.quattrostagioni.getCount() != 0) {
                Map<String, String> map = new HashMap<>();
                map.put("text1", "Четыре сезона ");
                map.put("text2", MainActivity.quattrostagioni.getCount() + " шт.");
                pizzaNames.add(map);
                PizzaMass [2] = ++indexhelper;
            }
            if (MainActivity.quattroformaggi.getCount() != 0) {
                Map<String, String> map = new HashMap<>();
                map.put("text1", "Четыре сыра ");
                map.put("text2", MainActivity.quattroformaggi.getCount() + " шт.");
                pizzaNames.add(map);
                PizzaMass [3] = ++indexhelper;
            }

            if (MainActivity.mexican.getCount() != 0) {
                Map<String, String> map = new HashMap<>();
                map.put("text1", "Мексиканская ");
                map.put("text2", MainActivity.mexican.getCount() + " шт.");
                pizzaNames.add(map);
                PizzaMass [4] = ++indexhelper;
            }
        }

        @Override

        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.bascket_page);
            mainActivity= new MainActivity();
            creator();
            listView = (ListView) findViewById(R.id.titles);
            String [] from = {"text1" , "text2"};
            String [] fromik = {"ftext1" , "ftext2"};
            int [] to = {R.id.textok1 , R.id.textok2};
            int [] too = {R.id.ftext1 , R.id.ftext2};
            ListAdapter adaptiruy = new SimpleAdapter(this, pizzaNames, R.layout.adapter_backet, from, to);
            listView.setAdapter(adaptiruy);
            workBD = new WorkBD(this);
            culculateFprice();

        }
...
}

I re-read a lot of sites that considered similar tasks, but could not figure it out and implement it in my code, I hope you can help me. Thank you in advance.

If you want to handle a click event inside your adapter item, you need to set an onClickListener to your button by creating a custom adapter extending from SimpleAdapter or BaseAdapter:

public class CustomAdapter extends SimpleAdapter {

    public CustomAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to) {
        super(context, data, resource, from, to);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View inflatedLayout = super.getView(position, convertView, parent);
        Button btn = inflatedLayout.findViewById(R.id.btn);
        btn.setOnClickListener(v -> {
          //Do whatever you want
        });
        return inflatedLayout;
    }
}

Then set it as your list adapter:

ListAdapter adapter = new CustomAdapter(context, data, R.layout.adapter_layout, from, to);
listView.setAdapter(adapter);

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