RecyclerView.setAdapter(androidx.recyclerview.widget.RecyclerView Adapter ADAPTER - java

Tried using recycler view inside recycler view the first adapter works fine but the second adapter never runs
tired fixing it over and over nothing works....help
The second recycler view is retrieving data from Firebase
The Data from firebase is received but never set to the required text views.
Activity
public class ScheduleOrder extends AppCompatActivity implements IFirebaseLoadListener {
DeviceSession deviceSession;
UserSession userSession;
String Device_Id="",User_Id="";
MyItemAdapter myItemAdapter;
IFirebaseLoadListener iFirebaseLoadListener;
List<String> Name;
List<ItemData>itemData;
ItemData itemData1;
RecyclerView my_recycler_view;
DatabaseReference myData;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_schedule_order);
itemData1=new ItemData();
deviceSession = new DeviceSession(getApplicationContext());
Device_Id = deviceSession.getDeviceDetails();
userSession = new UserSession(getApplicationContext());
final HashMap<String, String> user = userSession.getUserDetails();
User_Id = user.get(UserSession.User_Id);
FloatingActionButton myFab = (FloatingActionButton)findViewById(R.id.schedule_add);
myFab.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(),Schedule_Order.class);
startActivity(i);
finish();
}
});
myData=FirebaseDatabase.getInstance().getReference("Cart_Schedule"+"/"+Device_Id);
iFirebaseLoadListener=this;
my_recycler_view=findViewById(R.id.my_recyclr_view);
my_recycler_view.setHasFixedSize(true);
// my_recycler_view1.setHasFixedSize(true);
my_recycler_view.setLayoutManager(new LinearLayoutManager(this));
// my_recycler_view1.setLayoutManager(new LinearLayoutManager(this));
getFirebaseData();
}
private void getFirebaseData() {
myData.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
List<ItemGroup>itemGroups=new ArrayList<>();
Name=new ArrayList<>();
for(DataSnapshot groupSnapShot:dataSnapshot.getChildren()){
ItemGroup itemGroup=new ItemGroup();
itemGroup.setD_Time(groupSnapShot.child("d_Time").getValue(true).toString());
itemGroup.setF_Amount(groupSnapShot.child("f_Amount").getValue(true).toString());
for(DataSnapshot newdatasnap:groupSnapShot.child("listItem").getChildren()){
itemData1.setA_Item_name(newdatasnap.child("a_Item_name").getValue().toString());
itemData1.setB_Quantity(newdatasnap.child("b_Quantity").getValue().toString());
itemData1.setC_Price(newdatasnap.child("b_Quantity").getValue().toString());
Name.add(newdatasnap.child("a_Item_name").getValue().toString());
Name.add(newdatasnap.child("c_Price").getValue().toString());
Name.add(newdatasnap.child("b_Quantity").getValue().toString());
Log.d("JADOO", "onDataChange: ??? "+itemData1.getA_Item_name());
Log.d("JADOO", "onDataChange: ??? "+itemData1.getB_Quantity());
Log.d("JADOO", "onDataChange: ??? "+itemData1.getC_Price());
Log.d("JADOO", "onDataChange: ??? "+Name);}
Log.d("JADOO", "onDataChange: ====================================================");
itemGroups.add(itemGroup);
Name.clear();
}
iFirebaseLoadListener.onFirebaseLoadSuccess(itemGroups);
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
iFirebaseLoadListener.onFirebaseLoadFailed(databaseError.getMessage());
}
});
}
#Override
public void onFirebaseLoadSuccess(List<ItemGroup> itemGroupList) {
Log.d("JADOO", "onDataChange: Setting Adapter");
MyItemGroupAdapter adapter = new MyItemGroupAdapter(ScheduleOrder.this,itemGroupList,itemData1);
my_recycler_view.setAdapter(adapter);
}
#Override
public void onFirebaseLoadFailed(String message) {
}
}
First Adapter Java file
public class MyItemGroupAdapter extends RecyclerView.Adapter<MyItemGroupAdapter.MyViewHolder> {
private Context context;
private List<ItemGroup>dataList;
private ItemData name;
public MyItemGroupAdapter(Context context, List<ItemGroup> dataList,ItemData data) {
this.context = context;
this.dataList = dataList;
this.name=data;
}
#NonNull
#Override
public MyViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View itemView=LayoutInflater.from(context).inflate(R.layout.tempcartlayout,viewGroup,false);
return new MyViewHolder(itemView);
}
#Override
public void onBindViewHolder(#NonNull final MyViewHolder myViewHolder, int i) {
MyItemAdapter itemListAdapter = new MyItemAdapter(context,name);
myViewHolder.timee.setText(dataList.get(i).getD_Time());
myViewHolder.cost.setText(dataList.get(i).getF_Amount());
Log.d("JADOO", "onDataChange: IN MY ITEM GRP ADAPTER "+name.getA_Item_name());
myViewHolder.recycler_view_item_list.setHasFixedSize(true);
myViewHolder.recycler_view_item_list.setLayoutManager(new CustomLinearLayoutManager(context,LinearLayoutManager.VERTICAL,false));
myViewHolder.recycler_view_item_list.setAdapter(itemListAdapter);
myViewHolder.recycler_view_item_list.setNestedScrollingEnabled(false);
myViewHolder.can.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(context, "Button More : "+myViewHolder.timee.getText()+" -- "+myViewHolder.cost.getText(), Toast.LENGTH_SHORT).show();
}
});
}
#Override
public int getItemCount() {
return (dataList != null ? dataList.size() : 0);
}
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView timee,cost;
Button can;
RecyclerView recycler_view_item_list;
public MyViewHolder(#NonNull View itemView) {
super(itemView);
timee=itemView.findViewById(R.id.reorder_time);
cost=itemView.findViewById(R.id.reorder_cost);
can=itemView.findViewById(R.id.button);
Log.d("JADOO", "onDataChange: VIEW HOLDER ");
recycler_view_item_list=itemView.findViewById(R.id.schedulecart);
}}}
Second Adapter File
private Context context;
private ItemData name123;
public MyItemAdapter(Context context, ItemData Item_Name) {
this.context = context;
this.name123=Item_Name;
}
#NonNull
#Override
public MyViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View itemView= LayoutInflater.from(context).inflate(R.layout.cart_list_layout,viewGroup,false);
return new MyViewHolder(itemView);
}
#Override
public void onBindViewHolder(#NonNull MyViewHolder myViewHolder, final int i) {
Log.d("JADOO", "onBindViewHolder: IN MY ITEM ADAPTER");
ItemData itemData=new ItemData();
myViewHolder.m_name.setText(itemData.getA_Item_name());
myViewHolder.m_cost.setText(itemData.getC_Price());
myViewHolder.m_quant.setText(itemData.getB_Quantity());
}
#Override
public int getItemCount() {
return 0;
}
public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView m_name,m_quant,m_cost;
IItemClickListener iItemClickListener;
public void setiItemClickListener(IItemClickListener iItemClickListener){
this.iItemClickListener=iItemClickListener;
}
public MyViewHolder(#NonNull View itemView) {
super(itemView);
m_name=itemView.findViewById(R.id.tv_menu_name1);
m_quant=itemView.findViewById(R.id.quantity);
m_cost=itemView.findViewById(R.id.amount);
itemView.setOnClickListener(this);
}
#Override
public void onClick(View view) {
iItemClickListener.onItemClickListener(view,getAdapterPosition());
}}}
Error
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.food.pilo, PID: 28457
java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.recyclerview.widget.RecyclerView.setAdapter(androidx.recyclerview.widget.RecyclerView$Adapter)' on a null object reference
at com.food.pilo.ScheduleOrder.onFirebaseLoadSuccess(ScheduleOrder.java:161)
at com.food.pilo.ScheduleOrder$2.onDataChange(ScheduleOrder.java:143)
at com.google.firebase.database.Query$1.onDataChange(com.google.firebase:firebase-database##16.0.4:183)
at com.google.firebase.database.core.ValueEventRegistration.fireEvent(com.google.firebase:firebase-database##16.0.4:75)
at com.google.firebase.database.core.view.DataEvent.fire(com.google.firebase:firebase-database##16.0.4:63)
at com.google.firebase.database.core.view.EventRaiser$1.run(com.google.firebase:firebase-database##16.0.4:55)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7156)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:975)```

Your return value is 0 in Second adapter. Please add arraylist to second adapter.
#Override
public int getItemCount() {
return 0;
}

I think my_recycler_view=findViewById(R.id.my_recyclr_view); or
findViewById(R.id.schedulecart); returning null please check on these lines.

Related

write to Firebaseon recycler adapter

I have a recycler view that is being populated by firebase database. On the item.xml I have a button alongside data. On clicking the button on each item, I want to save supplier name in my database. Get error: nullpointer exception as my class Suppliers remains null. What is causing it?
Code
public class recyclerAdapter extends RecyclerView.Adapter<recyclerAdapter.MyViewHolder> {
Context context;
ArrayList<supplierClass> list;
public recyclerAdapter(Context context, ArrayList<supplierClass> list) {
this.context = context;
this.list = list;
}
#NonNull
#Override
public MyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(context).inflate(R.layout.item,parent,false);
return new MyViewHolder(v);
}
#Override
public void onBindViewHolder(#NonNull MyViewHolder holder, int position) {
supplierClass sc = list.get(position);
holder.sName.setText(sc.getsName());
holder.materialName.setText(sc.getMaterialName());
holder.CATEGORY.setText(sc.getCATEGORY());
}
#Override
public int getItemCount() {
return list.size();
}
public static class MyViewHolder extends RecyclerView.ViewHolder{
TextView sName,materialName,CATEGORY;
Button btnSelect;
DatabaseReference ref;
FirebaseUser user= FirebaseAuth.getInstance().getCurrentUser();
public MyViewHolder(#NonNull View itemView) {
super(itemView);
sName = itemView.findViewById(R.id.lblSupplierName);
materialName = itemView.findViewById(R.id.lblMaterial);
CATEGORY = itemView.findViewById(R.id.lblLocation);
btnSelect=itemView.findViewById(R.id.btnSelect);
supplierClass sc=new supplierClass();
String id=user.getUid();
btnSelect.setOnClickListener(v -> {
ref= FirebaseDatabase.getInstance().getReference("dashboard");
ref.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot) {
ref.child(id).setValue(sc.getsName());
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
}
});
});
}
}

Why i can't click my item on recyclerview

I can't click the item on recyclerview, I don't know why, so can somebody help me to clear this problem?
so this in my code in MainActivity, and I call this function in onCreate :
private void showRecyclerList(){
recyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this));
UserAdapter userAdapter = new UserAdapter(this, userModel);
recyclerView.setAdapter(userAdapter);
userAdapter.setOnItemClickCallback(new UserAdapter.OnItemClickCallback() {
#Override
public void onItemClicked(UserModel data) {
Intent intent = new Intent(MainActivity.this, UserDetailActivity.class);
intent.putExtra(UserDetailActivity.EXTRA_DATA, (Parcelable) userModel);
startActivity(intent);
}
});
}
I used the Interface from OnItemClickCallback and I up it on User Adapter, and this is my UserAdapter:
public class UserAdapter extends RecyclerView.Adapter<UserAdapter.ListViewHolder> {
private Context context;
private List<UserModel> userData = new ArrayList<>();
private OnItemClickCallback onItemClickCallback;
public void setOnItemClickCallback(final OnItemClickCallback onItemClickCallback) {
this.onItemClickCallback = onItemClickCallback;
}
public static final String DATA_EXTRA = "data_extra";
public UserAdapter(Context context, List<UserModel> userData) {
this.context = context;
this.userData = userData;
}
#NonNull
#Override
public ListViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.item_user, parent,false);
return new ListViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull ListViewHolder holder, int position) {
//UserModel user = userData.get(position);
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onItemClickCallback.onItemClicked(userData.get(holder.getAdapterPosition()));
}
});
Glide.with(context)
.load(userData.get(position).getAvatarUrl())
.into(holder.imgPhoto);
holder.tvName.setText(userData.get(position).getLogin());
}
#Override
public int getItemCount() {
return userData.size();
}
public class ListViewHolder extends RecyclerView.ViewHolder
{
ImageView imgPhoto;
TextView tvName;
public ListViewHolder(View itemView) {
super(itemView);
imgPhoto = itemView.findViewById(R.id.iv_avatar);
tvName = itemView.findViewById(R.id.tv_name);
}
}
public interface OnItemClickCallback {
void onItemClicked(UserModel data);
}
and this is my error message :
java.lang.NullPointerException: Attempt to invoke interface method 'void com.dicoding.githubuserwithapi.UserAdapter$OnItemClickCallback.onItemClicked(com.dicoding.githubuserwithapi.model.UserModel)' on a null object reference
at com.dicoding.githubuserwithapi.UserAdapter$1.onClick(UserAdapter.java:60)
at android.view.View.performClick(View.java:7357)
at android.view.View.performClickInternal(View.java:7334)
at android.view.View.access$3600(View.java:808)
at android.view.View$PerformClick.run(View.java:28200)
at android.os.Handler.handleCallback(Handler.java:907)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7478)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:549)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:941)
I/Process: Sending signal. PID: 9602 SIG: 9
I have an error in UserAdapter line 60
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
**onItemClickCallback.onItemClicked(userData.get(holder.getAdapterPosition()));**
}
});
on my UserModel class, there is only the data from the user and I used Parcelable on it.
I think I can't get the position, but I don't know where can I fix it, so can u guys help me with this problem..
When creating LayoutInflater you should use parent.context.
View view = LayoutInflater.from(parent.context).inflate(R.layout.item_user, parent,false);
Why are you calling adapter's getAdapterPosition() if position is already available to you as a parameter of onBindViewHolder()?
onItemClickCallback.onItemClicked(userData.get(position));
When you're defining that click listener you're not using the data you passed to it.
userAdapter.setOnItemClickCallback(new UserAdapter.OnItemClickCallback() {
#Override
public void onItemClicked(UserModel data) {
Intent intent = new Intent(MainActivity.this, UserDetailActivity.class);
intent.putExtra(UserDetailActivity.EXTRA_DATA, (Parcelable) **userModel**);
startActivity(intent);
}
});

Add ClickListener to RecyclerView realtime database

I need to add ClickListener to RecyclerView in my project but I couldn't.
I tried for days and failed :(
Help me, please.
(the variables are in Italian)
RecyclerView class:
public class ListaNonConsegnate extends AppCompatActivity{
private androidx.recyclerview.widget.RecyclerView recyclerView;
EditText searchBox;
DatabaseReference nonConsegnate;
private ArrayList<dati> list;
private String nomeUtente;
private SwipeRefreshLayout swipeRefreshLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lista_non_consegnate);
Bundle extras = getIntent().getExtras();
nomeUtente = extras.getString("KEY_nomeUtente"); //variable from the previous activity
searchBox = findViewById(R.id.searchBox); //for filtered list
nonConsegnate = FirebaseDatabase.getInstance().getReference().child(nomeUtente + "/00-b-Non consegnate"); //path on the database
recyclerView = findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
searchBox.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
ArrayList<dati> myList = new ArrayList<>();
//add filter
AdapterClass adapterClass = new AdapterClass(myList);
recyclerView.setAdapter(adapterClass);
}
});
}
#Override
protected void onStart() {
super.onStart();
if (nonConsegnate != null) {
nonConsegnate.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot) {
if (snapshot.exists()) {
list = new ArrayList<>();
for (DataSnapshot ds : snapshot.getChildren()) {
list.add(ds.getValue(dati.class));
}
AdapterClass adapterClass = new AdapterClass(list);
recyclerView.setAdapter(adapterClass);
}
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
Toast.makeText(ListaNonConsegnate.this, error.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
}
}
Adapter class:
public class AdapterClass extends RecyclerView.Adapter<AdapterClass.MyViewHolder>{
ArrayList<dati> list;
public AdapterClass(ArrayList<dati> list){
this.list = list;
}
#NonNull
#Override
public MyViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_items, viewGroup, false);
return new MyViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull MyViewHolder holder, int position) {
holder.bolla.setText(list.get(position).getDDT());
}
#Override
public int getItemCount() {
return list.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder{
TextView bolla;
public MyViewHolder(#NonNull View itemView) {
super(itemView);
bolla = itemView.findViewById(R.id.textViewBolla);
}
}
}
(OnClick will have to open a new activity by passing it the "nomeUtente" and "DDT" variables)
Thank you.
(StackOverflow tells me I need to add more details but there's nothing more to say so I write useless sentences) lol
1- Add this Code To Your Activity :
public void onClickCalled() {
// Your Code
}
2- and this in your adapter :
holder.nomeUtente.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
((YourNewActivity) context).onClickCalled();
}
});

Android Studio RecyclerView: No adapter attached; skipping layout

Help
the problem is that when running the application nothing appears in my recyclerview, only error android studio : E/RecyclerView: No adapter attached; skipping layout
Salon ListActivity
public class SalonListActivity extends AppCompatActivity implements IOnLoadCountSalon, IBranchLoadListener {
IOnLoadCountSalon iOnLoadCountSalon;
IBranchLoadListener iBranchLoadListener;
AlertDialog dialog;
#BindView(R.id.txt_salon_count)
TextView txt_salon_count;
#BindView(R.id.recycler_salon)
RecyclerView recycler_salon;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_salon_list);
ButterKnife.bind(this);
initView();
init();
loadSalonBaseOnCity(Common.state_name);
}
private void loadSalonBaseOnCity(String name) {
dialog.show();
FirebaseFirestore.getInstance().collection("AllSalon")
.document(name)
.collection("Branch")
.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
#Override
public void onComplete(#NonNull Task<QuerySnapshot> task) {
if(task.isSuccessful())
{
List<Salon> salons = new ArrayList<>();
iOnLoadCountSalon.onLoadCountSalonSuccess(task.getResult().size());
for(DocumentSnapshot salonSnapShot : task.getResult())
{
Salon salon = salonSnapShot.toObject(Salon.class);
salons.add(salon);
}
iBranchLoadListener.onBranchLoadSuccess(salons);
}
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
iBranchLoadListener.onBranchLoadFailed(e.getMessage());
}
});
}
private void init() {
dialog = new SpotsDialog.Builder().setContext(this).setCancelable(false).build();
iOnLoadCountSalon=this;
iBranchLoadListener = this;
}
private void initView() {
recycler_salon.setHasFixedSize(true);
recycler_salon.setLayoutManager(new GridLayoutManager(this,2));
recycler_salon.addItemDecoration(new SpacesItemDecoration(8));
}
#Override
public void onLoadCountSalonSuccess(int count) {
txt_salon_count.setText(new StringBuilder("Cantidad Salones (").append(count).append(")"));
}
#Override
public void onBranchLoadSuccess(List<Salon> salonList) {
MySalonAdapter salonAdapter = new MySalonAdapter(this,salonList);
recycler_salon.setAdapter(salonAdapter);
dialog.dismiss();
}
#Override
public void onBranchLoadFailed(String message) {
Toast.makeText(this,message, Toast.LENGTH_SHORT).show();
dialog.dismiss();
}}`
Salon Adapter
public class MySalonAdapter extends RecyclerView.Adapter<MySalonAdapter.MyViewHolder> {
Context context;
List<Salon> salonList;
List<CardView> cardViewList;
LocalBroadcastManager localBroadcastManager;
public MySalonAdapter(Context context, List<Salon> salonList) {
this.context = context;
this.salonList = salonList;
cardViewList = new ArrayList<>();
localBroadcastManager = LocalBroadcastManager.getInstance(context);
}
#NonNull
#Override
public MyViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View itemView = LayoutInflater.from(context).inflate(R.layout.layout_salon, viewGroup, false);
return new MyViewHolder(itemView);
}
#Override
public void onBindViewHolder(#NonNull final MyViewHolder myViewHolder, int i) {
myViewHolder.txt_salon_name.setText(salonList.get(i).getName());
myViewHolder.txt_salon_address.setText(salonList.get(i).getAddress());
if (!cardViewList.contains(myViewHolder.card_salon))
cardViewList.add(myViewHolder.card_salon);
myViewHolder.setiRecyckerItemSelectedListener(new IRecyclerItemSelectedListener() {
#Override
public void onItemSelected(View view, int position) {
}
});
}
#Override
public int getItemCount() {
return salonList.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
IRecyclerItemSelectedListener iRecyckerItemSelectedListener;
TextView txt_salon_name, txt_salon_address;
CardView card_salon;
public void setiRecyckerItemSelectedListener(IRecyclerItemSelectedListener iRecyckerItemSelectedListener) {
this.iRecyckerItemSelectedListener = iRecyckerItemSelectedListener;
}
public MyViewHolder(#NonNull View itemView) {
super(itemView);
card_salon = (CardView) itemView.findViewById(R.id.card_salon);
txt_salon_name = (TextView) itemView.findViewById(R.id.txt_salon_name);
txt_salon_address = (TextView) itemView.findViewById(R.id.txt_salon_address);
itemView.setOnClickListener(this);
}
#Override
public void onClick(View v) {
iRecyckerItemSelectedListener.onItemSelected(v, getAdapterPosition());
}
}
}
Sorry for my English I hope you can help me, I will be very grateful
The message is saying that the adapter is not attached at the time the layout is rendered. Which is true because you've attached it within a callback method
Instead, you can attach it immediately if you create a field of loaded salons and the adapter
IBranchLoadListener iBranchLoadListener;
List<Salon> mSalons = new ArrayList<>();
RecyclerView.Adapter<MySalonAdapter.MyViewHolder> mAdapter;
Use that list in the callback rather than make a new list
if(task.isSuccessful())
{
mSalons.clear();
List<DocumentSnapshot> result = task.getResult();
iOnLoadCountSalon.onLoadCountSalonSuccess(result.size());
for(DocumentSnapshot salonSnapShot : result)
{
Salon salon = salonSnapShot.toObject(Salon.class);
mSalons.add(salon);
}
iBranchLoadListener.onBranchLoadSuccess(mSalons);
}
And then initialize and attach your adapter immediately within the onCreate or init methods rather than wait for the callback
Within onBranchLoadSuccess you should notify the adapter that it needs updated

Passing data from RecyclerView.Adapter to fragment onClick

I'm trying to figure out how to get data from a clicked item in a RecyclerView to a listview in a Fragment. I can't seem to figure out how to do this, as I can't get my bundle to work.
I've tried various solutions offered here on Stackoverflow, but none of them have worked. I have managed to get the info from the recyclerview, but I am stuck trying to figure out how I can pass it to the fragment. Can someone please help me?
The Adapter class:
public class FoodAdapter extends RecyclerView.Adapter<FoodAdapter.ViewHolder> {
private Context context;
ArrayList<FoodActivity> list;
public FoodAdapter(ArrayList<FoodActivity> list){
this.list = list;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.listview_item_food, parent, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull final ViewHolder holder, final int position) {
holder.foods.setText(list.get(position).getName());
holder.carbo.setText(list.get(position).getCarbohydrates());
holder.protein.setText(list.get(position).getProtein());
holder.fats.setText(list.get(position).getFats());
//Get items from recyclerview when user clicks them. Then send them to FoodFragment
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String foods = list.get(position).getName();
String carbo = list.get(position).getCarbohydrates();
String protein = list.get(position).getProtein();
String fats = list.get(position).getFats();
Toast.makeText(v.getContext(), "test: " + foods, Toast.LENGTH_SHORT).show();
}
});
}
#Override
public int getItemCount() {
return list.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
TextView foods, carbo, fats, protein;
public ViewHolder(View itemView) {
super(itemView);
carbo = itemView.findViewById(R.id.carbo);
protein = itemView.findViewById(R.id.protein);
fats = itemView.findViewById(R.id.fats);
foods = itemView.findViewById(R.id.food);
}
}
}
The Fragment where the data comes from:
public class TrackingFragment extends Fragment {
DatabaseReference databaseReference;
ArrayList<FoodActivity> list;
RecyclerView recyclerView;
SearchView searchView;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_tracking, container, false);
databaseReference = FirebaseDatabase.getInstance().getReference().child("foods");
recyclerView = view.findViewById(R.id.rv);
searchView = view.findViewById(R.id.searchFood);
return view;
}
#Override
public void onStart() {
super.onStart();
if(databaseReference != null){
databaseReference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
if(dataSnapshot.exists()){
list = new ArrayList<>();
for(DataSnapshot ds : dataSnapshot.getChildren()){
list.add(ds.getValue(FoodActivity.class));
}
FoodAdapter adapter = new FoodAdapter(list);
recyclerView.setAdapter(adapter);
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
Toast.makeText(getActivity(), databaseError.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
if(searchView != null){
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
search(newText);
return true;
}
});
}
}
private void search(String str){
ArrayList<FoodActivity> searchList = new ArrayList<>();
for(FoodActivity object : list){
if(object.getName().toLowerCase().contains(str.toLowerCase())){
searchList.add(object);
}
}
FoodAdapter foodAdapter = new FoodAdapter(searchList);
recyclerView.setAdapter(foodAdapter);
}
}
And the class where it needs to go:
public class FoodFragment extends Fragment {
ImageButton addFood;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_food, container, false);
addFood = view.findViewById(R.id.addFood);
addFood.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
FragmentTransaction fr = getFragmentManager().beginTransaction();
fr.replace(R.id.fragment_container, new TrackingFragment());
fr.addToBackStack(null).commit();
}
});
return view;
}
}
The most simple is to add variables you need to your activity, set their values with onClick() and then retrieve data in other fragment:
in activity:
String foods;
public String getFoods() {
return foods;
}
public void setFoods(String foods) {
this.foods = foods;
}
in adapter:
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
((YourActivity)getActivity()).setFoods("Any value");
}
});
in destination fragment:
String foods = ((YourActivity)getActivity()).getFoods();
Add an interface to your adapter as follow
public class FoodAdapter extends RecyclerView.Adapter<FoodAdapter.ViewHolder> {
interface OnClickListener {
void onClick(FoodActivity clickedItem);
}
private OnClickListener mCallback;
private ArrayList<FoodActivity> list;
public FoodAdapter(ArrayList<FoodActivity> list){
this.list = list;
}
public void setOnClickListener(OnClickListener callback) {
mCallback = callback;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.listview_item_food, parent, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull final ViewHolder holder, final int position) {
holder.foods.setText(list.get(position).getName());
holder.carbo.setText(list.get(position).getCarbohydrates());
holder.protein.setText(list.get(position).getProtein());
holder.fats.setText(list.get(position).getFats());
//Get items from recyclerview when user clicks them. Then send them to FoodFragment
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mCallback != null)
mCallback.onClick(list.get(position));
}
});
}
#Override
public int getItemCount() {
return list.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
TextView foods, carbo, fats, protein;
public ViewHolder(View itemView) {
super(itemView);
carbo = itemView.findViewById(R.id.carbo);
protein = itemView.findViewById(R.id.protein);
fats = itemView.findViewById(R.id.fats);
foods = itemView.findViewById(R.id.food);
}
}
}
Android sometimes is a little complicated, when it involves the full stack:
01 RecyclerView Adapter
/** 01. Some Adapter */
public class SomeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private WeakReference<Context> mContext;
/** Constructor */
public SomeAdapter(#NonNull Context context) {
this.mContext = new WeakReference<>(context);
}
#NonNull
protected Context getContext() {
return this.mContext.get();
}
/** Call to Activity from within the adapter. */
private void someMethod() {
SomeActivity activity = (SomeActivity) getContext();
synchronized(activity) {activity.showLoginDialog();}
}
}
02 AppCompatActivity
/** 02. Some Activity */
public class SomeActivity extends AppCompatActivity {
#Nullable
private BaseFragment currentFragment = null;
/** Constructor */
public SomeActivity() {}
public void setCurrentFragment(Fragment fragment) {
this.currentFragment = fragment;
}
/** Call to the current Fragment. */
public void someMethod() {
if (currentFragment != null) {
currentFragment.someMethod();
}
}
}
03 Fragment
/** Some Fragment */
public class SomeFragment extends Fragment {
/** Constructor */
public SomeFragment() {}
#Override
public void onAttach(#NonNull Context context) {
super.onAttach(context);
((BaseActivity) context).setCurrentFragment(this);
}
#Override
public void onDetach() {
super.onDetach();
((BaseActivity) getActivity()).setCurrentFragment(this);
}
}
To get your data from FoodAdapter to TrackingFragment, you can add TrackingFragment (or an interface it implements, ideally) as a parameter to the FoodAdapter constructor, then use that instance to forward your ViewHolder clicks to TrackingFragment for handling.
To get your data from TrackingFragment to FoodFragment, use the setTargetFragment/onActivityResult pattern for fragment <-> fragment communication. This answer has an example: https://stackoverflow.com/a/13733914/3238938

Categories

Resources