I've two values in my document which will help me to set progress bar in the app. for ex joined and slots. both are numbers.
and below i am fetching the values and setting the progressbar value using the slots and joined number which i fetched from the firestore :
public class Home extends AppCompatActivity {
private FirebaseFirestore mDatabase;
private FirestoreRecyclerAdapter adapter;
RecyclerView recyclerView;
@Override
protected void onStart() {
super.onStart();
adapter.startListening();
}
@Override
protected void onStop() {
super.onStop();
adapter.stopListening();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
FirebaseAuth mAuth = FirebaseAuth.getInstance();
FirebaseUser mUser = mAuth.getCurrentUser();
mDatabase = FirebaseFirestore.getInstance();
recyclerView = findViewById(R.id.rv);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
if (mUser != null) {
fetch();
} else {
Intent intent = new Intent(Home.this, Login.class);
startActivity(intent);
finish();
}
}
private void fetch() {
Query query = mDatabase.collection("batches");
FirestoreRecyclerOptions<batches> options = new FirestoreRecyclerOptions.Builder<batches>()
.setQuery(query, new SnapshotParser<batches>() {
@NonNull
@Override
public matches parseSnapshot(@NonNull DocumentSnapshot snapshot) {
return new matches(
snapshot.getLong("slots"),
snapshot.getLong("joined")
);
}
}).build();
adapter = new FirestoreRecyclerAdapter<batches,BatchHolder>(options) {
@Override
protected void onBindViewHolder(@NonNull BatchHolder holder, int position, @NonNull batches model) {
holder.progressBar.setProgress(model.getJoined().intValue()/model.getSlots().intValue()*100);
}
@NonNull
@Override
public BatchHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_items,viewGroup,false);
return new BatchHolder(view);
}
};
recyclerView.setAdapter(adapter);
}
public class BatchHolder extends RecyclerView.ViewHolder {
ProgressBar progressBar;
public BatchHolder(@NonNull View itemView) {
super(itemView);
progressBar = itemView.findViewById(R.id.progressBar);
}
}
and in batches.class :
public class batches {
private Long slots, joined;
public matches(Long slots, Long joined) {
this.slots = slots;
this.joined = joined;
}
public Long getSlots() {
return slots;
}
public void setSlots(Long slots) {
this.slots = slots;
}
public Long getJoined() {
return joined;
}
public void setJoined(Long joined) {
this.joined = joined;
}
but when i run the app it says the following error :
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ProgressBar.setProgress(int)' on a null object reference
at app.Home$2.onBindViewHolder(Home.java:109)
at app.Home$2.onBindViewHolder(Home.java:100)
at com.firebase.ui.firestore.FirestoreRecyclerAdapter.onBindViewHolder(FirestoreRecyclerAdapter.java:125)
at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6781)
at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6823)
at android.support.v7.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5752).... so on