Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
58 views
in Technique[技术] by (71.8m points)

java - Android multi-select recyclerview items

I need to select some recyclerview items and send them to server with volley.

For some reason I can't seem the get the names of the selected items when I click on them.

I've logged the response, and I get absolutely nothing.

Here's the relevant code in the activity class:


        submitbutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                List<ListPetsModel> selectedPets = petAdapter.getSelectedPets();

                StringBuilder selectedPetNames = new StringBuilder();
                for (int i = 0; i > selectedPets.size(); i++) {
                    if (i == 0) {
                        selectedPets.get(i).getName();
                        selectedPetNames.append(selectedPets.get(i).getName());

                    } else {
                        selectedPetNames.append("
").append(selectedPets.get(i).getName());

                    }
                }
                Toast.makeText(Schedule.this, selectedPetNames, Toast.LENGTH_SHORT).show();

                sendrequest(owner, "3", "4", address, selectedPetNames.toString());
                Log.d("TAG", "" + String.valueOf(getname));
                Log.d("TAG", "This" + owner);
                Log.d("TAG", "This" + address);
                Log.d("TAG", "This" + selectedPetNames.toString());

                Log.d("TAG", "" + String.valueOf(getname));
                Log.d("TAG", "This" + owner);
                Log.d("TAG", "This" + address);
                Log.d("TAG", "This" + selectedPetNames.toString());
                Log.d("TAG", "This 3" + selectedPetNames);


            }

        });
    }

    


    private void getPetDetails() {
        RequestQueue queue = Volley.newRequestQueue(this);
        JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.GET, URL, null, new Response.Listener<JSONArray>() {
            @Override
            public void onResponse(JSONArray response) {
                for (int i = 0; i < response.length(); i++) {
                    try {
                        JSONObject petDetailsObject = response.getJSONObject(i);
                        ListPetsModel listPetsModel = new ListPetsModel();
                        listPetsModel.setName(petDetailsObject.getString("name"));
                        listPetsModelList.add(listPetsModel);
                        petAdapter.setListPetsModelList(listPetsModelList);


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

                petAdapter.notifyDataSetChanged();


            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.d("tag", "onErrorResponse: " + error.getMessage());
            }
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> params = new HashMap<String, String>();
                params.put("owner", owner);
                return params;

            }
        };

        petAdapter = new petAdapter(this, listPetsModelList, this);
        recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
        recyclerView.setAdapter(petAdapter);


        petAdapter.notifyDataSetChanged();


        queue.add(jsonArrayRequest);

    }

    

    private void sendrequest(final String owner, final String latitude, final String longitude, final String address, final String pets) {

        final ProgressDialog progressDialog = new ProgressDialog(this);
        progressDialog.setCancelable(false);
        progressDialog.setMessage("Please Wait.....");
        progressDialog.show();
        try {
            String url = "http://happy-paws.co.za/dogwalking/apis/v1/pet/request_walk.php";
            StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
                    new com.android.volley.Response.Listener<String>() {
                        @Override
                        public void onResponse(String response) {
                            progressDialog.dismiss();
                            String result = response.toString();
//                            Toast.makeText(Register.this, ""+result, Toast.LENGTH_SHORT).show();
                            getDataResponse1(result);
                            Log.d("TAG", "The" + result);
                            Log.i("TAG", "The" + result);
                            Log.d("TAG", "The" + response);
                            Log.i("TAG", "The" + response);
                        }
                    },
                    new com.android.volley.Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            Toast.makeText(Schedule.this, "" + error, Toast.LENGTH_SHORT).show();
                            progressDialog.dismiss();
                        }
                    }) {

                @Override
                public byte[] getBody() throws com.android.volley.AuthFailureError {
                    String str = "{"owner":"" + owner + "","latitude":"" + latitude + "","longitude":"" + longitude + "","address":"" + address + "","pets":"" + pets + ""}";
                    return str.getBytes();

                }

                ;

                @Override
                public Map<String, String> getHeaders() {
                    HashMap<String, String> headers = new HashMap<>();
//                    String auth = "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwOlwvXC9leGFtcGxlLm9yZyIsImF1ZCI6Imh0dHA6XC9cL2V4YW1wbGUuY29tIiwiaWF0IjoxMzU2OTk5NTI0LCJuYmYiOjEzNTcwMDAwMDAsImRhdGEiOnsiaWQiOiI3IiwiZmlyc3RuYW1lIjoiUHJhdGlrc2hhIiwic3VybmFtZSI6IlBhdGVsIiwiZW1haWwiOiJwcmF0aWtzaGFAZ21haWwuY29tIn19.hp5m-weJVfX_JTrZ3r1YfMpEgfQV1TSiwaP6BE3E3_8";
//                    headers.put("Authorization", auth);

                    return headers;
                }

                @Override
                protected Map<String, String> getParams() {
                    Map<String, String> params = new HashMap<String, String>();
//                    params.put("newsfeed_id",newsfeed_id);
//                    params.put("member_id",member_id);


                    return params;
                }

            };
            stringRequest.setRetryPolicy(new DefaultRetryPolicy(30000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
            RequestQueue requestQueue = Volley.newRequestQueue(Schedule.this);
            requestQueue.add(stringRequest);

        } catch (Exception e) {
            //App.handleUncaughtException(e);
        }
    }

    public void getDataResponse1(String result) {
        try {
            JSONObject jsonObject = new JSONObject(result);
            String message = jsonObject.getString("message");
//            String android_status=jsonObject.getString("android_status");
            Log.d("TAG", "The" + result);
            Log.i("TAG", "The" + result);

            if (message.equals("request was created.")) {
                Toast.makeText(Schedule.this, "" + message, Toast.LENGTH_SHORT).show();

            } else {
                Toast.makeText(Schedule.this, "" + message, Toast.LENGTH_SHORT).show();

            }

//

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

On submit I'm supposed to get the names of the selected items, but I don't.

Here's the model class:

public class ListPetsModel{
    public String name;

    public boolean isSelected =  false;

    public boolean isSelected() {
        return isSelected;
    }

    public void setSelected(boolean seleleted) {
        isSelected = seleleted;
    }


    public ListPetsModel(){}
    public ListPetsModel(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

Here's the adapter:

public class petAdapter extends RecyclerView.Adapter<petAdapter.ViewHolder> {
    LayoutInflater inflater;
    List<ListPetsModel> listPetsModelList;
    private FurryNameListener furryNameListener;


    public petAdapter(Context context, List<ListPetsModel> listPetsModelList, FurryNameListener furryNameListener) {
        this.inflater = LayoutInflater.from(context);
        this.listPetsModelList = listPetsModelList;
        this.furryNameListener = furryNameListener;

    }

    public void setListPetsModelList(List<ListPetsModel> listPetsModelList) {
        this.listPetsModelList = new ArrayList<>();
        this.listPetsModelList = listPetsModelList;
        notifyDataSetChanged();
    }


    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.selectpets, parent, false);
        return new ViewHolder(view);

    }


    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        holder.petname.setText(listPetsModelList.get(position).getName());
        holder.bindPetList(listPetsModelList.get(position));

    }


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

    public List<ListPetsModel> getSelectedPets() {
        List<ListPetsModel> selectedPetItems = new ArrayList<>();
        for (ListPetsModel listPetsModel : listPetsModelList) {
            if (listPetsModel.isSelected) {
                selectedPetItems.add(listPetsModel);
            }
        }

        return selectedPetItems;
    }


    class ViewHolder extends RecyclerView.ViewHolder {
        AppCompatButton petname;
        Boolean selected;


//        public List<ListPetsModel> getAll() {
//            return listPetsModelList;
//        }


        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            selected = false;
            petname = itemView.findViewById(R.id.selectpet);
//            petname.setOnClickListener(this);
        }

        void bindPetList(final ListPetsModel listPetsModel) {


            petname.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listPetsModel.isSelected) {
                        listPetsModel.isSelected = false;
                        v.setBackgroundResource(R.drawable.selectpetactive);
                        petname.setTextColor(Color.parseColor("#FFFFFFFF"));
                    } else {
                        listPetsMod

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...