How To Display The Firebase Data In Listview?
Solution 1:
You can use FirebaseListAdapter for display data in list.
Like :
Firebase ref = new Firebase("https://<yourapp>.firebaseio.com");
ListAdapter adapter = new FirebaseListAdapter<ChatMessage>(this, ChatMessage.class, android.R.layout.two_line_list_item, mRef)
{
protectedvoidpopulateView(View view, ChatMessage chatMessage)
{
((TextView)view.findViewById(android.R.id.text1)).setText(chatMessage.getName());
((TextView)view.findViewById(android.R.id.text2)).setText(chatMessage.getMessage());
}
};
listView.setListAdapter(adapter);
Solution 2:
You have almost done that you just need Recyclerview to display data here is a guide on how to display the data in the list.
Create a model and then store the email and time in this model.
classSample{
String time;
String email;
publicvoidsetEmail(String email){
this.email = email;
}
publicvoidsetTime(String time){
this.time = time;
}
}
now store the data from firebase into this model.
@OverridepublicvoidonDataChange(DataSnapshot dataSnapshot) {
List<Sample> sampleList = newArrayList<>();
for(DataSnapshot ds : dataSnapshot.getChildren()) {
Sample obj = newSample();
String email = ds.child("email").getValue(String.class);
String time = ds.child("time").getValue(String.class);
obj.setEmail(email);
obj.setTime(time);
sampleList.add(obj);
Log.d("TAG", email + " / " + time); // logcat check value
}
}
now follow this guide to create recyclerview and pass the list created above to recyclerview to inflate the list.
https://guides.codepath.com/android/using-the-recyclerview
EDit:
add dependency:
implementation 'com.android.support:recyclerview-v7:27.1.1'
now add recyclerview where you want to add it like in fragment or activity.
<?xml version="1.0" encoding="utf-8"?><android.support.constraint.ConstraintLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"android:layout_width="match_parent"android:layout_height="match_parent"><android.support.v7.widget.RecyclerViewandroid:id="@+id/rvSample"android:layout_width="0dp"android:layout_height="0dp"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent" /></android.support.constraint.ConstraintLayout>
now its time to add item view for a single row:
<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:orientation="horizontal"android:layout_width="match_parent"android:layout_height="wrap_content"android:paddingTop="10dp"android:paddingBottom="10dp"
><TextViewandroid:id="@+id/email"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"
/><TextViewandroid:id="@+id/time"android:layout_width="wrap_content"android:layout_height="wrap_content"android:paddingLeft="16dp"android:paddingRight="16dp"android:textSize="10sp"
/></LinearLayout>
now add an adapter for recyclerview to work.
publicclassAdapterextendsRecyclerView.Adapter<Adapter.ViewHolder> {
List<Sample> list;
Adapter(List<Sample> newList){
this.list = newList;
}
@Overridepublic Adapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Contextcontext= parent.getContext();
LayoutInflaterinflater= LayoutInflater.from(context);
Viewview= inflater.inflate(R.layout.item_contact, parent, false);
ViewHolderviewHolder=newViewHolder(view);
return viewHolder;
}
@OverridepublicvoidonBindViewHolder(Adapter.ViewHolder viewHolder, int position) {
// Get the data model based on positionSampleobj= list.get(position);
// Set item views based on your views and data model
viewHolder.time.setText(obj.getTime());
viewHolder.email.setText(obj.getEmail());
}
// Returns the total count of items in the list@OverridepublicintgetItemCount() {
return list.size();
}
publicclassViewHolderextendsRecyclerView.ViewHolder {
public TextView email;
public TextView time;
publicViewHolder(View itemView) {
super(itemView);
email = (TextView) itemView.findViewById(R.id.email);
time = (TextView) itemView.findViewById(R.id.time);
}
}
}
now access the created recyclerview in your fragment and activity. like below and set properties and set adapter.
List<Sample> list = newArrayList<>();
Adapteradapter=newAdapter(list);
rvSample.setLayoutManager(newLinearLayoutManager(this));
rvSample.setAdapter(adapter);
now when you get the data from firebase just add to existing list and notify the adapter.
@OverridepublicvoidonDataChange(DataSnapshot dataSnapshot) {
List<Sample> sampleList = newArrayList<>();
for(DataSnapshot ds : dataSnapshot.getChildren()) {
Sample obj = newSample();
String email = ds.child("email").getValue(String.class);
String time = ds.child("time").getValue(String.class);
obj.setEmail(email);
obj.setTime(time);
sampleList.add(obj);
Log.d("TAG", email + " / " + time); // logcat check value
}
list.addAll(sampleList);
adapter.notifyDataSetChanged();
}
Solution 3:
Try this code.. Recycler view make adapter for display ..
publicclassDisplayAllDataextendsRecyclerView.Adapter<DisplayAllData.ItemViewHolder> {
private List<User> mUserLsit = newArrayList<>();
private Context mContext;
@Overridepublic ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Viewview= LayoutInflater.from(parent.getContext()).inflate(R.layout.row_layout, parent, false);
returnnewItemViewHolder(view);
}
publicDisplayAllData(Context mContext, List<User> mUserLsit) {
this.mContext = mContext;
this.mUserLsit = mUserLsit;
}
@OverridepublicvoidonBindViewHolder(ItemViewHolder holder, int position) {
Useruser= mUserLsit.get(position);
holder.mTvName.setText(user.name);
holder.mTvEmail.setText(user.email);
holder.mTvPwd.setText(user.pwd);
}
@OverridepublicintgetItemCount() {
return mUserLsit.size();
}
publicclassItemViewHolderextendsRecyclerView.ViewHolder {
TextView mTvName, mTvEmail, mTvPwd;
publicItemViewHolder(View itemView) {
super(itemView);
mTvEmail = itemView.findViewById(R.id.rlTvEmail);
mTvName = itemView.findViewById(R.id.rlTvName);
mTvPwd = itemView.findViewById(R.id.rlTvPwd);
}
}
}
make activity..
publicclassDisplayActivityextendsAppCompatActivity {
privateRecyclerView mRvData;
privateDisplayAllData allDataAdapter;
privateDatabaseReference mDatabase;
privateTextView mTvEmpty;
privateFirebaseDatabase mFirebaseInstance;
privateList<User> mUserList = newArrayList<>();
@OverrideprotectedvoidonCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.display_data);
initView();
}
privatevoidinitView() {
mFirebaseInstance = FirebaseDatabase.getInstance();
mDatabase = mFirebaseInstance.getReference("usersDb/UserTable");
mRvData = findViewById(R.id.rvData);
mTvEmpty = findViewById(R.id.dlTvEmpty);
mRvData.setLayoutManager(newLinearLayoutManager(this));
mDatabase.addValueEventListener(newValueEventListener() {
@OverridepublicvoidonDataChange(DataSnapshot dataSnapshot) {
mUserList.clear();
for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) {
User user = dataSnapshot1.getValue(User.class);
mUserList.add(user);
}
allDataAdapter = newDisplayAllData(DisplayActivity.this, mUserList);
mRvData.setAdapter(allDataAdapter);
allDataAdapter.notifyDataSetChanged();
if (mUserList.isEmpty())
mTvEmpty.setVisibility(View.VISIBLE);
else
mTvEmpty.setVisibility(View.GONE);
}
@OverridepublicvoidonCancelled(DatabaseError databaseError) {
}
});
}
}
and i hope you add internet permission into android manifest file.
Post a Comment for "How To Display The Firebase Data In Listview?"