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)
{

}
});









share|improve this question
























  • 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















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)
{

}
});









share|improve this question
























  • 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













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)
{

}
});









share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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


















  • 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












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);
}





share|improve this answer























  • 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


















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();





share|improve this answer




























    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);





    share|improve this answer





















      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














       

      draft saved


      draft discarded


















      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

























      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);
      }





      share|improve this answer























      • 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















      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);
      }





      share|improve this answer























      • 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













      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);
      }





      share|improve this answer














      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);
      }






      share|improve this answer














      share|improve this answer



      share|improve this answer








      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


















      • 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












      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();





      share|improve this answer

























        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();





        share|improve this answer























          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();





          share|improve this answer












          replace last else block with this



              TuitionPackage tuitionPackage = new TuitionPackage();
          tuitionPackage.setPrice(mPriceView.getText().toString());
          tuitionPackageList.add(tuitionPackage);
          mPackageAdapter.notifyDataSetChanged();






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 11 at 8:24









          Touhidul Islam

          266110




          266110






















              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);





              share|improve this answer

























                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);





                share|improve this answer























                  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);





                  share|improve this answer












                  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);






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 11 at 12:51









                  Adda

                  12119




                  12119






























                       

                      draft saved


                      draft discarded



















































                       


                      draft saved


                      draft discarded














                      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





















































                      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







                      Popular posts from this blog

                      Schultheiß

                      Liste der Kulturdenkmale in Wilsdruff

                      Android Play Services Check