Method cannot resolve in Android - java

I have a problem where the method "deleteItem" cannot be resolved at this line adapters.deleteItem(viewHolder.getAdapterPosition());even I have created that method at public void deleteItem(int position).Do I placed the method wrongly?
Java codes:
final FirestoreRecyclerAdapter adapters;
fStore = FirebaseFirestore.getInstance();
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
final String current = user.getUid();
Query query = fStore.collection("Users").document(user.getUid()).collection("Diary");
FirestoreRecyclerOptions<ModelClass> options = new FirestoreRecyclerOptions.Builder<ModelClass>()
.setQuery(query, ModelClass.class)
.build();
adapters = new FirestoreRecyclerAdapter<ModelClass, ModelViewHolder>(options) {
#NonNull
#Override
public ModelViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_diary, parent, false);
return new ModelViewHolder(view);
}
#Override
protected void onBindViewHolder(#NonNull ModelViewHolder holder, int position,
#NonNull ModelClass model) {
holder.date.setText(model.getDate());
holder.note.setText(model.getText());
holder.time.setText(model.getTime());
holder.divider.setText(model.getDivider());
}
public void deleteItem(int position) {
getSnapshots().getSnapshot(position).getReference().delete();
}
};
adapters.startListening();
recyclerView.setLayoutManager(new LinearLayoutManager(this));
SpacingItemDecoration itemDecorator = new SpacingItemDecoration(20);
recyclerView.addItemDecoration(itemDecorator);
recyclerView.setAdapter(adapters);
new ItemTouchHelper(new ItemTouchHelper.SimpleCallback(0,
ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) {
#Override
public boolean onMove(#NonNull RecyclerView recyclerView, #NonNull RecyclerView.ViewHolder viewHolder, #NonNull RecyclerView.ViewHolder target) {
return false;
}
#Override
public void onSwiped(#NonNull RecyclerView.ViewHolder viewHolder, int direction) {
adapters.deleteItem(viewHolder.getAdapterPosition());
}
}).attachToRecyclerView(recyclerView);
}
public class ModelViewHolder extends RecyclerView.ViewHolder {
TextView date, time, note, divider;
public ModelViewHolder(#NonNull View itemView) {
super(itemView);
date = itemView.findViewById(R.id.date_text);
time = itemView.findViewById(R.id.time_textview);
note = itemView.findViewById(R.id.note_textview);
divider = itemView.findViewById(R.id.line_textview);
}
}

You can't do it since adapters is a FirestoreRecyclerAdapter which doesn't contain this method.
Just extend the FirestoreRecyclerAdapter and use it.
public class CustomAdapter extends FirestoreRecyclerAdapter<....>{
public CustomAdapter(#NonNull FirestoreRecyclerOptions<....> options) {
super(options);
}
public void deleteItem(int i){ ... }
#Override
protected void onBindViewHolder(){....}
#Override
public ModelViewHolder onCreateViewHolder(){ ...}
}

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) {
}
});
});
}
}

How to take value from dynamic edit text in Adapter

I have a RecyclerView with one edit text in Adapter where the editText is dynamic based on object present inside the array, How can i take the value of each edittext ?
public class AdapterFastagEditList extends RecyclerView.Adapter<AdapterFastagEditList.ViewHolder> {
Activity activity;
List<JSONObject> list;
OnItemViewClickListener onItemViewClickListener;
public AdapterFastagEditList(Activity activity, List<JSONObject> list, OnItemViewClickListener onItemViewClickListener) {
this.activity = activity;
this.list = list;
this.onItemViewClickListener = onItemViewClickListener;
}
public void setOnItemViewClickListener(OnItemViewClickListener onItemViewClickListener) {
this.onItemViewClickListener = onItemViewClickListener;
}
#NonNull
#Override
public AdapterFastagEditList.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
// Initialize view for recyclerview
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
AdapterDynamicEdittextFastagBinding binding = AdapterDynamicEdittextFastagBinding.inflate(inflater, parent, false);
return new ViewHolder(binding.getRoot(), binding);
}
JSONObject object = new JSONObject();
#Override
public void onBindViewHolder(#NonNull AdapterFastagEditList.ViewHolder holder, int position) {
object = list.get(position);
AdapterDynamicEdittextFastagBinding binding = holder.binding;
try {
binding.TextInputContent.setHint(object.getString("BdetailsFieldName"));
binding.EditTextContent.setId(View.generateViewId());
} catch (JSONException e) {
e.printStackTrace();
}
}
#Override
public int getItemCount() {
return list.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
AdapterDynamicEdittextFastagBinding binding;
public ViewHolder(#NonNull View itemView, AdapterDynamicEdittextFastagBinding binding) {
super(itemView);
this.binding = binding;
}
}
}
public class ViewHolder extends RecyclerView.ViewHolder {
AdapterDynamicEdittextFastagBinding binding;
public ViewHolder(#NonNull View itemView, AdapterDynamicEdittextFastagBinding binding) {
super(itemView);
this.binding = binding;
binding.yourEdittext.addTextChangedListener(new TextWatcher() {
#Override
public void afterTextChanged(Editable s) {}
#Override
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start,
int before, int count) {
//s.toString() gives you changes
//getAdapterPosition() also gives you changed item index
}
});
}
}

get int data from firestore

im trying to get int data from firestore but before that app crashes i dont know why when i try it as string it work i get it and i can list it but when i try it int app crashes even before get data
here is my code second part is my adapter logchat said 2 line is wrong i mark it with stairs for u
FirebaseFirestore firestore;
ArrayList<String> useremaillist;
ArrayList<Integer> userpointlist;
SkorAdapter skorAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_skor_board);
firestore=FirebaseFirestore.getInstance();
useremaillist=new ArrayList<>();
userpointlist=new ArrayList<>();
getscore();
RecyclerView skorboardrecyclerView=findViewById(R.id.recyclerView);
skorboardrecyclerView.setLayoutManager(new LinearLayoutManager(this));
skorAdapter=new SkorAdapter(useremaillist,userpointlist);
skorboardrecyclerView.setAdapter(skorAdapter);
}
public void getscore(){
CollectionReference collectionReference=firestore.collection("Kullanıcılar ve Skorlar");
collectionReference.orderBy("skor", Query.Direction.DESCENDING).addSnapshotListener(new EventListener<QuerySnapshot>() {
#Override
public void onEvent(#Nullable QuerySnapshot value, #Nullable FirebaseFirestoreException error) {
userpointlist.clear();
useremaillist.clear();
if(error!=null){
Log.e("tag",error.getLocalizedMessage());
}
if(value!=null){
for(DocumentSnapshot snapshot:value.getDocuments()){
Map<String,Object> userdatat=snapshot.getData();
String emailt=(String) userdatat.get("email");
int skor=(int) userdatat.get("skor");
useremaillist.add(emailt);
userpointlist.add(skor);
skorAdapter.notifyDataSetChanged();
}
}
}
});
}
** public class SkorAdapter extends RecyclerView.Adapter<SkorAdapter.Holder> {
private ArrayList<String> emaillistforadapter;
private ArrayList<Integer> pointlistforadapter;
public SkorAdapter(ArrayList<String> emaillistforadapter,ArrayList<Integer> pointlistforadapter ) {
this.emaillistforadapter = emaillistforadapter;
this.pointlistforadapter = pointlistforadapter;
}
#NonNull
#Override
public Holder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
LayoutInflater layoutInflater=LayoutInflater.from(parent.getContext());
View view=layoutInflater.inflate(R.layout.skorboard_row,parent,false);
return new Holder(view);
}
#Override
public void onBindViewHolder(#NonNull SkorAdapter.Holder holder, int position) {
holder.useremailtext.setText(emaillistforadapter.get(position));
** holder.userpointtext.setText(pointlistforadapter.get(position));
}
#Override
public int getItemCount() {
return emaillistforadapter.size();
}
public class Holder extends RecyclerView.ViewHolder {
TextView useremailtext;
TextView userpointtext;
public Holder(#NonNull View itemView) {
super(itemView);
useremailtext=itemView.findViewById(R.id.kullanıcımailtext);
userpointtext=itemView.findViewById(R.id.kullanıcıpointtext);
}
}
The problem is in the adapter then.
You have a private ArrayList<Integer> pointlistforadapter; and the problem is that you are doing holder.userpointtext.setText(pointlistforadapter.get(position)); and setText() accepts a CharSeq and it lets you add an Int in paramter because it also has this signature :
And since it's not a resid it crashes.
What you can do is :
holder.userpointtext.setText(pointlistforadapter.get(position).toString());

Android Studio: Deleting Item from ChildRecyclerView

I'm currently building a schedule app where users can input information, and the app will organize the information in a list with each day of the week as the header with each corresponding activity under the day. I'm using a parent child recyclerview to do this (i.e. each day of the week is a part of the parent recyclerview and each activity in the schedule is part of the child recyclerview). Right now I'm trying to add a swipe to delete information in the child recyclerview. Does anyone know how I would go about doing this using the parent child recyclerview configuration, where I have two different adapters for each recyclerview?
Code for Parent RecyclerView:
public class MainRecyclerViewAdapter extends RecyclerView.Adapter<MainRecyclerViewAdapter.ViewHolder> {
ArrayList<DayHeader> weekList;
private Context mContext;
public MainRecyclerViewAdapter(Context mContext, ArrayList<DayHeader> weekList) {
this.mContext = mContext;
this.weekList = weekList;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
View view = layoutInflater.inflate(R.layout.dayofweekheader,parent,false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
DayHeader section = weekList.get(position);
String nameofday = section.getDayName();
ArrayList<medinfo> meditems = section.getmMedItems();
holder.weekdayname.setText(nameofday);
MedicationAdapter childrecyclerAdapter = new MedicationAdapter(mContext,meditems);
holder.childrecyclerview.setAdapter(childrecyclerAdapter);
}
#Override
public int getItemCount() {
return weekList.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
TextView weekdayname;
RecyclerView childrecyclerview;
public ViewHolder(#NonNull View itemView) {
super(itemView);
weekdayname = itemView.findViewById(R.id.weekdayheader);
childrecyclerview = itemView.findViewById(R.id.childrecyclerview);
}
}
}
Code for Child RecyclerView:
public class MedicationAdapter extends RecyclerView.Adapter<MedicationAdapter.MedicationViewHolder>{
private ArrayList<medinfo> mMedList;
private Context mContext;
public static class MedicationViewHolder extends RecyclerView.ViewHolder {
public TextView mMedname;
public TextView mTime;
public TextView mNumbPills;
public ImageView popup;
public MedicationViewHolder(#NonNull View itemView) {
super(itemView);
mMedname = itemView.findViewById(R.id.medname);
mTime = itemView.findViewById(R.id.time);
mNumbPills = itemView.findViewById(R.id.numberofpills);
popup = itemView.findViewById(R.id.menumore);
}
}
public MedicationAdapter(Context mContext, ArrayList<medinfo> medList) {
this.mContext = mContext;
this.mMedList = medList;
}
#NonNull
#Override
public MedicationViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.example_item,parent,false);
MedicationViewHolder mvh = new MedicationViewHolder(v);
return mvh;
}
#Override
public void onBindViewHolder(#NonNull MedicationViewHolder holder, int position) {
medinfo currentItem = mMedList.get(position);
String medName = "Medication: " + currentItem.getmMedName();
holder.mMedname.setText(medName);
String medTime = "Time: " + currentItem.getmTime();
holder.mTime.setText(medTime);
String numbPills = "# of Pills: " + currentItem.getmNumbPills();
holder.mNumbPills.setText(numbPills);
}
#Override
public int getItemCount() {
return mMedList.size();
}
public void removeItem(int position) {
mMedList.remove(position);
notifyItemRemoved(position);
notifyItemRangeChanged(position,mMedList.size());
}
}
I also have this SwipeItem class:
public class SwipeItem extends ItemTouchHelper.SimpleCallback {
MedicationAdapter mMedAdapter;
SwipeItem(MedicationAdapter mMedAdapter){
super(0,ItemTouchHelper.LEFT|ItemTouchHelper.RIGHT);
this.mMedAdapter = mMedAdapter;
}
#Override
public boolean onMove(#NonNull RecyclerView recyclerView, #NonNull RecyclerView.ViewHolder viewHolder, #NonNull RecyclerView.ViewHolder target) {
return false;
}
#Override
public void onSwiped(#NonNull RecyclerView.ViewHolder viewHolder, int direction) {
int position = viewHolder.getBindingAdapterPosition();
this.mMedAdapter.removeItem(position);
}
}
So how can I implement the SwipeItem in my adapter codes to delete each child item?

Getting onSwipe event from a nested RecyclerView to the parent Fragment

I'm developping an app in which my main activity hosts different fragments via a SectionsPageAdapter. My question is about one of those fragments that roughtly looks like the image bellow:
My innerRecyclers lists differents items that I want to be able to remove by swiping and make the user validate the deletion via a DialogFragment. I was able to implement the swipe action but what I'm struggly to do is to implement a listener in the OuterRecycler for OnItemTouch so I can use an interface and send the action back to my fragment which will dispay my DialoFragment afterwards.
The code of my OuterRecycler currently looks like that:
public class OuterRecyclerAdapter extends RecyclerView.Adapter<OuterRecyclerAdapter.InsideRecyclerHolder> {
private final Context mContext;
private final String mText;
private final ArrayList<MainViewArrayListMasterList> mDataArrayList;
OuterRecyclerAdapter(Context context, ArrayList<MainViewArrayListMasterList> DataArrayList, String text) {
mContext = context;
mDataArrayList = RecyclerArrayListType;
mText = text;
}
#NonNull
#Override
public OuterRecyclerAdapter.InsideRecyclerHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
LayoutInflater myInflater = LayoutInflater.from(mContext);
View myOwnView = myInflater.inflate(R.layout.outer_recycler, parent, false);
return new OuterRecyclerAdapter.InsideRecyclerHolder(myOwnView);
}
#Override
public void onBindViewHolder(#NonNull OuterRecyclerAdapter.InsideRecyclerHolder holder, int position) {
MainViewArrayListMasterList currentItem = mDataArrayList.get(position);
holder.CardTitle.setText(String.valueOf(currentItem.getmYear()));
LinearLayoutManager mLayoutManager = new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false);
holder.CardRecycler.setLayoutManager(mLayoutManager);
InnerRecyclerAdapter historyInnerView_Adapter = new InnerRecyclerAdapter(mContext, currentItem.getmArrayListStored(), mText);
new ItemTouchHelper(ListHistoryViewSwiped).attachToRecyclerView(holder.CardRecycler);
holder.CardRecycler.setAdapter(historyInnerView_Adapter);
}
#Override
public int getItemCount() {
return mDataArrayList.size();
}
static class InsideRecyclerHolder extends RecyclerView.ViewHolder {
final TextView CardTitle;
final RecyclerView CardRecycler;
InsideRecyclerHolder(#NonNull View itemView) {
super(itemView);
CardTitle = itemView.findViewById(R.id.TitleText);
CardRecycler = itemView.findViewById(R.id.Inside_Recycler);
}
}
ItemTouchHelper.SimpleCallback ListHistoryViewSwiped = new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.RIGHT) {
#Override
public boolean onMove(#NonNull RecyclerView recyclerView, #NonNull RecyclerView.ViewHolder viewHolder, #NonNull RecyclerView.ViewHolder target) {
return false;
}
#Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
}
};
}
Is OnItemTouchListener the good way to acheive this ? Or do I have to create a new callback method ?
I actually circumvented this issue by calling the DialogFragment from my OuterRecycler:
public class OuterRecyclerAdapter extends RecyclerView.Adapter<OuterRecyclerAdapter.InsideRecyclerHolder> {
private final Context mContext;
private final String mText;
private final ArrayList<MainViewArrayListMasterList> mDataArrayList;
private FragmentManager mFragmentManager;
OuterRecyclerAdapter(Context context, FragmentManager FragmentManager, ArrayList<MainViewArrayListMasterList> DataArrayList, String text) {
mContext = context;
mFragmentManager = FragmentManager;
mDataArrayList = RecyclerArrayListType;
mText = text;
}
#NonNull
#Override
public OuterRecyclerAdapter.InsideRecyclerHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
LayoutInflater myInflater = LayoutInflater.from(mContext);
View myOwnView = myInflater.inflate(R.layout.outer_recycler, parent, false);
return new OuterRecyclerAdapter.InsideRecyclerHolder(myOwnView);
}
#Override
public void onBindViewHolder(#NonNull OuterRecyclerAdapter.InsideRecyclerHolder holder, int position) {
MainViewArrayListMasterList currentItem = mDataArrayList.get(position);
holder.CardTitle.setText(String.valueOf(currentItem.getmYear()));
LinearLayoutManager mLayoutManager = new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false);
holder.CardRecycler.setLayoutManager(mLayoutManager);
InnerRecyclerAdapter historyInnerView_Adapter = new InnerRecyclerAdapter(mContext, currentItem.getmArrayListStored(), mText);
new ItemTouchHelper(new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.RIGHT) {
#Override
public boolean onMove(#NonNull RecyclerView recyclerView, #NonNull RecyclerView.ViewHolder viewHolder, #NonNull RecyclerView.ViewHolder target) {
return false;
}
#Override
public void onSwiped(#NonNull RecyclerView.ViewHolder viewHolder, int direction) {
openMyDialog(currentItem.getmArrayListStored().get(viewHolder.getAdapterPosition()));
}
}).attachToRecyclerView(holder.CardRecycler);
holder.CardRecycler.setAdapter(historyInnerView_Adapter);
}
#Override
public int getItemCount() {
return mDataArrayList.size();
}
static class InsideRecyclerHolder extends RecyclerView.ViewHolder {
final TextView CardTitle;
final RecyclerView CardRecycler;
InsideRecyclerHolder(#NonNull View itemView) {
super(itemView);
CardTitle = itemView.findViewById(R.id.TitleText);
CardRecycler = itemView.findViewById(R.id.Inside_Recycler);
}
}
private void openMyDialog(MainViewArrayListRecycler itemToDelete) {
MyDialog myDialog = MyDialog.newInstance(itemToDelete);
myDialog.show(mFragmentManager, "MyDialogDialog");
}
}

Categories

Resources