I got an error, for not getting data from firebase, I don't know why but I try to set the key to my modeldata. And when I try to put it on Recycleview. I got an error when I tried to get the data.
Take a look at my code.
ListActivity
public class ListActivity extends BaseActivity {
private DatabaseReference databaseReference;
private RecyclerView labelRecycleView;
private ArrayList<DataFirebase> listLabel;
private listAdapter listAdapter;
private Button bInput;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
findView();
setDataToList();
initListener();
putLayoutManager();
}
private void setDataToList() {
simpleway.progressDialog(ListActivity.this);
databaseReference.child("Username").addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
listLabel = new ArrayList<>();
for (DataSnapshot dataSnap : dataSnapshot.getChildren()) {
DataFirebase modelClass = dataSnap.getValue(DataFirebase.class);
modelClass.setLabel(dataSnap.getKey());
listLabel.add(modelClass);
}
listAdapter = new listAdapter(listLabel, ListActivity.this);
labelRecycleView.setAdapter(listAdapter);
simpleway.stopProgressDialog();
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
System.out.println(databaseError.getDetails() + " " + databaseError.getMessage());
simpleway.stopProgressDialog();
}
});
}
private void putLayoutManager() {
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
labelRecycleView.setLayoutManager(layoutManager);
labelRecycleView.setItemAnimator(new DefaultItemAnimator());
}
#Override
public void findView() {
databaseReference = FirebaseDatabase.getInstance().getReference();
labelRecycleView = findViewById(R.id.listRecyclerVIew);
bInput = findViewById(R.id.inputData);
}
#Override
public void initListener() {
bInput.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
simpleway.startnextActivity(ListActivity.this, InputActivity.class);
}
});
}
}
and this is my adapter it show that i cant get data from my label.
ListAdapter
public class listAdapter extends RecyclerView.Adapter<listAdapter.ViewHolder> {
private List<DataFirebase> dataList;
private AppCompatActivity someActivity;
public listAdapter(List<DataFirebase> dataFirebases, AppCompatActivity someActivity) {
this.dataList = dataFirebases;
this.someActivity = someActivity;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.item_label, viewGroup, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull ViewHolder viewHolder, int i) {
final DataFirebase labelList = dataList.get(i);
viewHolder.textLabel.setText(labelList.getLabel());
viewHolder.linearLabel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
someActivity.getSupportFragmentManager()
.beginTransaction()
.setCustomAnimations(R.anim.enter_from_right, R.anim.exit_to_right, R.anim.enter_from_right, R.anim.exit_to_right)
.replace(R.id.container_layout, new TitleFragment())
.commit();
}
});
}
#Override
public int getItemCount() {
return dataList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public LinearLayout linearLabel;
public TextView textLabel;
public ViewHolder(#NonNull View itemView) {
super(itemView);
textLabel = itemView.findViewById(R.id.text_label);
linearLabel = itemView.findViewById(R.id.linearLabel);
}
}
}
its perfectly fine before I try to install for the second time. the data are showed up just fine but the second time I try to install I got this error.
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at com.example.parzival.flashcard.adapter.listAdapter.onBindViewHolder(listAdapter.java:45)
at com.example.parzival.flashcard.adapter.listAdapter.onBindViewHolder(listAdapter.java:23)
at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6781)
at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6823)
How can I fix this?
Going by the stack trace, your textview is null.
textLabel = itemView.findViewById(R.id.text_label);
linearLabel = itemView.findViewById(R.id.linearLabel);
Make sure that your text_label / linearLabel id belongs to R.layout.item_label.
Please show your data structure
add this in your code:
if (modelClass != null){
modelClass.setLabel(dataSnap.getKey());
}
Related
This Is My MainActivity.
public class MatchContests extends AppCompatActivity implements ItemClickListener {
RecyclerView recyclerView;
private DatabaseReference myref;
private ArrayList<ZgetsetContests> contestsList;
private AdapterContests adapterContests;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_match_contests);
recyclerView = findViewById(R.id.recyclerView);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setHasFixedSize(true);
adapterContests.setClickListener(this);
myref = FirebaseDatabase.getInstance().getReference();
contestsList = new ArrayList<>();
ClearAll();
GetContestDetails();
}
private void GetContestDetails()
{
Query query = myref.child("contestsdetails").child("match1");
query.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
ClearAll();
for (DataSnapshot snapshot : dataSnapshot.getChildren())
{
ZgetsetContests zgetsetContests = new ZgetsetContests();
zgetsetContests.setEntry(snapshot.child("entry").getValue().toString());
zgetsetContests.setBonususage(snapshot.child("bonususage").getValue().toString());
zgetsetContests.setFirstposition(snapshot.child("firstposition").getValue().toString());
zgetsetContests.setMaintitle(snapshot.child("maintitle").getValue().toString());
zgetsetContests.setTeamsallowed(snapshot.child("teamsallowed").getValue().toString());
zgetsetContests.setTotalspots(snapshot.child("totalspots").getValue().toString());
zgetsetContests.setPrizepool(snapshot.child("prizepool").getValue().toString());
contestsList.add(zgetsetContests);
}
adapterContests = new AdapterContests(getApplicationContext(),contestsList);
recyclerView.setAdapter(adapterContests);
adapterContests.notifyDataSetChanged();
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
private void ClearAll()
{
if (contestsList != null) {
contestsList.clear();
if (adapterContests != null){adapterContests.notifyDataSetChanged();}
}
contestsList = new ArrayList<>();
}
#Override
public void onClick(View v, int position) {
}
}
My Adapter Class.
public class AdapterContests extends RecyclerView.Adapter<AdapterContests.ViewHolder> {
private static final String Tag = "RecyclerView";
private Context mContext;
private ArrayList<ZgetsetContests> contestsList;
private DatabaseReference dr;
private ItemClickListener clickListener;
public AdapterContests(Context mContext, ArrayList<ZgetsetContests> contestsList) {
this.mContext = mContext;
this.contestsList = contestsList;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.design_contests,parent,false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull final ViewHolder holder, int position) {
holder.entry.setText(contestsList.get(position).getEntry());
holder.prizepool.setText(contestsList.get(position).getPrizepool());
holder.firstposition.setText(contestsList.get(position).getFirstposition());
holder.totalspots.setText(contestsList.get(position).getTotalspots());
holder.teamsallowed.setText(contestsList.get(position).getTeamsallowed());
holder.bonususage.setText(contestsList.get(position).getBonususage());
holder.maintitle.setText(contestsList.get(position).getMaintitle());
holder.progressBar.setMax(Integer.parseInt(contestsList.get(position).getTotalspots()));
dr = FirebaseDatabase.getInstance().getReference().child("match1joinedteams").child("contest1");
dr.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.exists())
{
Long countjoinedteams1 = dataSnapshot.getChildrenCount();
int a = countjoinedteams1.intValue();
holder.progressBar.setProgress(a);
}else{ }
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
#Override
public int getItemCount() {
return contestsList.size();
}
public void setClickListener(ItemClickListener itemClickListener) {
this.clickListener = itemClickListener;
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener
{
TextView maintitle;
TextView entry;
TextView prizepool;
TextView firstposition;
TextView teamsallowed;
TextView bonususage;
TextView totalspots;
ProgressBar progressBar;
public ViewHolder(#NonNull View itemView) {
super(itemView);
maintitle = itemView.findViewById(R.id.maintitle1);
entry = itemView.findViewById(R.id.entry1);
prizepool = itemView.findViewById(R.id.prizepool1);
firstposition = itemView.findViewById(R.id.firstpoisition1);
teamsallowed = itemView.findViewById(R.id.teamsallowed1);
bonususage = itemView.findViewById(R.id.bonususage1);
totalspots = itemView.findViewById(R.id.totalspots1);
progressBar = itemView.findViewById(R.id.progressbar1);
itemView.setTag(itemView);
itemView.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if (clickListener != null) clickListener.onClick(v, getAdapterPosition());
}
}
}
AppCrashes When I Enter The Activity.
Problem is In MainActivity On Line adapterContests.setClickListener(this); Logcat Shows Attempt To Invoke Virtual Method on null Object.
When I Remove This Line App Works Fine But Click Listener Will Not Be Working.
Before When i was only retriving data app was working fine but after implementing onClick App Crashed
Well you call the method on the adapter when it isn't initialized yet.
Do you see this line:
adapterContests.setClickListener(this);
Move it under these:
..............
adapterContests = new AdapterContests(getApplicationContext(),contestsList);
recyclerView.setAdapter(adapterContests);
//here...
adapterContests.notifyDataSetChanged();
..........
UPDATE:
//SHOULD BE LIKE THIS
adapterContests.setClickListener(MatchContests.this);
After implementing all the Recyclerview codes, i still end up with this Error :E/RecyclerView: No adapter attached; skipping layout.
Im not sure if it is because of the layout or where it is placed...
I changed the location of it multiple times and i had to revert back to an older version as some of the solutions i found on the internet caused it to crash.
public class MainMenu extends AppCompatActivity implements
IFirebaseLoadListener {
private Context context;
private List<ItemData> itemDataList;
private List<ItemGroup> dataList;
AlertDialog dialog;
IFirebaseLoadListener iFirebaseLoadListener;
RecyclerView my_recycler_view;
DatabaseReference myData;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_menu);
myData = FirebaseDatabase.getInstance().getReference("Data");
dialog = new SpotsDialog.Builder().setContext(this).build();
iFirebaseLoadListener = this;
//View
my_recycler_view = (RecyclerView)findViewById(R.id.my_recyclr_view);
my_recycler_view.setHasFixedSize(true);
my_recycler_view.setLayoutManager(new LinearLayoutManager(this));
getFirebaseData();
}
private void getFirebaseData () {
dialog.show();
myData.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange (#NonNull DataSnapshot dataSnapshot) {
List<ItemGroup> itemGroups =new ArrayList<>();
for(DataSnapshot groupSnapShot:dataSnapshot.getChildren()){
ItemGroup itemGroup = new ItemGroup();
itemGroup.setHeaderTitle(groupSnapShot.child("headerTitle").getValue (String.class));
GenericTypeIndicator<ArrayList<ItemData>> genericTypeIndicator =new GenericTypeIndicator<ArrayList<ItemData>>(){};
itemGroup.setListItem(groupSnapShot.child("listItem").getValue(genericTypeIndicator));
itemGroups.add(itemGroup);
}
iFirebaseLoadListener.onFirebaseLoadSuccess(itemGroups);
}
#Override
public void onCancelled (#NonNull DatabaseError databaseError) {
iFirebaseLoadListener.onFirebaseLoadFailed(databaseError.getMessage());
}
});
}
#Override
public void onFirebaseLoadSuccess(List<ItemGroup> itemGroupList) {
MyItemGroupAdapter adapter = new
MyItemGroupAdapter(this,itemGroupList);
my_recycler_view.setAdapter(adapter);
dialog.dismiss();
}
#Override
public void onFirebaseLoadFailed(String message) {
Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
}
I have 2 other pages for my Adapters, "MyItemAdapter" and "MyItemGroupAdapter"
but im not sure why it isnt able to call them
My Adapters:
public class MyItemAdapter extends RecyclerView.Adapter<MyItemAdapter.MyViewHolder> {
private Context context;
private List<ItemData> itemDataList;
public MyItemAdapter(Context context, List<ItemData> itemDataList) {
this.context = context;
this.itemDataList = itemDataList;
}
#NonNull
#Override
public MyViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View itemView = LayoutInflater.from(context).inflate(R.layout.layout_item,viewGroup,false);
return new MyViewHolder(itemView);
}
#Override
public void onBindViewHolder(#NonNull MyViewHolder myViewHolder, final int i) {
myViewHolder.txt_item_title.setText(itemDataList.get(i).getName());
Picasso.get().load(itemDataList.get(i).getArt()).into(myViewHolder. img_item);
myViewHolder.setiItemClickListener(new IItemClickListener() {
#Override
public void onItemClickListener(View view, int position) {
Toast.makeText(context, ""+itemDataList.get(i).getName(), Toast.LENGTH_SHORT).show();
}
});
}
#Override
public int getItemCount() {return (itemDataList != null ? itemDataList.size() : 0);
}
public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView txt_item_title;
ImageView img_item;
IItemClickListener iItemClickListener;
public void setiItemClickListener(IItemClickListener iItemClickListener) {
this.iItemClickListener = iItemClickListener;
}
public MyViewHolder(#NonNull View itemView) {
super(itemView);
txt_item_title = itemView.findViewById(R.id.name);
img_item = itemView.findViewById(R.id.art);
itemView.setOnClickListener(this);
}
#Override
public void onClick(View view) {
iItemClickListener.onItemClickListener(view,getAdapterPosition());
}
}
}
There's no data for your RecyclerView to display when the view is created.
Inside of your onCreate you should create your adapter with empty or null data then call setAdapter() on your RecyclerView using the empty adapter. Later, you can update the adapter with new data and call notifyDataSetChanged() on your adapter to refresh it.
I get this error and I dont know how to fix it
java.lang.IndexOutOfBoundsException: Index: 2, Size: 2
at java.util.ArrayList.get(ArrayList.java:411)
at com.tijdelijk.firebasetest.Start$1.populateViewHolder(Start.java:69)
at com.tijdelijk.firebasetest.Start$1.populateViewHolder(Start.java:64)
I am working with firebase database and based on CategoryId I put the items in a arraylist for the SubCategories my code:
public class CategoryFragment extends Fragment {
View myFragment;
RecyclerView listCategory;
RecyclerView.LayoutManager layoutManager;
FirebaseRecyclerAdapter<Category, CategoryViewHolder> adapter;
FirebaseDatabase database;
DatabaseReference categories;
DatabaseReference subCategory;
public static CategoryFragment newInstance() {
CategoryFragment fragment = new CategoryFragment();
return fragment;
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
database = FirebaseDatabase.getInstance();
categories = database.getReference("Category");
subCategory = database.getReference("SubCategory");
}
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
myFragment = inflater.inflate(R.layout.fragment_category, container, false);
listCategory = (RecyclerView) myFragment.findViewById(R.id.listCategory);
listCategory.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(container.getContext());
listCategory.setLayoutManager(layoutManager);
loadCategories();
return myFragment;
}
private void loadCategories() {
adapter = new FirebaseRecyclerAdapter<Category, CategoryViewHolder>(
Category.class,
R.layout.category_layout,
CategoryViewHolder.class,
categories
) {
#Override
protected void populateViewHolder(CategoryViewHolder viewHolder, final Category model, int position) {
viewHolder.category_name.setText(model.getName());
Picasso.with(getActivity())
.load(model.getImage())
.into(viewHolder.category_image);
viewHolder.setItemClickListener(new ItemClickListener() {
#Override
public void onClick(View view, int position, boolean isLongClick) {
Intent startGame = new Intent(getActivity(), Start.class);
Common.categoryId = adapter.getRef(position).getKey();
loadSubCategory(Common.categoryId);
startActivity(startGame);
}
});
}
};
adapter.notifyDataSetChanged();
listCategory.setAdapter(adapter);
}
private void loadSubCategory(String categoryId) {
//Clear list if there are old subCategory
if (Common.subCategoryList.size() > 0) {
Common.subCategoryList.clear();
}
subCategory.orderByChild("CategoryId").equalTo(categoryId)
.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
SubCategory ques = postSnapshot.getValue(SubCategory.class);
Common.subCategoryList.add(ques);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
in this activity I want to display also a recyclerview but this time based on the arraylist I got from the categoryfragment here is my code:
public class Start extends AppCompatActivity {
FirebaseDatabase database;
DatabaseReference subCategory;
FirebaseRecyclerAdapter<SubCategory, SubCategoryViewHolder> adapter;
RecyclerView listSubCategory;
RecyclerView.LayoutManager layoutManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);
database = FirebaseDatabase.getInstance();
subCategory = database.getReference("SubCategory");
listSubCategory = findViewById(R.id.listSubCategory);
listSubCategory.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(getBaseContext());
listSubCategory.setLayoutManager(layoutManager);
loadSubCategories();
adapter.notifyDataSetChanged();
listSubCategory.setAdapter(adapter);
}
private void loadSubCategories() {
adapter = new FirebaseRecyclerAdapter<SubCategory, SubCategoryViewHolder>(
SubCategory.class,
R.layout.subcategory_layout,
SubCategoryViewHolder.class,
subCategory
) {
#Override
protected void populateViewHolder(SubCategoryViewHolder viewHolder, SubCategory model, int position) {
viewHolder.subcategory_nlname.setText(Common.subCategoryList.get(position).getLatijnseNaam());
viewHolder.subcategory_ltname.setText(Common.subCategoryList.get(position).getNederlandseNaam());
viewHolder.setItemClickListener(new ItemClickListener() {
#Override
public void onClick(View view, int position, boolean isLongClick) {
Intent startGame = new Intent(Start.this, Start.class);
Common.categoryId = adapter.getRef(position).getKey();
startActivity(startGame);
}
});
}
};
}
}
here is my viewholder:
public class SubCategoryViewHolder extends RecyclerView.ViewHolder
implements View.OnClickListener{
public TextView subcategory_nlname;
public TextView subcategory_ltname;
private ItemClickListener itemClickListener;
public SubCategoryViewHolder(View itemView) {
super(itemView);
subcategory_ltname = itemView.findViewById(R.id.latijnse_naam);
subcategory_nlname = itemView.findViewById(R.id.nederlandse_naam);
// itemView.findViewById(R.id.btnPlay).setOnClickListener(this);
}
public void setItemClickListener(ItemClickListener itemClickListener) {
this.itemClickListener = itemClickListener;
}
#Override
public void onClick(View view) {
itemClickListener.onClick(view,getAdapterPosition(),false);
}
}
IndexOutOfBoundException means that you are trying to access a value that is at an index out of the array.
In your case, you have an array of two values. So you have the keys 0 and 1. As said in the error, your code is trying to access the index 2, which does not exist.
You have to check that you are accessing an index that is in the range of values of the array.
Good day to you.
Currently I can display all of the items in my RecyclerView. What I wish to do is calculate how many of these items there are. My items are saved in CardView. I don't want to calculate or sum any particular field, just the number of actual items. Here is my code and I have tried to set it up using getItemCount() method and attach this to a TextView and display outside of RecyclerView. This is not working for me:
public class COM800s1 extends AppCompatActivity {
private FirebaseFirestore db = FirebaseFirestore.getInstance();
private CollectionReference attendanceRef = db.collection("Attendance");
private AttendanceAdapter adapter;
private Context context;
private TextView attendanceNumbers;
private int counter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_com800s1);
attendanceNumbers = findViewById(R.id.attendanceNums);
FloatingActionButton floatingActionButton = findViewById(R.id.button_add_session);
floatingActionButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(COM800s1.this,NewAttendanceRecord.class));
}
});
setUpRecyclerView();
}
private void setUpRecyclerView() {
final Query query = attendanceRef.whereEqualTo("sessionID", "5I0PLcpgmwdcjKLYLgkU").orderBy("userEmail", Query.Direction.ASCENDING);
FirestoreRecyclerOptions<Attendance> options = new FirestoreRecyclerOptions.Builder<Attendance>()
.setQuery(query, Attendance.class).build();
adapter = new AttendanceAdapter(options);
RecyclerView recyclerView = findViewById(R.id.recycler_view);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(adapter);
//counter = adapter.getItemCount();
//attendanceNumbers.setText("Currently " +counter+ " students have attended this session");
new ItemTouchHelper(new ItemTouchHelper.SimpleCallback(0,
ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) {
#Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
return false;
}
#Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
adapter.deleteItem(viewHolder.getAdapterPosition());
}
}).attachToRecyclerView(recyclerView);
adapter.setSetOnItemClickListener(new AttendanceAdapter.OnItemClickListener() {
#Override
public void onItemClick(Attendance documentSnapshot, int position) {
}
});
}
#Override
protected void onStart () {
super.onStart();
adapter.startListening();
}
#Override
protected void onStop(){
super.onStop();
adapter.stopListening();
}
}
Here is my Adapter code:
public class AttendanceAdapter extends FirestoreRecyclerAdapter<Attendance, AttendanceAdapter.AttendanceHolder>{
private AttendanceAdapter.OnItemClickListener listener;
public AttendanceAdapter(#NonNull FirestoreRecyclerOptions<Attendance> options) {
super(options);
}
#Override
protected void onBindViewHolder(#NonNull AttendanceHolder holder, int position, #NonNull Attendance model) {
holder.textViewStudent.setText(model.getUserEmail());
holder.textViewTime.setText(model.getSignInTime());
holder.textViewSession.setText(model.getSessionID());
holder.textViewUserUID.setText(model.getUserUID());
}
#NonNull
#Override
public AttendanceHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.attendance_item, parent, false);
return new AttendanceAdapter.AttendanceHolder(v);
}
public void deleteItem(int position){
getSnapshots().getSnapshot(position).getReference().delete();
}
public int countItems(){
return getItemCount();
}
class AttendanceHolder extends RecyclerView.ViewHolder {
TextView textViewStudent;
TextView textViewTime;
TextView textViewSession;
TextView textViewUserUID;
public AttendanceHolder(View itemView){
super(itemView);
textViewStudent = itemView.findViewById(R.id.text_view_student_name);
textViewTime = itemView.findViewById(R.id.text_view_course);
textViewSession = itemView.findViewById(R.id.text_view_student_ID);
textViewUserUID = itemView.findViewById(R.id.text_view_user_UID);
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int position = getAdapterPosition();
if (position != RecyclerView.NO_POSITION && listener != null){
listener.onItemClick(getSnapshots().get(position),position);
}
}
});
}
}
public interface OnItemClickListener{
void onItemClick(Attendance documentSnapshot, int position);
}
public void setSetOnItemClickListener(AttendanceAdapter.OnItemClickListener listener){
this.listener = listener;
}
Here is my updates code which returns null pointer object on the onClickListener:
public class COM800s1 extends AppCompatActivity {
private FirebaseFirestore db = FirebaseFirestore.getInstance();
private CollectionReference attendanceRef = db.collection("Attendance");
private AttendanceAdapter adapter;
private TextView attendanceNumbers;
private TextView attendanceNumbs;
private Button attendanceButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_com800s1);
attendanceNumbs = findViewById(R.id.attendanceNums);
attendanceButton = findViewById(R.id.calculateAttendance);
FloatingActionButton floatingActionButton = findViewById(R.id.button_add_session);
floatingActionButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(COM800s1.this,NewAttendanceRecord.class));
}
});
setUpRecyclerView();
attendanceButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
attendanceRef.whereEqualTo("sessionID", "5I0PLcpgmwdcjKLYLgkU").get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
#Override
public void onComplete(#NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
int count = 0;
for (DocumentSnapshot document : task.getResult()) {
count++;
attendanceNumbs = findViewById(R.id.attendanceNums);
attendanceNumbs.setText(count);
}
Log.d("TAG", count + "");
} else {
}
}
});
}
});
}
private void setUpRecyclerView() {
final Query query = attendanceRef.whereEqualTo("sessionID", "5I0PLcpgmwdcjKLYLgkU").orderBy("userEmail", Query.Direction.ASCENDING);
FirestoreRecyclerOptions<Attendance> options = new FirestoreRecyclerOptions.Builder<Attendance>()
.setQuery(query, Attendance.class).build();
adapter = new AttendanceAdapter(options);
RecyclerView recyclerView = findViewById(R.id.recycler_view);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(adapter);
new ItemTouchHelper(new ItemTouchHelper.SimpleCallback(0,
ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) {
#Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
return false;
}
#Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
adapter.deleteItem(viewHolder.getAdapterPosition());
}
}).attachToRecyclerView(recyclerView);
adapter.setSetOnItemClickListener(new AttendanceAdapter.OnItemClickListener() {
#Override
public void onItemClick(Attendance documentSnapshot, int position) {
}
});
}
#Override
protected void onStart () {
super.onStart();
adapter.startListening();
}
#Override
protected void onStop(){
super.onStop();
adapter.stopListening();
}
}
If you want to get the list of your recyclerView just create a static method in your recyclerView's adapter and just return your list in that.
for example:
public static List<modelOfYourList> getCount(){
return yourAdapterList;
}
Perhaps add something like this to your AttendanceAdapter class?
I took a look at the FirestoreRecyclerOptionsclass, which exposes a getSnapshots() method. This returns a ObservableSnapshotArray, which in turn extends from Base ObservableSnapshotArray, which exposes a size() method.
class AttendanceAdapter extends FirestoreRecyclerAdapter<Attendance, AttendanceAdapter.AttendanceHolder>{
{
private FirestoreRecyclerOptions<Attendance> options;
public AttendanceAdapter(#NonNull FirestoreRecyclerOptions<Attendance> options)
{
super(options);
this.options = options;
}
#Override
public int getItemCount()
{
final int count = options.getSnapshots() != null
? options.getSnapshots().size()
: 0;
return count;
}
Sources:
FirestoreRecyclerOptions
ObservableSnapshotArray
BaseObservableSnapshotArray
Update: I see that the FirestoreRecyclerAdapter already has a getItemCount method already and you are calling it. Perhaps the data has not yet returned? What are you seeing for the count when you print it?
If you are trying to make the counter variable as a global variable and use it outside the callback it will always be null because as #Vinnie mentioned in his answer, the data has not finished loading yet. What's really happening is that you are trying to get the data from the database and immediately use it it while the counter variable has the initial value of 0. Then, when getting the data from the database completes, it changes counter's value, but it's never read again. To solve this, the use result of your getItemCount() method inside your adapter class to set it's value to the TextView.
If you want to count the number of documents beneath a collection, please also see my answer from this post.
I tried to change the database in the console firebase but the data in my adapter unchanged, recylerview was only changed when the application is closed and opened again
MainActivity.java
public class MainActivity extends AppCompatActivity {
private List<ModelDB> dbList = new ArrayList<>();
private AdapterDB adapter;
private RecyclerView recyclerView;
FirebaseDatabase database;
DatabaseReference reference;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initFirebase();
getData();
initRecylerView();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu,menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.add_person:
Intent intent = new Intent(getApplicationContext(),RegisterActivity.class);
startActivity(intent);
break;
default:
Toast.makeText(getApplicationContext(),"lalalala",Toast.LENGTH_LONG).show();
break;
}
return super.onOptionsItemSelected(item);
}
private void initFirebase(){
database = FirebaseDatabase.getInstance();
reference = database.getReference("user");
}
private void initRecylerView(){
recyclerView = (RecyclerView)findViewById(R.id.rv_list);
recyclerView.setHasFixedSize(true);
adapter = new AdapterDB(dbList);
recyclerView.setAdapter(adapter);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(layoutManager);
}
private void getData() {
reference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
//loop data all user
for (DataSnapshot postSnapshot: dataSnapshot.getChildren()) {
//instance object to get and set data
ModelDB modelDB = postSnapshot.getValue(ModelDB.class);
//adding data list from object
dbList.add(modelDB);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
AdapterDB.java
public class AdapterDB extends RecyclerView.Adapter<AdapterDB.MyViewHolder> {
private List<ModelDB> modelDBList;
public AdapterDB(List<ModelDB> modelDBList) {
this.modelDBList = modelDBList;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_data,parent,false);
return new MyViewHolder(view);
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
ModelDB modelDB = modelDBList.get(position);
holder.tv_nama.setText(modelDB.getNama());
holder.tv_alamat.setText(modelDB.getAlamat());
}
#Override
public int getItemCount() {
return modelDBList.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView tv_nama, tv_alamat;
public MyViewHolder(View itemView) {
super(itemView);
tv_nama = (TextView) itemView.findViewById(R.id.tv_nama);
tv_alamat = (TextView) itemView.findViewById(R.id.tv_alamat);
}
}
}
You need to kind of let the adapter know that the data has changed and that the UI has to be refreshed or whatever. The OnDataChange in your code should be like this (check the last line within the method) -
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
//loop data all user
for (DataSnapshot postSnapshot: dataSnapshot.getChildren()) {
//instance object to get and set data
ModelDB modelDB = postSnapshot.getValue(ModelDB.class);
//adding data list from object
dbList.add(modelDB);
adapter.notifyDataSetChanged();
}
}
This should do the trick assuming there are no other bugs in some other parts of your code