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