RecyclerView 多个按钮应保存 volley 的用户 ID,并在单击按钮时将用户 ID 发送到数据库

RecycleView multiplebuttons should hold user id from volley and send user id to database on click of buttons

我创建了一个带有多个按钮的 RecyclerView。使用 PHP 和 MySQL 的截击,两个按钮都将保持 user_id。但是我无法在单击特定用户的按钮时发送用户 ID。我试了很多都做不到。

实际上,有 2 个用户有图像,每个用户有两个按钮。每个按钮使用 PHP 从 Mysql 数据库填充 user_id。功能是当我按下按钮时,特定用户的 user_id 应该转到数据库并更新到数据库中,但这并没有发生。如果有人知道如何解决这个问题,请告诉我。



public class MainActivity extends AppCompatActivity  {
    //this is the JSON Data URL
    //make sure you are using the correct ip else it will not work
    private static final String URL_PRODUCTS = "http://192.168.10.119/staradmin/andcustomerrequest.php";

    //a list to store all the products
    List<CustomerRequest> productList;

    //the recyclerview
    RecyclerView recyclerView;
    CustomerRequestAdapter adapter;
    private RecyclerView mList;
    private LinearLayoutManager linearLayoutManager;
    RecyclerView.LayoutManager layoutManager;
    TextView textView;


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

        productList = new ArrayList<>();
        recyclerView = (RecyclerView) findViewById(R.id.recycleView);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this));
        adapter = new CustomerRequestAdapter(MainActivity.this, productList);
        recyclerView.setAdapter(adapter);

        loadProducts();

    }


    private void loadProducts(){

        StringRequest stringRequest = new StringRequest(Request.Method.GET, URL_PRODUCTS,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {

                        try {
                            JSONArray products = new JSONArray(response);
                            for(int i=0; i<products.length();i++){
                                JSONObject productObject = products.getJSONObject(i);
                                String user_id = productObject.getString("user_id");
                                String name = productObject.getString("name");
                                String price = productObject.getString("price");
                                //  String city = productObject.getString("city");
                                String image = productObject.getString("image");
                                String outletname = productObject.getString("outletname");

                                CustomerRequest product = new CustomerRequest(user_id,name,price,image,outletname);
                                productList.add(product);

                            }

                            //   adapter = new ProductAdapter(MainActivity.this,productList);
                            recyclerView.setAdapter(adapter);

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

                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(MainActivity.this,error.getMessage(),Toast.LENGTH_SHORT).show();

            }
        });

        Volley.newRequestQueue(this).add(stringRequest);
    }



}
----------------------------
adapterclass
public class CustomerRequestAdapter extends RecyclerView.Adapter<CustomerRequestAdapter.ProductViewHolder>  {
    private Context mCtx;
    private List<CustomerRequest> customerRequestList;
    //HashMap<String,String> modelList;

    private static final String URL_PRODUCTS = "http://192.168.10.119/staradmin/andcustomeradd.php";

    String UseridHolder;





    public CustomerRequestAdapter(Context mCtx, List<CustomerRequest> customerRequestList) {
        this.mCtx = mCtx;
        this.customerRequestList = customerRequestList;
       // this.modelList = modelList;
    }


    @NonNull
    @Override
    public ProductViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(mCtx);
        View view = inflater.inflate(R.layout.custrequest, null);
        return new ProductViewHolder(view);
    }


    @Override
    public void onBindViewHolder(@NonNull final ProductViewHolder holder, final int position) {

        final CustomerRequest customerRequest = customerRequestList.get(position);

        //loading the image
        Glide.with(mCtx)
                .load(customerRequest.getImage())
                .into(holder.imageView);
        holder.textViewId.setText(customerRequest.getUser_id());
        holder.textViewTitle.setText(customerRequest.getName());
        holder.textViewPrice.setText(customerRequest.getPrice());
        holder.textViewPrice.setText(customerRequest.getCity());
        holder.textViewOutlet.setText(customerRequest.getOutletname());


        holder.accept.setTag(R.id.accept);
        holder.reject.setTag(R.id.reject);

       holder.accept.setOnClickListener(new View.OnClickListener() {
           String  i = customerRequest.getUser_id();

           @Override
           public void onClick(View v) {
           final String   URL_PRODUCTS = "http://192.168.10.119/staradmin/andcustomeradd.php";

               final String i = customerRequest.getUser_id();
               UseridHolder = i.toString().trim();

              // Toast.makeText(mCtx,UseridHolder,Toast.LENGTH_LONG).show();
               //Toast.makeText(mCtx,String.valueOf(i),Toast.LENGTH_LONG).show();
               //  Intent intent = new Intent(mCtx,MainActivity.class);
               // intent.putExtra("user_id",customerRequest.getUser_id());
               //  mCtx.startActivity(intent);

               StringRequest stringRequest = new StringRequest(Request.Method.POST, URL_PRODUCTS,
                       new Response.Listener<String>() {
                           @Override
                           public void onResponse(String response) {

                               if(response.contains("true")){
                                   Toast.makeText(mCtx,response,Toast.LENGTH_LONG).show();
                               }else{
                                   Toast.makeText(mCtx,"hello",Toast.LENGTH_LONG).show();
                               }

                           }
                       }, new Response.ErrorListener() {
                   @Override
                   public void onErrorResponse(VolleyError error) {
                       Toast.makeText(mCtx,error.getMessage(),Toast.LENGTH_LONG).show();
                   }
               }
               ){

                   @Override
                   protected Map<String, String> getParams() throws AuthFailureError {
                       Map<String, String> params = new HashMap<String, String>();
                       params.put("i",UseridHolder);
                       return params;
                   }
               };

               Volley.newRequestQueue(mCtx).add(stringRequest);
           }

       });


        holder.reject.setOnClickListener(new View.OnClickListener() {
            String k = customerRequest.getUser_id();
            @Override
            public void onClick(View v) {
                Toast.makeText(mCtx,String.valueOf(k),Toast.LENGTH_LONG).show();
            }
        });


    }

    @Override
    public int getItemCount() {
        return customerRequestList.size();
    }


    class ProductViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

        TextView textViewTitle, textViewPrice, textViewOutlet, textViewId;
        ImageView imageView;
        Button accept, reject;

        RelativeLayout parentLayout;

        public ProductViewHolder(@NonNull View itemView) {
            super(itemView);

            textViewTitle = itemView.findViewById(R.id.textViewTitle);
            textViewPrice = itemView.findViewById(R.id.textViewPrice);
            textViewOutlet = itemView.findViewById(R.id.textViewOutlet);
            textViewId = itemView.findViewById(R.id.textViewId);
            imageView = itemView.findViewById(R.id.imageView);
            accept = itemView.findViewById(R.id.accept);
            reject = itemView.findViewById(R.id.reject);


        }

        @Override
        public void onClick(View v) {

        }
    }

}
-----------------
class
public class CustomerRequest {

    private String user_id;
    private String name,city,price,outletname;
  //  double price;
    private String image;

    public CustomerRequest(String user_id, String name, String price, String image, String outletname) {
        this.name = name;
        this.city = city;
        this.image = image;
        this.price = price;
        this.outletname = outletname;
        this.user_id = user_id;
    }

    public String getName() {
        return name;
    }

    public String getCity() {
        return city;
    }

    public String getPrice() {
        return price;
    }

    public String getImage() {
        return image;
    }

    public String getOutletname() {
        return outletname;
    }

    public String getUser_id() {
        return user_id;
    }
}

试试这个,你的问题要么是当用户点击按钮时无法获取 ID,要么是发送数据时出现问题

class ProductViewHolder extends RecyclerView.ViewHolder{

    TextView textViewTitle, textViewPrice, textViewOutlet, textViewId;
    ImageView imageView;
    Button accept, reject;

    RelativeLayout parentLayout;

    public ProductViewHolder(@NonNull View itemView) {
        super(itemView);

        textViewTitle = itemView.findViewById(R.id.textViewTitle);
        textViewPrice = itemView.findViewById(R.id.textViewPrice);
        textViewOutlet = itemView.findViewById(R.id.textViewOutlet);
        textViewId = itemView.findViewById(R.id.textViewId);
        imageView = itemView.findViewById(R.id.imageView);
        accept = itemView.findViewById(R.id.accept);
        reject = itemView.findViewById(R.id.reject);


       accept.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //send the id
                customerRequest.getUser_id();
            }
        });

    }
}