How to pass data from adapter class to bottom sheet dialog fragment - android

I have a recycler view in my fragment and every recycler view row item has a button so when user clicks on a button it should open BottomSheetDialogFragment and detail info of that particular row item should be visible in BottomSheetDialogFragment.
What I did so far dialog fragment is showing properly on the click of a button but I am not getting any idea of how can I pass data to dialog fragment from adapter class.
Below is my code:
FactsAdapter.java
public class FactsAdapter extends RecyclerView.Adapter<FactsAdapter.ViewHolder> {
List<Facts> factList;
Context context;
public FactsAdapter(List<Facts> factList, Context context) {
this.factList = factList;
this.context = context;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.facts_row,parent,false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
final Facts model = factList.get(position);
final String str1 = model.getDescription();
RequestOptions requestOptions = new RequestOptions();
requestOptions.placeholder(R.color.place);
holder.title.setText(model.getTitle());
Glide.with(context).load(model.getImage()).apply(requestOptions).into(holder.factImage);
holder.more.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v){
BottomSheet bottomSheet = new BottomSheet();
bottomSheet.show(((FragmentActivity) context).getSupportFragmentManager(),bottomSheet.getTag());
}
});
}
#Override
public int getItemCount() {
return factList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
ImageView factImage;
TextView title;
Button more;
public ViewHolder(#NonNull View itemView) {
super(itemView);
factImage = itemView.findViewById(R.id.factImage);
title = itemView.findViewById(R.id.title);
more = itemView.findViewById(R.id.more);
}
}
}
How can I pass data to dialog fragment?

test it
adapter:
Bundle args = new Bundle();
args.putString("key", "value");
BottomSheet bottomSheet = new BottomSheet();
bottomSheet .setArguments(args);
bottomSheet .show(((FragmentActivity) context).getSupportFragmentManager(),bottomSheet.getTag());
bottom sheet fragment :
Bundle mArgs = getArguments();
String myValue = mArgs.getString("key");

If I get you right, you can try to make one interface:
public interface DataTransferInterface {
public void onSetValues(ArrayList<?> al);
}
Then impletent this inteface in your Fragment/Activity/BottomSheetDialogFragment and pass it as listener to your adapter's constructor:
#Override
public void onSetValues(List<Fact> list) {
if (list.size() < 1) {
txtEmptyAdapter.setVisibility(View.VISIBLE);
}
}
public FactsAdapter(List<Facts> factList, Context context, DataTransferInterface listener) {
this.factList = factList;
this.context = context;
this.listener = listener;
}
Now it's easy to call this listener wherever you need and get required data in your Fragment/Activity/BottomSheetDialogFragment, like that:
listener.onSetValues(factList);

Related

Move an item of recyclerview into another recyclerview on button click on custom adapter

I have two recycler views and both of them use the same custom adapter.
https://imgur.com/3WMGgiL - this is how the item from the recycler view looks like
What i want to do is, whenever the "Anuleaza rezervarea" button is clicked, i want to remove the item from the current recyclerview and add it to another recycler view
How can i do that? I'll leave my custom adapter code below. I really don't understand what to write in the on click listener.
public class RecyclerViewAdapterRezervare extends RecyclerView.Adapter<RecyclerViewAdapterRezervare.RecycleViewHolder> {
private List<Rezervare> rezervari;
private Context context;
private LayoutInflater inflater;
private String numeUtilizator;
private String status;
private Rezervare rezervare = new Rezervare();
private DatabaseReference reff;
public RecyclerViewAdapterRezervare(List<Rezervare> rezervari, Context context, String numeUtilizator) {
this.rezervari = rezervari;
this.context = context;
this.numeUtilizator = numeUtilizator;
this.inflater = LayoutInflater.from(context);
}
#NonNull
#Override
public RecycleViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.rezervari_layout, parent, false);
return new RecycleViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull RecycleViewHolder holder, int position) {
rezervare = rezervari.get(position);
holder.btn_anuleaza.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
new AlertDialog.Builder(view.getContext()).setTitle("Anulare rezervare").setMessage("Sunteti sigur ca doriti anularea rezervarii?")
.setPositiveButton(R.string.da, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
holder.btn_anuleaza.setText(R.string.anulat);
holder.btn_anuleaza.setBackgroundColor(view.getResources().getColor(R.color.red));
}
}).setNegativeButton(R.string.nu, null).show();
}
});
}
#Override
public int getItemCount() {
return rezervari.size();
}
public static class RecycleViewHolder extends RecyclerView.ViewHolder {
public TextView tv_nume_teren;
public TextView tv_adresa_teren;
public TextView tv_ora;
public TextView tv_data;
public Button btn_anuleaza;
public Button btn_navigheaza;
public static boolean isCanceled;
public RecycleViewHolder(#NonNull View itemView) {
super(itemView);
tv_nume_teren = itemView.findViewById(R.id.tv_rezervare_nume_teren);
tv_adresa_teren = itemView.findViewById(R.id.tv_rezervare_adresa_teren);
btn_anuleaza = itemView.findViewById(R.id.btn_anuleaza);
btn_navigheaza = itemView.findViewById(R.id.btn_maps);
tv_ora = itemView.findViewById(R.id.tv_rezervare_ore);
tv_data = itemView.findViewById(R.id.tv_rezervare_data);
isCanceled = false;
if(isCanceled){
btn_anuleaza.setText(R.string.anulat);
btn_anuleaza.setBackgroundColor(itemView.getResources().getColor(R.color.red));
} else {
btn_anuleaza.setText(R.string.anuleaza_rezervare);
btn_anuleaza.setBackgroundColor(itemView.getResources().getColor(R.color.teal_700));
}
}
}
When i click on the "Anuleaza rezervare" button, the two buttons become red and unclickable, and i want to transfer the item to another recycler view. How can i do that? Thank you!
You can achieve this by calling,
int removeIndex = 2;
data.remove(removeIndex);
adapter.notifyItemRemoved(removeIndex);
on the first recyclerView and update the second by calling,
String newItem = "This is a new item.";
int updateIndex = 3; // Your index to insert new the item
data.set(updateIndex, newItem);
adapter.notifyItemChanged(updateIndex);
for the second recyclerView.

Communication between adapter and fragment in android

I am stuck need your kind help, i have an activity in android user sign up, and apart from general information i have a list of existing users/entities shown in recyclerview and each item of recyclerview list contains a follow button, i want to captures the position/ buttons that were clicked to check which users/entities the new user has followed. Any suggest how to do it?
I had customer adapter and recycler view, Check the attached image for clarity. I want to create an array list that will contain ids of each list item whose button is clicked.
RecyclerView containing list item and follow button
//this adapter use on admin
public class FollowLocalBusinessAdapter extends RecyclerView.Adapter {
AlertDialog.Builder alertDialogBuilder;
private Context context;
private List<FollowLocalBusinessMC> list;
private LayoutInflater layoutInflater;
private DatabaseReference mDatabase;//database reference
private FollowLocalBusinessMC localBusinessMC;
private ListAdapterListener mListener;
public FollowLocalBusinessAdapter(Context context, List<FollowLocalBusinessMC> list) {
this.context = context;
this.list = list;
layoutInflater = LayoutInflater.from(context);
alertDialogBuilder = new AlertDialog.Builder(context);
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int position) {
View view = layoutInflater.inflate(R.layout.list_item_local_business, parent, false);
MyViewHolder holder = new MyViewHolder(view);
return holder;
}
#Override
public void onBindViewHolder(final FollowLocalBusinessAdapter.MyViewHolder holder, final int position)//is called
{
localBusinessMC = list.get(position);
if (holder != null) {
if (holder.followBtn.getText().equals("Follow")) {
holder.followBtn.setBackgroundResource(R.drawable.follow_btn_bg);
holder.followBtn.setTextColor(context.getResources().getColor(R.color.colorBlueButton));
} else {
holder.followBtn.setBackgroundResource(R.drawable.following_btn_bg);
holder.followBtn.setTextColor(context.getResources().getColor(R.color.colorWhite));
}
holder.mainHeading.setText(localBusinessMC.getMainHeading());
holder.subHeading.setText(localBusinessMC.getSubHeading());
Glide.with(context).load(localBusinessMC.getIcon()).into(holder.icon);
holder.followBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(context, "" + list.get(position).getId(), Toast.LENGTH_SHORT).show();
if (holder.followBtn.getText().equals("Follow")) {
holder.followBtn.setText("Following");
holder.followBtn.setTextColor(context.getResources().getColor(R.color.colorWhite));
holder.followBtn.setBackgroundResource(R.drawable.following_btn_bg);
} else {
holder.followBtn.setText("Follow");
holder.followBtn.setBackgroundResource(R.drawable.follow_btn_bg);
holder.followBtn.setTextColor(context.getResources().getColor(R.color.colorBlueButton));
}
}
});
}
}
#Override
public int getItemCount() {
return list.size();
}
public interface ListAdapterListener { // create an interface
void onClickAtOKButton(int position); // create callback function
}
class MyViewHolder extends RecyclerView.ViewHolder {
TextView mainHeading, subHeading;
ImageView icon;
Button followBtn;
MyViewHolder(View v) {
super(v);
mainHeading = v.findViewById(R.id.main_lbsr_id);
subHeading = v.findViewById(R.id.sub_lbsr_id);
icon = v.findViewById(R.id.icon_lbsr_id);
followBtn = v.findViewById(R.id.followBtn_lbsr_id);
}
}
}
create an interface in your Adapter with one method :
public interface CallBack{
void onItemClicked(int position , /*anyThing else*/);
}
then get an instance of interface in your Adapter :
private YourAdapter.CallBack;
in your Adpater constructor get object of CallBack :
public YourAdapter(Context context, List</* */> list, YourAdapter.CallBackcallback callBack) {
this.context = context;
this.list= list;
this.callback = callback;
}
#Override
public void onBindViewHolder(#NonNull ChoosenExerciseViewHolder holder, final int position) {
holder.View.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
callback.onItemClicked(
//pass any parameter youe defined
//for ex : position , ///);
}
});
}
then in your Activity/Fragment :
private YourAdapter.CallBack callback= (position, /* */ ) -> {
// do what you want
};
YourAdapter adapter = new YourAdapter(getContext(), calback, list);
receyclerview.setAdapter(adapter )

Android: Pass text from Textview when clicking an Image button inside recycler view

So I have a recycler view with 2 texts and 1 image button. I want to click the image button and then open a new activity and transfering the text from the textViewADV1 to the next activity
My items .xml in my recyclerview consist of the following
<ImageButton
android:id="#+id/imageButton1"
android:clickable="true"
android:onClick="openActivity2"/>
<TextView
android:id="#+id/textViewADV1"
android:text="Line 1"/>
<TextView
android:id="#+id/textViewADV2"
android:text="Line 2"/>
My openActivity2() from Main Activity
public void openActivity2(View view)
{
Intent intentLoadNewActivity = new Intent(AdvancedResults.this,OpenSelectedAdvanced.class);
startActivity(intentLoadNewActivity);
}
I tried doing textViewADV1.getText().toString() but it only reads the text from the first item in the recycler view
My recycler adapter
public class AdvancedAdapter extends RecyclerView.Adapter<AdvancedAdapter.AdvancedViewHolder> {
private ArrayList<AdvancedItem> mAdvancedList;
public String mImage;
public static class AdvancedViewHolder extends RecyclerView.ViewHolder {
public ImageButton mImagebtn;
public TextView mTextView1;
public TextView mTextView2;
public AdvancedViewHolder(View itemView) {
super(itemView);
mImagebtn = itemView.findViewById(R.id.imageButtonADV);
mTextView1 = itemView.findViewById(R.id.textViewADV1);
mTextView2 = itemView.findViewById(R.id.textViewADV2);
}
}
public AdvancedAdapter(ArrayList<AdvancedItem> advancedList) {
mAdvancedList = advancedList;
}
#Override
public AdvancedViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.advanced_item, parent, false);
AdvancedViewHolder evh = new AdvancedViewHolder(v);
return evh;
}
#Override
public void onBindViewHolder(AdvancedViewHolder holder, int position) {
AdvancedItem currentItem = mAdvancedList.get(position);
holder.mTextView1.setText(currentItem.getText1());
holder.mTextView2.setText(currentItem.getText2());
mImage = "www.testImageURL.com";
Picasso.get().load(mImage).into(holder.mImagebtn);
}
#Override
public int getItemCount() {
return mAdvancedList.size();
}
}
First of all pass a context to your adapter when you initialize it:
//in the main activity that you initialize the adapter in
AdvancedAdapter adapter = new AdvancedAdapter(your_list , MainActivity.this);
Now change the constructor of AdvancedAdapter to accept context
public class AdvancedAdapter extends RecyclerView.Adapter<AdvancedAdapter.AdvancedViewHolder> {
private ArrayList<AdvancedItem> mAdvancedList;
private Context context;
.............
//constructor
public AdvancedAdapter(ArrayList<AdvancedItem> advancedList,Context context) {
mAdvancedList = advancedList;
this.context = context;
}
Now in onBindViewHolder in the AdvancedAdapter
#Override
public void onBindViewHolder(AdvancedViewHolder holder, int position) {
AdvancedItem currentItem = mAdvancedList.get(position);
holder.mTextView1.setText(currentItem.getText1());
holder.mTextView2.setText(currentItem.getText2());
mImage = "www.testImageURL.com";
Picasso.get().load(mImage).into(holder.mImagebtn);
//on click image button
hodler.mImagebtn.setOnclickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
Intent intent = new Intent(context , OpenSelectedAdvanced.class);
intent.putExtra("data" , currentItem.getText1());
context.startActivity(intent);
}
});
}
To get the data from OpenSelectedAdvanced activity:
//in oncreate method:
Intent intent = getIntent();
//this is your text view text that came from the clicked image button
String transferedText = intent.getStringExtra("data");
If you have declared your onBindViewHolder() in your adapter class so why are you starting activity from MainActivity. You should add setOnclicklistenr() in onBindViewHolder() to achieve the desired result that you want that is in following way :
public class AdvancedAdapter extends RecyclerView.Adapter<AdvancedAdapter.AdvancedViewHolder> {
private ArrayList<AdvancedItem> mAdvancedList;
//one more thing you have to create Context field so that you can
//you can start the activity from any context (From Any activity)
private Context mContext;
public String mImage;
public static class AdvancedViewHolder extends RecyclerView.ViewHolder {
public ImageButton mImagebtn;
public TextView mTextView1;
public TextView mTextView2;
public AdvancedViewHolder(View itemView) {
super(itemView);
mImagebtn = itemView.findViewById(R.id.imageButtonADV);
mTextView1 = itemView.findViewById(R.id.textViewADV1);
mTextView2 = itemView.findViewById(R.id.textViewADV2);
}
}
public AdvancedAdapter(ArrayList<AdvancedItem> advancedList, Context mContext) {
mAdvancedList = advancedList;
this.mContext = mContext;
}
#Override
public AdvancedViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.advanced_item, parent, false);
AdvancedViewHolder evh = new AdvancedViewHolder(v);
return evh;
}
#Override
public void onBindViewHolder(AdvancedViewHolder holder, int position) {
AdvancedItem currentItem = mAdvancedList.get(position);
holder.mTextView1.setText(currentItem.getText1());
holder.mTextView2.setText(currentItem.getText2());
mImage = "www.testImageURL.com";
holder.mImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Here You start your activity
Intent intent = new Intent(mContext, Activity2.class);
//you can putExtras here
mContext.startActivity(intent);
});
Picasso.get().load(mImage).into(holder.mImagebtn);
}
#Override
public int getItemCount() {
return mAdvancedList.size();
}
}
Now initialize this adapter in your activity where you implemented recyclerView And Send that arraylist into the parameters of that constructor of your adapter :) if did not understand yet you can try the tutorial on the link given below : https://www.youtube.com/watch?v=bIppSKk9afI

Adding item to a recyclerview from dialog within a fragment class.?

I have a fragment which must contains two recycler views. The view looks like follows. i need to add an item to the bottom recyclerview from a dialog. L'll post my code for the adapter class and xml. Please guide me to achieve this, a sample or a code is more than welcome.
I've tried to pass the data from the dialog to the second recyclerview but it does not show the added item. Tell me anything i'm doing wrong here. Nothing i tried worked out to update my second recyclerview... :/
My Fragment Class
public class ReceiptFragment extends Fragment { //implements ContReceiptsAdapter.ItemOnClickListener
RecyclerView rv_rceipts, rv_addpayments;
RecyclerView.Adapter mContReceiptsAdapter,mAddPaymentsAdapter;
RecyclerView.LayoutManager mLayoutManager1,mLayoutManager2;
View receipts_view;
private Context mContext;
ContReceiptsAdapter contReceiptsAdapter;
AddPaymentsAdapter addPaymentsAdapter;
//-- items in detailed dialog here
private ArrayList<Contract> contracts;
private ArrayList<AddPayments> payments;
private View linearLayout;
public View mRootView;
public ReceiptFragment() {
}
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
receipts_view = inflater.inflate(R.layout.fragment_receipt, container, false);
BuildReceiptsRV();
BuildAddedPaymentsRV();
// for setting the first item as default in the list
// onItemClicked(0);
return receipts_view;
}
private void BuildReceiptsRV() {
rv_rceipts = receipts_view.findViewById(R.id.rv_tobe_added);
rv_rceipts.setHasFixedSize(true);
rv_rceipts.setItemAnimator(new DefaultItemAnimator());
mLayoutManager1 = new LinearLayoutManager(getActivity());
rv_rceipts.setLayoutManager(mLayoutManager1);
mContReceiptsAdapter = new ContReceiptsAdapter(getActivity(), ApplicationClass.contractList);
rv_rceipts.setAdapter(mContReceiptsAdapter);
}
private void BuildAddedPaymentsRV() {
rv_addpayments = receipts_view.findViewById(R.id.rv_added);
rv_addpayments.setHasFixedSize(true);
rv_addpayments.setItemAnimator(new DefaultItemAnimator());
mLayoutManager2 = new LinearLayoutManager(getActivity());
rv_addpayments.setLayoutManager(mLayoutManager2);
mAddPaymentsAdapter = new AddPaymentsAdapter(getActivity(),ApplicationClass.paymentList);
rv_addpayments.setAdapter(mAddPaymentsAdapter);
}
//change when click on change on item added to second rv
private void changeLayoutRV() {
linearLayout = mRootView.findViewById(R.id.rv_tobe_added);
if (linearLayout.getVisibility() == View.GONE) {
linearLayout.setVisibility(View.VISIBLE);
} else {
linearLayout.setVisibility(View.GONE);
}
}
}
My Adapter for the First Recyclerview
public class ContReceiptsAdapter extends RecyclerView.Adapter<ContReceiptsAdapter.ReceiptsViewHolder> {
// private static Dialog AddItemDialog;
private ArrayList<Contract> contracts;
private ArrayList<AddPayments> payments = new ArrayList<>();
Context mContext;
String inPending;
//for the details dialog
EditText et_number, contract_id_val, product_type_val, mobile_number_ADialog, et_amount_add;
// ImageView btn_add_item;
//--to set to add item dialog
public TextView contract_number, vehicle_number, contract_number_ADialog, vehicle_number_ADialog;
CheckBox checkbox_pending;
public ContReceiptsAdapter(Context context, ArrayList<Contract> contractList) {
contracts = contractList;
mContext = context;
}
public class ReceiptsViewHolder extends RecyclerView.ViewHolder {
TextView tv_contract, tvVehicleN, tvContractType;
ImageView btn_add_item;
LinearLayout detailsLayout;
public ReceiptsViewHolder(#NonNull final View itemView) {
super(itemView);
detailsLayout = itemView.findViewById(R.id.add_details_item_id);
tv_contract = itemView.findViewById(R.id.tv_contract);
tvVehicleN = itemView.findViewById(R.id.tvVehicleN);
tvContractType = itemView.findViewById(R.id.tvContractType);
btn_add_item = itemView.findViewById(R.id.btn_add_item);
}
}
#NonNull
#Override
public ContReceiptsAdapter.ReceiptsViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.receipt_tobe_added_rv_item, viewGroup, false);
final ReceiptsViewHolder receiptsViewHolder = new ReceiptsViewHolder(view);
//dialog ini
// ShowDetails();
final Dialog detailedDialog = new Dialog(this.mContext);
detailedDialog.setContentView(R.layout.fragment_details);
receiptsViewHolder.detailsLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//click view dto show the detaileds view of the list item
contract_number = detailedDialog.findViewById(R.id.contract_id_val);
vehicle_number = detailedDialog.findViewById(R.id.vehicle_num_val);
contract_number.setText(contracts.get(receiptsViewHolder.getAdapterPosition()).getContractNumber());
vehicle_number.setText(contracts.get(receiptsViewHolder.getAdapterPosition()).getVehicleNumber());
detailedDialog.show();
detailedDialog.setCancelable(true);
// mListener.onItemClicked(contracts.indexOf(v.getTag()));
}
});
return receiptsViewHolder;
}
#Override
public void onBindViewHolder(#NonNull final ContReceiptsAdapter.ReceiptsViewHolder viewHolder, int i) {
viewHolder.itemView.setTag(contracts.get(i));
// Contract currentItem = contracts.get(i);
viewHolder.tv_contract.setText(contracts.get(i).getContractNumber());
viewHolder.tvVehicleN.setText(contracts.get(i).getVehicleNumber());
viewHolder.tvContractType.setText(contracts.get(i).getContractType());
final Dialog AddItemDialog = new Dialog(mContext);
AddItemDialog.setContentView(R.layout.fragment_add_receipt);
viewHolder.btn_add_item.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// ShowDetails();7
contract_number_ADialog = AddItemDialog.findViewById(R.id.contract_id_val_add);
contract_number_ADialog.setText(contracts.get(viewHolder.getAdapterPosition()).getContractNumber());
vehicle_number_ADialog = AddItemDialog.findViewById(R.id.product_type_val_add);
vehicle_number_ADialog.setText(contracts.get(viewHolder.getAdapterPosition()).getVehicleNumber());
mobile_number_ADialog = AddItemDialog.findViewById(R.id.et_number_add);
mobile_number_ADialog.setText(contracts.get(viewHolder.getAdapterPosition()).getMobileNumber());
checkbox_pending = AddItemDialog.findViewById(R.id.checkbox_pending_add);
checkbox_pending.setChecked(Boolean.parseBoolean(contracts.get(viewHolder.getAdapterPosition()).getPendingState()));
Button btn_cancel_receipt = AddItemDialog.findViewById(R.id.btn_cancel_receipt);
Button btn_add_receipt = AddItemDialog.findViewById(R.id.btn_add_receipt);
et_amount_add = AddItemDialog.findViewById(R.id.et_amount_add);
AddItemDialog.show();
AddItemDialog.setCancelable(true);
btn_cancel_receipt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AddItemDialog.dismiss();
}
});
btn_add_receipt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AddData();
//add item to rv2
AddItemDialog.dismiss();
}
});
}
});
}
#Override
public int getItemCount() {
return contracts.size();
}
private void addAnItem(AddPayments payment) {
payments = ApplicationClass.paymentList;
payments.add(payment);
this.notifyDataSetChanged();
}
private void AddData() {
String c_code = contract_number_ADialog.getText().toString();
String c_mobile = mobile_number_ADialog.getText().toString();
String c_amount = et_amount_add.getText().toString();
AddPayments added_payment = new AddPayments(c_amount, c_code, c_mobile, "true");
payments.add(added_payment);
notifyDataSetChanged();
}
}
My Adapter class for the Second Recyclerview
public class AddPaymentsAdapter extends RecyclerView.Adapter<AddPaymentsAdapter.AddPayViewHolder> {
private ArrayList<AddPayments> payments;
Context mContext;
public AddPaymentsAdapter(Context context, ArrayList<AddPayments> addPayments) {
mContext = context ;
payments = addPayments;
}
public class AddPayViewHolder extends RecyclerView.ViewHolder{
TextView tv_contract_d, tv_amount_d, tv_isPending_d, tv_mobile_num_d;
ImageView btn_delete_item;
ConstraintLayout addedPaymentLayout;
public AddPayViewHolder(#NonNull View itemView) {
super(itemView);
addedPaymentLayout = itemView.findViewById(R.id.added_payment_item_id);
tv_contract_d = itemView.findViewById(R.id.tv_contract_d);
tv_amount_d = itemView.findViewById(R.id.tv_amount_d);
tv_isPending_d = itemView.findViewById(R.id.tv_isPending_d);
tv_mobile_num_d = itemView.findViewById(R.id.tv_mobile_num_d);
btn_delete_item = itemView.findViewById(R.id.btn_delete_item);
}
}
#NonNull
#Override
public AddPaymentsAdapter.AddPayViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.receipt_added_rv_item, viewGroup, false);
final AddPayViewHolder addPayViewHolder= new AddPayViewHolder(view);
return addPayViewHolder;
}
#Override
public void onBindViewHolder(#NonNull AddPaymentsAdapter.AddPayViewHolder addPayViewHolder, final int i) {
AddPayments currentItem = payments.get(i);
addPayViewHolder.itemView.setTag(payments.get(i));
addPayViewHolder.tv_contract_d.setText(payments.get(i).getAddContractCode());
addPayViewHolder.tv_isPending_d.setText(payments.get(i).getAddIsPending());
addPayViewHolder.tv_amount_d.setText(payments.get(i).getAddAmount());
addPayViewHolder.tv_mobile_num_d.setText(payments.get(i).getAddMobile());
addPayViewHolder.btn_delete_item.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//delete function
// Toast.makeText(mContext, "clicked delete icon inHoldernAdapter", Toast.LENGTH_SHORT).show();
removeItem(i);
}
});
}
#Override
public int getItemCount() {
return payments.size();
}
private void removeItem(int i) {
payments.remove(i);
this.notifyItemRemoved(i);
}
}
From your code in the adapter for the first recycler view you are using this method AddData(); to add data. But this method adds data to payments which is the list where you hold the data which you want to show in the second recycler view but have no connection to it(it's just a field of the adapter class).
The this.notifyDataSetChanged() notifies the current adapter for a change in data(first recycler view). You will need to add this data to the second recycler view adapter thou. You aren't doing that right now.
So a way would be to create a method inside of the fragment which takes a list of AddPayments as argument and add this data to the second recycler view adapater. It would be something like this:
Fragment method:
public void addSecondRVData(AddPayments itemToAdd) {
mAddPaymentsAdapter.addNewPayment(itemToAdd);
}
Second recycler view method:
public void addData(AddPayments itemToAdd) {
payments.add(itemToAdd);
this.notifyDataSetChanged():
}
Dont forget to initialize the payment list in the second recycler view.
Then in the first adapter do smth like this:
private ReceiptFragment receiptFragment;
//Constructor
public ContReceiptsAdapter(Context context, ArrayList<Contract> contractList, ReceiptFragment receiptFragment) {
contracts = contractList;
mContext = context;
this.receiptFragment = receiptFragment;
}
//Use this to add elements to second rv
receiptFragment.addSecondRVData(newItem);
Hope this helps although is not the cleanest solution. Check the example I wrote here because can't test it at the moment.

calling a Fragment from a button in a listview Item

I Have a ListView in a Fragment, in my ListView's Item, i have a Button How can i call another Fragment a custom DialogFragment from clicking on the button and keep the item information?
you can Use interface ,
Create an Interface in Adapter class , then set it from owner fragment and when user click on Button call interface Method
Adapter class :
public class TestAdapter extends RecyclerView.Adapter<TestAdapter.ViewHolder> {
private ArrayList<AddressModel> dataset;
private WeakReference<Activity> activity;
private onChecked listener;
public TestAdapter(Activity activity, ArrayList<AddressModel> datasett) {
this.dataset = datasett;
this.activity = new WeakReference<Activity>(activity);
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View rootView = LayoutInflater.from(activity.get()).inflate(R.layout.item_popup_change_address_step_two, null);
TextView tv = (TextView) rootView.findViewById(R.id.title_itemPopUp_ChangeAddress_TV);
tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
tv.setBackground(ContextCompat.getDrawable(parent.getContext(), R.drawable.edittext_style));
RecyclerView.LayoutParams lp = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
rootView.setLayoutParams(lp);
return new ViewHolder(rootView);
}
#Override
public void onBindViewHolder(ViewHolder holder, final int position) {
holder.title.get().setText(dataset.get(position).getTitle());
holder.rootView.get().setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (listener != null) //call interface
listener.chekedListener(dataset.get(position));
}
});
}
#Override
public int getItemCount() {
if (dataset == null)
return 0;
return dataset.size();
}
public void setListener(onChecked listener) {
this.listener = listener;
}
public interface onChecked {
void chekedListener(AddressModel addressClass_serialized);
}
public class ViewHolder extends RecyclerView.ViewHolder {
private WeakReference<TextView> title;
private WeakReference<LinearLayout> rootView;
public ViewHolder(View itemView) {
super(itemView);
title = new WeakReference<TextView>((TextView) itemView.findViewById(R.id.title_itemPopUp_ChangeAddress_TV));
rootView = new WeakReference<LinearLayout>((LinearLayout) itemView.findViewById(R.id.rootCardView_addressItemPopUP_cd));
}
}
}
in fragment :
adapter.setListener(new TestAdapter.onChecked() {
#Override
public void chekedListener(AddressModel addressClass_serialized) {
//Call Dialog Here
}
});
Logic/Steps
Set the Click Listener in List Items.
Step 1:- Make Constructor in DialogFragment/CustomDialog to pass the Information from OnClick of ListItem.
Step 2:- Do whatevet u want in Dialog Class.

Categories

Resources