首页 > 解决方案 > 如何在listview android中添加按钮onclick事件?

问题描述

应用预览

这是我的应用程序预览。我的应用程序从谷歌表接收值。您可以看到紫色的按钮。Listview 包括来自工作表和按钮的四个值。按钮的作用是转到下一个使用意图的活动。

所以我的问题是如何在按钮上添加点击事件以进入下一个活动

list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">

<ListView
        android:id="@+id/lv_items"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" >

    </ListView>

</androidx.constraintlayout.widget.ConstraintLayout>

list_item_row.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:background="#fff"
        android:orientation="horizontal"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/constraintLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"

            app:layout_constraintBottom_toTopOf="@+id/tv_brand"
            app:layout_constraintTop_toTopOf="@+id/tv_brand"
            tools:layout_editor_absoluteX="5dp">

            <TextView
                android:id="@+id/tv_item_name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="33dp"
                android:text="TextView"
                android:textColor="#000"
                android:textSize="18dp"
                app:layout_constraintBottom_toTopOf="@+id/tv_brand"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

            <TextView
                android:id="@+id/tv_brand"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="14dp"
                android:text="TextView"
                android:textSize="15dp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintStart_toStartOf="@+id/tv_item_name"
                app:layout_constraintTop_toBottomOf="@+id/tv_item_name" />

            <TextView
                android:id="@+id/textView3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginEnd="18dp"
                android:text="id"
                android:textColor="#000"
                android:textSize="18dp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toStartOf="@+id/tv_price"
                app:layout_constraintTop_toTopOf="parent" />

            <TextView
                android:id="@+id/tv_price"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginEnd="51dp"
                android:text="TextView"
                android:textColor="#000"
                android:textSize="18dp"
                app:layout_constraintBaseline_toBaselineOf="@+id/textView3"
                app:layout_constraintEnd_toEndOf="parent" />

            <Button
                android:id="@+id/button"
                android:layout_width="46dp"
                android:layout_height="20dp"
                android:layout_marginEnd="65dp"
                android:layout_marginTop="14dp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintTop_toTopOf="parent" />
        </androidx.constraintlayout.widget.ConstraintLayout>
    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

ListItem.java

public class ListItem extends AppCompatActivity
{
    ListView listView;
    ListAdapter adapter;
    ProgressDialog loading;
    Button button;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list_item);

        listView = (ListView) findViewById(R.id.lv_items);
        button = findViewById(R.id.button);
        getItems();
    }

    private void getItems() {

        loading =  ProgressDialog.show(this,"Loading","please wait",false,true);

        StringRequest stringRequest = new StringRequest(Request.Method.GET, "My Web App URL is here",
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        parseItems(response);
                    }
                },

                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                    }
                }
        );

        int socketTimeOut = 50000;
        RetryPolicy policy = new DefaultRetryPolicy(socketTimeOut, 0, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);

        stringRequest.setRetryPolicy(policy);

        RequestQueue queue = Volley.newRequestQueue(this);
        queue.add(stringRequest);

    }


    private void parseItems(String jsonResposnce) {

        ArrayList<HashMap<String, String>> list = new ArrayList<>();

        try {
            JSONObject jobj = new JSONObject(jsonResposnce);
            JSONArray jarray = jobj.getJSONArray("items");


            for (int i = 0; i < jarray.length(); i++) {

                JSONObject jo = jarray.getJSONObject(i);

                String itemName = jo.getString("itemName");
                String brand = jo.getString("brand");
                String price = jo.getString("price");
                String id = jo.getString("id");


                HashMap<String, String> item = new HashMap<>();
                item.put("itemName", itemName);
                item.put("brand", brand);
                item.put("price",price);
                item.put("id",id);

                list.add(item);


            }
        } catch (JSONException e) {
            e.printStackTrace();
        }


        adapter = new SimpleAdapter(this,list,R.layout.list_item_row,
                new String[]{"itemName","brand","price","id",},new int[]{R.id.tv_item_name,R.id.tv_brand,R.id.tv_price,R.id.textView3,R.id.button});


        listView.setAdapter(adapter);
        loading.dismiss();
    }

}

标签: javaandroid-listviewcustom-adapter

解决方案


推荐阅读