BlackCoder :
I'm creating an app where i am storing data to firebase and retriving it to my reclycler view and but issue is, i want to display the most recent added data to the top of the recycler view. I am trying to get the latest data and I had done all possibly Solution still I am not getting.
please see the following code....
here is my Mainclass:
public class ShowDetails extends AppCompatActivity {
ProgressDialog progressDialog;
List<DataClass> list = new ArrayList<>();
RecyclerView recyclerView;
RecyclerView.Adapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_details);
recyclerView = findViewById(R.id.erecyclerView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(ShowDetails.this));
progressDialog = new ProgressDialog(ShowDetails.this);
progressDialog.setMessage("Loading Data from Firebase Database");
progressDialog.show();
FirebaseOptions options = new FirebaseOptions.Builder()
.setApplicationId("1:102738125513:android:3042292623f0448c") // Required for Analytics.
.setApiKey("AIzaSyAZbWBpPzjAkrg23riK9kwk2T0tDf47dko") // Required for Auth.
.setDatabaseUrl("https://citycare-fed31.firebaseio.com/") // Required for RTDB.
.build();
FirebaseApp.initializeApp(this);
// Retrieve my other app.
FirebaseApp app = FirebaseApp.getInstance("[DEFAULT]");
// Get the database for the other app.
FirebaseDatabase secondaryDatabase = FirebaseDatabase.getInstance(app);
DatabaseReference data = secondaryDatabase.getInstance().getReference("Electricity");
data.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot ds : dataSnapshot.getChildren()) {
for (DataSnapshot dSnapshot : ds.getChildren()) {
DataClass DataClass = dSnapshot.getValue(DataClass.class);
Log.d("Show", DataClass.getEname() == null ? "" : DataClass.getEname());
list.add(DataClass);
}
adapter = new ShowDetails.DataAdapter(ShowDetails.this, list);
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
progressDialog.dismiss();
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
progressDialog.dismiss();
}
});
}
and this is my Adapter class:
private class DataAdapter extends RecyclerView.Adapter<ShowDetails.DataAdapter.ViewHolder> {
Context context;
List<DataClass> listData;
public DataAdapter(Context context, List<DataClass> list) {
this.context = context;
this.listData = list;
}
@Override
public ShowDetails.DataAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.show_items, parent, false);
ShowDetails.DataAdapter.ViewHolder viewHolder = new ShowDetails.DataAdapter.ViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(ShowDetails.DataAdapter.ViewHolder holder, int position) {
final DataClass AllDetails = listData.get(position);
holder.NameTextView.setText(AllDetails.getEname());
holder.EmailTextView.setText(AllDetails.getEemail());
holder.DateTextView.setText(AllDetails.getEdate());
holder.LocationTextView.setText(AllDetails.getElocation());
holder.TypeTextView.setText(AllDetails.getEtype());
Picasso.with(context).load(AllDetails.getImgurl()).resize(120, 60).into(holder.ImageTextView);
holder.ImageTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(ShowDetails.this, ShowImage.class);
i.putExtra("img", AllDetails.getImgurl());
startActivity(i);
}
});
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(ShowDetails.this, Email_Activity.class);
i.putExtra("txt", AllDetails.getEemail());
startActivity(i);
}
});
}
@Override
public int getItemCount() {
return listData.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
public TextView NameTextView;
public TextView EmailTextView;
public TextView DateTextView;
public TextView LocationTextView;
public TextView TypeTextView;
public ImageView ImageTextView;
public ViewHolder(View itemView) {
super(itemView);
NameTextView = itemView.findViewById(R.id.ShowNameTextView);
EmailTextView = itemView.findViewById(R.id.ShowEmailTextView);
DateTextView = itemView.findViewById(R.id.ShowDateTextView);
LocationTextView = itemView.findViewById(R.id.ShowLocationTextView);
TypeTextView = itemView.findViewById(R.id.ShowTypeTextView);
ImageTextView = itemView.findViewById(R.id.ShowImageView);
}
}
}
here is my database
here is my API response after removing extra for loop API response
please help me out i am new to programming
Vahan Mambreyan :
You can add your data to list's first iterator in loop, and move setadapter out of loops:
for (DataSnapshot ds : dataSnapshot.getChildren()) {
for (DataSnapshot dSnapshot : ds.getChildren()) {
DataClass DataClass = dSnapshot.getValue(DataClass.class);
list.add(0, DataClass);
}
}
adapter = new ShowDetails.DataAdapter(ShowDetails.this, list);
recyclerView.setAdapter(adapter);
progressDialog.dismiss();