это мой последний проект, и я хочу помочь вам:
создайте адаптер для вашего переработчика, как показано ниже:
public class ListAddressAdapter extends RecyclerView.Adapter<ListAddressAdapter.MyViewHolder> {
Context context;
List<ShippingInfoResponse> list;
ApiInterface apiInterface;
SelectAddress addressClicked;
deleteAddress deleteClicked;
boolean cardViewClicked = false;
public ListAddressAdapter(Context context, List<ShippingInfoResponse> list) {
this.context = context;
this.list = list;
}
@Override
public ListAddressAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.adress_list_item, parent, false);
return new ListAddressAdapter.MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(final ListAddressAdapter.MyViewHolder holder, final int position) {
holder.cardView.setBackgroundColor(context.getResources().getColor(R.color.white));
holder.txtShowName.setText(list.get(position).getName());
holder.txtShowProvince.setText(list.get(position).getProvince());
holder.txtShowCity.setText(list.get(position).getCity());
holder.txtShowAddress.setText(list.get(position).getCity()+" "+list.get(position).getAddress());
holder.txtShowNumber.setText(list.get(position).getMobile());
holder.txtEdit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(context, AddAddressActivity.class);
intent.putExtra("id", list.get(position).getId());
intent.putExtra("name", list.get(position).getName());
intent.putExtra("province", list.get(position).getProvince());
intent.putExtra("city", list.get(position).getCity());
intent.putExtra("address", list.get(position).getAddress());
intent.putExtra("postalCode", list.get(position).getPostalCode());
intent.putExtra("mobile", list.get(position).getMobile());
context.startActivity(intent);
}
});
holder.txtSelect.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
addressClicked.selectAddress(
list.get(position).getName(),
list.get(position).getAddress(),
list.get(position).getMobile(),
list.get(position).getId(),
list.get(position).toString()
);
Log.e("select", "clicked");
}
});
holder.txtDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
deleteClicked.deleteAddress(list.get(position).getId());
}
});
}
@Override
public int getItemCount() {
return list.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView txtName, txtShowName;
TextView txtProvince, txtShowProvince;
TextView txtCity, txtShowCity;
TextView txtAddress, txtShowAddress;
TextView txtNumber, txtShowNumber;
TextView txtDelete;
TextView txtEdit;
CardView cardView;
LinearLayout linearTxtEdit;
TextView txtSelect;
public MyViewHolder(View itemView) {
super(itemView);
txtName = (TextView) itemView.findViewById(R.id.txt_name_in_list_address);
txtShowName = (TextView) itemView.findViewById(R.id.txt_show_name);
txtProvince = (TextView) itemView.findViewById(R.id.txt_province_in_list_address);
txtShowProvince = (TextView) itemView.findViewById(R.id.txt_show_province);
txtCity = (TextView) itemView.findViewById(R.id.txt_city_in_list_address);
txtShowCity = (TextView) itemView.findViewById(R.id.txt_show_city);
txtAddress = (TextView) itemView.findViewById(R.id.txt_address_in_list_address);
txtShowAddress = (TextView) itemView.findViewById(R.id.txt_show_address);
txtNumber = (TextView) itemView.findViewById(R.id.txt_number_in_list_address);
txtShowNumber = (TextView) itemView.findViewById(R.id.txt_show_number);
txtEdit = (TextView) itemView.findViewById(R.id.txt_edit);
cardView = (CardView) itemView.findViewById(R.id.cardItem_address);
linearTxtEdit = (LinearLayout) itemView.findViewById(R.id.linearTxtEdit);
txtSelect = (TextView) itemView.findViewById(R.id.txt_select_address);
txtDelete = (TextView) itemView.findViewById(R.id.txt_delete_Address);
txtEdit.setTypeface(Application.vazirDigit);
txtName.setTypeface(Application.vazirDigit);
txtShowName.setTypeface(Application.vazirDigit);
txtProvince.setTypeface(Application.vazirDigit);
txtShowProvince.setTypeface(Application.vazirDigit);
txtCity.setTypeface(Application.vazirDigit);
txtShowCity.setTypeface(Application.vazirDigit);
txtAddress.setTypeface(Application.vazirDigit);
txtShowAddress.setTypeface(Application.vazirDigit);
txtNumber.setTypeface(Application.vazirDigit);
txtShowNumber.setTypeface(Application.vazirDigit);
txtSelect.setTypeface(Application.vazirDigit);
txtDelete.setTypeface(Application.vazirDigit);
}
}
public interface SelectAddress {
public void selectAddress(String name, String address, String number, int id, String position);
}
public void setOnSelectAddressClicked(SelectAddress selectAddressClicked) {
this.addressClicked = selectAddressClicked;
}
public interface deleteAddress {
public void deleteAddress(int id);
}
public void setOnDeleteClicked(deleteAddress deleteAddressClicked) {
this.deleteClicked = deleteAddressClicked;
}
}
и в вашей деятельности:
private void fillRecyclerView() {
adapter = new ListAddressAdapter(this, info);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
avl.setVisibility(View.GONE);
adapter.setOnSelectAddressClicked(new ListAddressAdapter.SelectAddress() {
@Override
public void selectAddress(String name, String address, String number, int id, String position) {
Name = name;
Address = address;
Number = number;
Id = id;
deleteId=id;
Log.e("Name", name);
SelectAddressFromList();
}
});
adapter.setOnDeleteClicked(new ListAddressAdapter.deleteAddress() {
@Override
public void deleteAddress(int id) {
deleteId = id;
Log.w("id", deleteId + "");
deleteLastAddress();
avl.setVisibility(View.GONE);
}
});
}
private void deleteLastAddress() {
apiInterface = ApiClient.getClient(this).create(ApiInterface.class);
Call<Void> call = apiInterface.deleteAddress(deleteId);
call.enqueue(new Callback<Void>() {
@Override
public void onResponse(Call<Void> call, Response<Void> response) {
Log.w("responceCodeDelete", response.code() + "");
Log.w("responceMessage", response.message());
info.clear();
avl.setVisibility(View.GONE);
getAllAddress();
}
@Override
public void onFailure(Call<Void> call, Throwable t) {
}
});
}
private void SelectAddressFromList() {
apiInterface = ApiClient.getClient(this).create(ApiInterface.class);
Call<Void> call = apiInterface.sendShippingInfo(
Name,
"خوزستان"
, "اهواز",
Address,
"4947118914",
Number
);
call.enqueue(new Callback<Void>() {
@Override
public void onResponse(Call<Void> call, Response<Void> response) {
Log.w("RspncCodSendAddressFrmLst", response.code() + "");
if (response.isSuccessful()) {
Intent intent = new Intent();
intent.putExtra("Name", Name);
intent.putExtra("Address", Address);
intent.putExtra("id", Id);
Log.w("sendingId", Id + "");
setResult(RESULT_OK, intent);
deleteLastAddress();
finish();
}
}
@Override
public void onFailure(Call<Void> call, Throwable t) {
}
});
}