[英]Not getting total item count from recyclerview
I want to display "No upcoming Appointments" when my recyclerview is empty and display recyclerview when it's not empty.我想在我的 recyclerview 为空时显示“没有即将到来的约会”,并在它不为空时显示 recyclerview。 I have tried various codes to check whether my recyclerview is empty or not, but none worked.
我尝试了各种代码来检查我的 recyclerview 是否为空,但没有一个有效。 Here is my code
这是我的代码
public class appointments extends Fragment {
private RecyclerView recyclerView;
private DatabaseReference ref;
private ArrayList<BookedUserInfo> arrayList;
private FirebaseRecyclerOptions<BookedUserInfo> options;
private FirebaseAuth mAuth;
private String uid;
private FirebaseRecyclerAdapter<BookedUserInfo, FireBaseViewHolderUpcomingAppointment> adapter;
private String shopname;
private ProgressDialog progressDialog;
private TextView emptyview;
private int x=0;
@Override
public void onStart() {
super.onStart();
adapter.startListening();
}
@Override
public void onStop() {
super.onStop();
adapter.stopListening();
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View RootView = inflater.inflate(R.layout.appointment_navigation,container,false);
//find view by ID
recyclerView = (RecyclerView) RootView.findViewById(R.id.recycleupcomingappointment);
emptyview = (TextView) RootView.findViewById(R.id.empty_view);
//Auth instance
mAuth = FirebaseAuth.getInstance();
FirebaseUser currentuser = mAuth.getCurrentUser();
uid = currentuser.getUid();
//initialize progress dialog
progressDialog = new ProgressDialog(getActivity());
progressDialog.setMessage("Loading");
progressDialog.show();
//database refrence
ref = FirebaseDatabase.getInstance().getReference().child("USER").child(uid).child("UpcomingAppointment");
ref.keepSynced(true);
//recycler view
LinearLayoutManager llm = new LinearLayoutManager(getActivity());
llm.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(llm);
//array list
arrayList = new ArrayList<BookedUserInfo>();
options = new FirebaseRecyclerOptions.Builder<BookedUserInfo>().setQuery(ref,BookedUserInfo.class).build();
//adapter
adapter = new FirebaseRecyclerAdapter<BookedUserInfo, FireBaseViewHolderUpcomingAppointment>(options) {
@Override
protected void onBindViewHolder(@NonNull final FireBaseViewHolderUpcomingAppointment holder, int position, @NonNull final BookedUserInfo model) {
final String datedata = model.getDate();
final String timedata = model.getTime();
final int bookingiddata = model.getBookingid();
Date initDate = null;
try {
initDate = new SimpleDateFormat("dd-MM-yyyy").parse(datedata);
} catch (ParseException e) {
e.printStackTrace();
}
SimpleDateFormat formatter = new SimpleDateFormat("EEE, d MMM yyyy");
final String parsedDate = formatter.format(initDate);
holder.time.setText(timedata);
holder.date.setText(parsedDate);
holder.id.setText(String.valueOf(bookingiddata));
final String shopuid = model.getShopuid();
DatabaseReference myRef =FirebaseDatabase.getInstance().getReference().child("SHOP").child(shopuid);
myRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
// This method is called once with the initial value and again
shopname = dataSnapshot.child("shopname").getValue().toString();
holder.name.setText(shopname);
progressDialog.dismiss();
}
@Override
public void onCancelled(DatabaseError error) {
progressDialog.dismiss();
// Failed to read value
}
});
progressDialog.dismiss();
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(), AppointmentBill.class);
intent.putExtra("bookingid",String.valueOf(bookingiddata));
intent.putExtra("timetext",timedata);
intent.putExtra("datetext",datedata);
intent.putExtra("uidtext",shopuid);
intent.putExtra("appointmentinfotext",model.getAppointmentinfo());
startActivity(intent);
}
});
}
@NonNull
@Override
public FireBaseViewHolderUpcomingAppointment onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new FireBaseViewHolderUpcomingAppointment(LayoutInflater.from(getActivity()).inflate(R.layout.cardview_appointment,parent,false));
}
};
recyclerView.setAdapter(adapter);
//check total items
if(adapter.getItemCount() == 0)
{
recyclerView.setVisibility(View.GONE);
emptyview.setVisibility(View.VISIBLE);
}
else
{
recyclerView.setVisibility(View.VISIBLE);
emptyview.setVisibility(View.GONE);
}
progressDialog.dismiss();
return RootView;
}
XML code XML代码
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/relativelayoutview"
android:layout_below="@id/upcomingappointments">
<TextView
android:id="@+id/empty_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="30dp"
android:visibility="gone"
android:background="#E7E7E7"
android:paddingBottom="30dp"
android:text="---------No Upcoming Appointments---------"
android:gravity="center">
</TextView>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycleupcomingappointment"
android:layout_width="match_parent"
android:background="#E7E7E7"
android:layout_height="match_parent">
</androidx.recyclerview.widget.RecyclerView>
</RelativeLayout>
I'm using google firebase(realtime database) to get appointment details.我正在使用 google firebase(实时数据库)来获取约会详细信息。 Please suggest me a solution for this problem.
请建议我解决此问题。
I also tried this but failed so I instead used snapshot.getchilderncount
In my firebase database reference and checked if childrencount =0 or not我也试过这个但失败了,所以我在我的 firebase 数据库参考中使用了
snapshot.getchilderncount
并检查了 childrencount 是否 =0
Create a value event listener
For your apointment database reference and in it say if(snapshot.getChildrenCount==0) { ///your logic }
为您的 apointment 数据库引用创建一个
value event listener
器,并在其中说if(snapshot.getChildrenCount==0) { ///your logic }
Try to move this logic inside onDataChange callback method:尝试在 onDataChange 回调方法中移动这个逻辑:
//check total items
if(adapter.getItemCount() == 0)
{
recyclerView.setVisibility(View.GONE);
emptyview.setVisibility(View.VISIBLE);
}
else
{
recyclerView.setVisibility(View.VISIBLE);
emptyview.setVisibility(View.GONE);
}
this is a sample recyclerView app这是一个示例 recyclerView 应用程序
Adapter.java适配器.java
public class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder> {
List<?> list; //i don`t know your model or type date then i write (?)
public Adapter(List<?> list) {
this.list = list;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.your_layout,parent,false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
//you write your code when bind your view hoder
YourMode model = list.get(position);
}
@Override
public int getItemCount() {
return list.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public ViewHolder(@NonNull View itemView) {
super(itemView);
//for get your view or widget your use **itemView.findViewById()**
}
}
}
and this code for get Item count:以及获取项目计数的代码:
public class MyFragment extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.your_fragment_layout, container,false);
Adapter adapter = Adapter(yourList);
if (adapter.getItemCount() == 0){
}else {
}
return view;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.