'How to display new item at the top position of the adapter?
How to display new data at the top position of an adapter? Here are the coding:-
Sorry just now, I wrongly copy the coding, now I just noticed. Sorry my bad. The issues now is I want to display the latest advertisement at the top of the adapter....
mDatabase.child("Advertisement").child(mAuth.getUid()).addValueEventListener(new ValueEventListener()
{
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot)
{
//iterating through all the values in database
mChildrenList = new ArrayList<>();
for (DataSnapshot postSnapshot : dataSnapshot.getChildren())
{
Advertisement advertisement = postSnapshot.getValue(Advertisement.class);
mChildrenList.add(advertisement);
}
//Creating adapter
mAdapter = new AdvertisementAdapter(getApplicationContext(), mChildrenList, "AddAds");
//Adding adapter to recyclerview
mRecyclerView.setAdapter(mAdapter);
//mAdapter.notifyItemChanged(0);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError)
{
}
});
Solution 1:[1]
replace last else block with this
TuitionPackage tuitionPackage = new TuitionPackage();
tuitionPackage.setPrice(mPriceView.getText().toString());
tuitionPackageList.add(tuitionPackage);
mPackageAdapter.notifyDataSetChanged();
Solution 2:[2]
Here are my answer with the help from the above coding to sort the first ads at the top position:-
//Iterating through all the values in database
mChildrenList = new ArrayList<>();
for (DataSnapshot postSnapshot : dataSnapshot.getChildren())
{
Advertisement advertisement = postSnapshot.getValue(Advertisement.class);
mChildrenList.add(0, advertisement);
}
//Creating adapter
mAdapter = new AdvertisementAdapter(getApplicationContext(), mChildrenList, "AddAds");
//Adding adapter to recyclerview
mRecyclerView.setAdapter(mAdapter);
mAdapter.notifyItemInserted(0);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | touhid udoy |
| Solution 2 | Daisy |
