How to display new item at the top position of the adapter?
up vote
1
down vote
favorite
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)
{
}
});
android android-studio
add a comment |
up vote
1
down vote
favorite
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)
{
}
});
android android-studio
Can you give the log of the error you got?
– Birju Vachhani
Nov 11 at 8:17
what is a red error? please provide logcat
– Touhidul Islam
Nov 11 at 8:20
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
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)
{
}
});
android android-studio
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)
{
}
});
android android-studio
android android-studio
edited Nov 11 at 8:50
asked Nov 8 at 16:45
Adda
12119
12119
Can you give the log of the error you got?
– Birju Vachhani
Nov 11 at 8:17
what is a red error? please provide logcat
– Touhidul Islam
Nov 11 at 8:20
add a comment |
Can you give the log of the error you got?
– Birju Vachhani
Nov 11 at 8:17
what is a red error? please provide logcat
– Touhidul Islam
Nov 11 at 8:20
Can you give the log of the error you got?
– Birju Vachhani
Nov 11 at 8:17
Can you give the log of the error you got?
– Birju Vachhani
Nov 11 at 8:17
what is a red error? please provide logcat
– Touhidul Islam
Nov 11 at 8:20
what is a red error? please provide logcat
– Touhidul Islam
Nov 11 at 8:20
add a comment |
3 Answers
3
active
oldest
votes
up vote
1
down vote
accepted
This line:
tuitionPackageList.add(0, new TuitionPackage(tuitionPackageList));
you get an error because you are instantiating a TuitionPackage
object with tuitionPackageList
as a parameter and I believe this is not correct.
Earlier in the code you made the same instantiation with just:
TuitionPackage tuitionPackage = new TuitionPackage();
and you added the item to the end of the list.
Is it this item that you wanted to add at position 0?
Edit change to this:
else {
TuitionPackage tuitionPackage = new TuitionPackage();
tuitionPackage.setPrice(mPriceView.getText().toString());
tuitionPackageList.add(0, tuitionPackage);
mPackageAdapter.notifyItemInserted(0);
}
I want to add new item at the top of my advertisement adapter...
– Adda
Nov 11 at 8:21
See my edited answer
– forpas
Nov 11 at 8:28
It works! But for tuitionPackage. I want the whole advertisement. Which is the latest advertisement added will be at the top position in the adapter...
– Adda
Nov 11 at 8:41
You know I don't know exactly how all your code works, but if you want the new item at the top you must insert it at position 0 just like the code i wrote above. Isn't it inserted at the top?
– forpas
Nov 11 at 8:43
It is. Alright, I will try first. Can I have you email sir? If anything wrong, it easy for me to ask you...
– Adda
Nov 11 at 8:45
|
show 3 more comments
up vote
1
down vote
replace last else block with this
TuitionPackage tuitionPackage = new TuitionPackage();
tuitionPackage.setPrice(mPriceView.getText().toString());
tuitionPackageList.add(tuitionPackage);
mPackageAdapter.notifyDataSetChanged();
add a comment |
up vote
0
down vote
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);
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
This line:
tuitionPackageList.add(0, new TuitionPackage(tuitionPackageList));
you get an error because you are instantiating a TuitionPackage
object with tuitionPackageList
as a parameter and I believe this is not correct.
Earlier in the code you made the same instantiation with just:
TuitionPackage tuitionPackage = new TuitionPackage();
and you added the item to the end of the list.
Is it this item that you wanted to add at position 0?
Edit change to this:
else {
TuitionPackage tuitionPackage = new TuitionPackage();
tuitionPackage.setPrice(mPriceView.getText().toString());
tuitionPackageList.add(0, tuitionPackage);
mPackageAdapter.notifyItemInserted(0);
}
I want to add new item at the top of my advertisement adapter...
– Adda
Nov 11 at 8:21
See my edited answer
– forpas
Nov 11 at 8:28
It works! But for tuitionPackage. I want the whole advertisement. Which is the latest advertisement added will be at the top position in the adapter...
– Adda
Nov 11 at 8:41
You know I don't know exactly how all your code works, but if you want the new item at the top you must insert it at position 0 just like the code i wrote above. Isn't it inserted at the top?
– forpas
Nov 11 at 8:43
It is. Alright, I will try first. Can I have you email sir? If anything wrong, it easy for me to ask you...
– Adda
Nov 11 at 8:45
|
show 3 more comments
up vote
1
down vote
accepted
This line:
tuitionPackageList.add(0, new TuitionPackage(tuitionPackageList));
you get an error because you are instantiating a TuitionPackage
object with tuitionPackageList
as a parameter and I believe this is not correct.
Earlier in the code you made the same instantiation with just:
TuitionPackage tuitionPackage = new TuitionPackage();
and you added the item to the end of the list.
Is it this item that you wanted to add at position 0?
Edit change to this:
else {
TuitionPackage tuitionPackage = new TuitionPackage();
tuitionPackage.setPrice(mPriceView.getText().toString());
tuitionPackageList.add(0, tuitionPackage);
mPackageAdapter.notifyItemInserted(0);
}
I want to add new item at the top of my advertisement adapter...
– Adda
Nov 11 at 8:21
See my edited answer
– forpas
Nov 11 at 8:28
It works! But for tuitionPackage. I want the whole advertisement. Which is the latest advertisement added will be at the top position in the adapter...
– Adda
Nov 11 at 8:41
You know I don't know exactly how all your code works, but if you want the new item at the top you must insert it at position 0 just like the code i wrote above. Isn't it inserted at the top?
– forpas
Nov 11 at 8:43
It is. Alright, I will try first. Can I have you email sir? If anything wrong, it easy for me to ask you...
– Adda
Nov 11 at 8:45
|
show 3 more comments
up vote
1
down vote
accepted
up vote
1
down vote
accepted
This line:
tuitionPackageList.add(0, new TuitionPackage(tuitionPackageList));
you get an error because you are instantiating a TuitionPackage
object with tuitionPackageList
as a parameter and I believe this is not correct.
Earlier in the code you made the same instantiation with just:
TuitionPackage tuitionPackage = new TuitionPackage();
and you added the item to the end of the list.
Is it this item that you wanted to add at position 0?
Edit change to this:
else {
TuitionPackage tuitionPackage = new TuitionPackage();
tuitionPackage.setPrice(mPriceView.getText().toString());
tuitionPackageList.add(0, tuitionPackage);
mPackageAdapter.notifyItemInserted(0);
}
This line:
tuitionPackageList.add(0, new TuitionPackage(tuitionPackageList));
you get an error because you are instantiating a TuitionPackage
object with tuitionPackageList
as a parameter and I believe this is not correct.
Earlier in the code you made the same instantiation with just:
TuitionPackage tuitionPackage = new TuitionPackage();
and you added the item to the end of the list.
Is it this item that you wanted to add at position 0?
Edit change to this:
else {
TuitionPackage tuitionPackage = new TuitionPackage();
tuitionPackage.setPrice(mPriceView.getText().toString());
tuitionPackageList.add(0, tuitionPackage);
mPackageAdapter.notifyItemInserted(0);
}
edited Nov 11 at 8:28
answered Nov 11 at 8:17
forpas
3,6371214
3,6371214
I want to add new item at the top of my advertisement adapter...
– Adda
Nov 11 at 8:21
See my edited answer
– forpas
Nov 11 at 8:28
It works! But for tuitionPackage. I want the whole advertisement. Which is the latest advertisement added will be at the top position in the adapter...
– Adda
Nov 11 at 8:41
You know I don't know exactly how all your code works, but if you want the new item at the top you must insert it at position 0 just like the code i wrote above. Isn't it inserted at the top?
– forpas
Nov 11 at 8:43
It is. Alright, I will try first. Can I have you email sir? If anything wrong, it easy for me to ask you...
– Adda
Nov 11 at 8:45
|
show 3 more comments
I want to add new item at the top of my advertisement adapter...
– Adda
Nov 11 at 8:21
See my edited answer
– forpas
Nov 11 at 8:28
It works! But for tuitionPackage. I want the whole advertisement. Which is the latest advertisement added will be at the top position in the adapter...
– Adda
Nov 11 at 8:41
You know I don't know exactly how all your code works, but if you want the new item at the top you must insert it at position 0 just like the code i wrote above. Isn't it inserted at the top?
– forpas
Nov 11 at 8:43
It is. Alright, I will try first. Can I have you email sir? If anything wrong, it easy for me to ask you...
– Adda
Nov 11 at 8:45
I want to add new item at the top of my advertisement adapter...
– Adda
Nov 11 at 8:21
I want to add new item at the top of my advertisement adapter...
– Adda
Nov 11 at 8:21
See my edited answer
– forpas
Nov 11 at 8:28
See my edited answer
– forpas
Nov 11 at 8:28
It works! But for tuitionPackage. I want the whole advertisement. Which is the latest advertisement added will be at the top position in the adapter...
– Adda
Nov 11 at 8:41
It works! But for tuitionPackage. I want the whole advertisement. Which is the latest advertisement added will be at the top position in the adapter...
– Adda
Nov 11 at 8:41
You know I don't know exactly how all your code works, but if you want the new item at the top you must insert it at position 0 just like the code i wrote above. Isn't it inserted at the top?
– forpas
Nov 11 at 8:43
You know I don't know exactly how all your code works, but if you want the new item at the top you must insert it at position 0 just like the code i wrote above. Isn't it inserted at the top?
– forpas
Nov 11 at 8:43
It is. Alright, I will try first. Can I have you email sir? If anything wrong, it easy for me to ask you...
– Adda
Nov 11 at 8:45
It is. Alright, I will try first. Can I have you email sir? If anything wrong, it easy for me to ask you...
– Adda
Nov 11 at 8:45
|
show 3 more comments
up vote
1
down vote
replace last else block with this
TuitionPackage tuitionPackage = new TuitionPackage();
tuitionPackage.setPrice(mPriceView.getText().toString());
tuitionPackageList.add(tuitionPackage);
mPackageAdapter.notifyDataSetChanged();
add a comment |
up vote
1
down vote
replace last else block with this
TuitionPackage tuitionPackage = new TuitionPackage();
tuitionPackage.setPrice(mPriceView.getText().toString());
tuitionPackageList.add(tuitionPackage);
mPackageAdapter.notifyDataSetChanged();
add a comment |
up vote
1
down vote
up vote
1
down vote
replace last else block with this
TuitionPackage tuitionPackage = new TuitionPackage();
tuitionPackage.setPrice(mPriceView.getText().toString());
tuitionPackageList.add(tuitionPackage);
mPackageAdapter.notifyDataSetChanged();
replace last else block with this
TuitionPackage tuitionPackage = new TuitionPackage();
tuitionPackage.setPrice(mPriceView.getText().toString());
tuitionPackageList.add(tuitionPackage);
mPackageAdapter.notifyDataSetChanged();
answered Nov 11 at 8:24
Touhidul Islam
266110
266110
add a comment |
add a comment |
up vote
0
down vote
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);
add a comment |
up vote
0
down vote
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);
add a comment |
up vote
0
down vote
up vote
0
down vote
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);
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);
answered Nov 11 at 12:51
Adda
12119
12119
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53212362%2fhow-to-display-new-item-at-the-top-position-of-the-adapter%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Can you give the log of the error you got?
– Birju Vachhani
Nov 11 at 8:17
what is a red error? please provide logcat
– Touhidul Islam
Nov 11 at 8:20