RecyclerView Adapter inserting rows in weird positionsRecyclerView onClickHow to add dividers and spaces between items in RecyclerView?Why doesn't RecyclerView have onItemClickListener()?How to create RecyclerView with multiple view type?How to update RecyclerView Adapter Data?How to get values from views in a recyclerview after they are scrolled out of screenMultiple Adapters or One Adapter for different lists and objects - Code PerformanceWhy onBindViewHolder index isn't incrementing in Recycler View?

How do I change my LaTex document to follow some Word requirements?

Which game is this?

Do Veracrypt encrypted volumes have any kind of brute force protection?

How to soundproof the Wood Shop?

David slept with Bathsheba because she was pure?? What does that mean?

Why did the Death Eaters wait to reopen the Chamber of Secrets?

Why are ambiguous grammars bad?

Is tuition reimbursement a good idea if you have to stay with the job

Purpose of cylindrical attachments on Power Transmission towers

My mom's return ticket is 3 days after I-94 expires

Why is my Taiyaki (Cake that looks like a fish) too hard and dry?

What does BREAD stand for while drafting?

Convert GE Load Center to main breaker

Changing the PK column of a data extension without completely recreating it

When to use the uncountable form of a noun?

Why didn't all the iron and heavier elements find their way to the center of the accretion disc in the early solar system?

Fastest way from 8 to 7

Approach sick days in feedback meeting

Part of my house is inexplicably gone

Is this Homebrew Eldritch Invocation, Accursed Memory, balanced?

Must CPU have a GPU if motherboard provides display port (when no separate video card)?

Print "N NE E SE S SW W NW"

In American Politics, why is the Justice Department under the President?

Why is the concept of the Null hypothesis associated with the student's t distribution?



RecyclerView Adapter inserting rows in weird positions


RecyclerView onClickHow to add dividers and spaces between items in RecyclerView?Why doesn't RecyclerView have onItemClickListener()?How to create RecyclerView with multiple view type?How to update RecyclerView Adapter Data?How to get values from views in a recyclerview after they are scrolled out of screenMultiple Adapters or One Adapter for different lists and objects - Code PerformanceWhy onBindViewHolder index isn't incrementing in Recycler View?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








1















I have a Fragment inside of my MainActivity that contains a RecyclerView being populated by a RecyclerView.Adapter. The RecyclerView inflates row_layout.xml file, which has one EditText and one ImageButton, like this:



enter image description here



The button is initially loaded with an "add" image. When clicked, my adapter does this:



package com.fragments.homework04;

import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;

import java.util.ArrayList;

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder>
public static int positionAdapter;

private ArrayList<ItemData> itemsData;
AdapterButtonInterface mListener;

public MyAdapter(ArrayList<ItemData> itemsData, AdapterButtonInterface adapterButtonInterface)
this.itemsData = itemsData;
try
mListener = adapterButtonInterface;

catch(ClassCastException e)
throw new ClassCastException("Class does not implement interface!");



public static class ViewHolder extends RecyclerView.ViewHolder

public EditText txtIngredients;
public ImageView imgViewIcon;

public ViewHolder(View itemLayoutView)
super(itemLayoutView);
txtIngredients = itemLayoutView.findViewById(R.id.etIngredientName);
imgViewIcon = itemLayoutView.findViewById(R.id.btnAddRemoveIngredient);



@Override
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
View itemLayoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_layout, null);
Log.d("itemsSizeAdapter", String.valueOf(itemsData.size()));
ViewHolder viewHolder = new ViewHolder(itemLayoutView);
return viewHolder;


@Override
public void onBindViewHolder(final ViewHolder viewHolder, final int position)
viewHolder.imgViewIcon.setTag(itemsData.get(position).getTag());
viewHolder.imgViewIcon.setImageResource(itemsData.get(position).getImageUrl());
viewHolder.imgViewIcon.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
positionAdapter = viewHolder.getAdapterPosition();
if(itemsData.get(position).getTag().contains("Add"))
if(getItemCount() < 5)
itemsData.get(position).setIngredientValue(viewHolder.txtIngredients.getText().toString());
mListener.addBtnClicked(positionAdapter, getItemCount(), itemsData.get(position).getIngredientValue());


else
if(getItemCount() > 0)
Log.d("adapterPosition:", String.valueOf(position));
mListener.removeBtnClicked(positionAdapter, getItemCount());



);


// Return the size of your itemsData (invoked by the layout manager)
@Override
public int getItemCount()
return itemsData.size();


public interface AdapterButtonInterface
void addBtnClicked(int position, int itemCount, String ingredientValue);
void removeBtnClicked(int position, int itemCount);




Basically calls the method in the interface, that is being implemented by my fragment. In my fragment, I am doing this:



ArrayList<ItemData> itemsData;
public void addBtnClicked(int position, int itemCount, String ingredientValue)
if(itemCount == 4)
//Requirement that there can only be 5 rows at any time.
//Hence when itemCount == 4, we are adding the 5th row and it needs to be a remove instead of add
itemsData.add(position + 1, new ItemData("Remove" + itemCount, R.mipmap.remove_ing, ""));

else
itemsData.add(position + 1, new ItemData("Add" + itemCount, R.mipmap.add_ing, ""));

//When the row is added, the EditText's value isn't stored anywhere
//So I am adding it to the arraylist I have
itemsData.get(position).setIngredientValue(ingredientValue);
itemsData.get(position).setImageUrl(R.mipmap.remove_ing);
itemsData.get(position).setTag("Remove");
mAdapter.notifyDataSetChanged();


@Override
public void removeBtnClicked(int position, int itemCount)
Log.d("listenerPosition:", String.valueOf(position));
itemsData.remove(position);
if(itemCount == 5)
itemsData.get(3).setImageUrl(R.mipmap.add_ing);
itemsData.get(3).setTag("Add3");

mAdapter.notifyDataSetChanged();



A good bit of this works perfectly. When my app loads, I see one EditText and one ImageButton. When I click the ImageButton for the first time, it adds my second EditText and my second ImageButton underneath my first.



BUT, the problem arises when I click my button the second time. The third row gets inserted in the top of the RecyclerView instead of the bottom. My already existent 1st and 2nd rows get pushed down (along with the contents of the EditText), and my newly added row goes to the top of the recycler.



It gets even weirder, when I click on the "remove" button on my newly inserted top row. It removes one row (as expected) and it clears all my EditTexts (WHY).



If anyone can look at this and tell me where I am making a mistake, I'd appreciate it. I can provide more of my code if needed.










share|improve this question






























    1















    I have a Fragment inside of my MainActivity that contains a RecyclerView being populated by a RecyclerView.Adapter. The RecyclerView inflates row_layout.xml file, which has one EditText and one ImageButton, like this:



    enter image description here



    The button is initially loaded with an "add" image. When clicked, my adapter does this:



    package com.fragments.homework04;

    import android.support.v7.widget.RecyclerView;
    import android.util.Log;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.EditText;
    import android.widget.ImageView;

    import java.util.ArrayList;

    public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder>
    public static int positionAdapter;

    private ArrayList<ItemData> itemsData;
    AdapterButtonInterface mListener;

    public MyAdapter(ArrayList<ItemData> itemsData, AdapterButtonInterface adapterButtonInterface)
    this.itemsData = itemsData;
    try
    mListener = adapterButtonInterface;

    catch(ClassCastException e)
    throw new ClassCastException("Class does not implement interface!");



    public static class ViewHolder extends RecyclerView.ViewHolder

    public EditText txtIngredients;
    public ImageView imgViewIcon;

    public ViewHolder(View itemLayoutView)
    super(itemLayoutView);
    txtIngredients = itemLayoutView.findViewById(R.id.etIngredientName);
    imgViewIcon = itemLayoutView.findViewById(R.id.btnAddRemoveIngredient);



    @Override
    public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
    View itemLayoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_layout, null);
    Log.d("itemsSizeAdapter", String.valueOf(itemsData.size()));
    ViewHolder viewHolder = new ViewHolder(itemLayoutView);
    return viewHolder;


    @Override
    public void onBindViewHolder(final ViewHolder viewHolder, final int position)
    viewHolder.imgViewIcon.setTag(itemsData.get(position).getTag());
    viewHolder.imgViewIcon.setImageResource(itemsData.get(position).getImageUrl());
    viewHolder.imgViewIcon.setOnClickListener(new View.OnClickListener()
    @Override
    public void onClick(View v)
    positionAdapter = viewHolder.getAdapterPosition();
    if(itemsData.get(position).getTag().contains("Add"))
    if(getItemCount() < 5)
    itemsData.get(position).setIngredientValue(viewHolder.txtIngredients.getText().toString());
    mListener.addBtnClicked(positionAdapter, getItemCount(), itemsData.get(position).getIngredientValue());


    else
    if(getItemCount() > 0)
    Log.d("adapterPosition:", String.valueOf(position));
    mListener.removeBtnClicked(positionAdapter, getItemCount());



    );


    // Return the size of your itemsData (invoked by the layout manager)
    @Override
    public int getItemCount()
    return itemsData.size();


    public interface AdapterButtonInterface
    void addBtnClicked(int position, int itemCount, String ingredientValue);
    void removeBtnClicked(int position, int itemCount);




    Basically calls the method in the interface, that is being implemented by my fragment. In my fragment, I am doing this:



    ArrayList<ItemData> itemsData;
    public void addBtnClicked(int position, int itemCount, String ingredientValue)
    if(itemCount == 4)
    //Requirement that there can only be 5 rows at any time.
    //Hence when itemCount == 4, we are adding the 5th row and it needs to be a remove instead of add
    itemsData.add(position + 1, new ItemData("Remove" + itemCount, R.mipmap.remove_ing, ""));

    else
    itemsData.add(position + 1, new ItemData("Add" + itemCount, R.mipmap.add_ing, ""));

    //When the row is added, the EditText's value isn't stored anywhere
    //So I am adding it to the arraylist I have
    itemsData.get(position).setIngredientValue(ingredientValue);
    itemsData.get(position).setImageUrl(R.mipmap.remove_ing);
    itemsData.get(position).setTag("Remove");
    mAdapter.notifyDataSetChanged();


    @Override
    public void removeBtnClicked(int position, int itemCount)
    Log.d("listenerPosition:", String.valueOf(position));
    itemsData.remove(position);
    if(itemCount == 5)
    itemsData.get(3).setImageUrl(R.mipmap.add_ing);
    itemsData.get(3).setTag("Add3");

    mAdapter.notifyDataSetChanged();



    A good bit of this works perfectly. When my app loads, I see one EditText and one ImageButton. When I click the ImageButton for the first time, it adds my second EditText and my second ImageButton underneath my first.



    BUT, the problem arises when I click my button the second time. The third row gets inserted in the top of the RecyclerView instead of the bottom. My already existent 1st and 2nd rows get pushed down (along with the contents of the EditText), and my newly added row goes to the top of the recycler.



    It gets even weirder, when I click on the "remove" button on my newly inserted top row. It removes one row (as expected) and it clears all my EditTexts (WHY).



    If anyone can look at this and tell me where I am making a mistake, I'd appreciate it. I can provide more of my code if needed.










    share|improve this question


























      1












      1








      1


      0






      I have a Fragment inside of my MainActivity that contains a RecyclerView being populated by a RecyclerView.Adapter. The RecyclerView inflates row_layout.xml file, which has one EditText and one ImageButton, like this:



      enter image description here



      The button is initially loaded with an "add" image. When clicked, my adapter does this:



      package com.fragments.homework04;

      import android.support.v7.widget.RecyclerView;
      import android.util.Log;
      import android.view.LayoutInflater;
      import android.view.View;
      import android.view.ViewGroup;
      import android.widget.EditText;
      import android.widget.ImageView;

      import java.util.ArrayList;

      public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder>
      public static int positionAdapter;

      private ArrayList<ItemData> itemsData;
      AdapterButtonInterface mListener;

      public MyAdapter(ArrayList<ItemData> itemsData, AdapterButtonInterface adapterButtonInterface)
      this.itemsData = itemsData;
      try
      mListener = adapterButtonInterface;

      catch(ClassCastException e)
      throw new ClassCastException("Class does not implement interface!");



      public static class ViewHolder extends RecyclerView.ViewHolder

      public EditText txtIngredients;
      public ImageView imgViewIcon;

      public ViewHolder(View itemLayoutView)
      super(itemLayoutView);
      txtIngredients = itemLayoutView.findViewById(R.id.etIngredientName);
      imgViewIcon = itemLayoutView.findViewById(R.id.btnAddRemoveIngredient);



      @Override
      public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
      View itemLayoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_layout, null);
      Log.d("itemsSizeAdapter", String.valueOf(itemsData.size()));
      ViewHolder viewHolder = new ViewHolder(itemLayoutView);
      return viewHolder;


      @Override
      public void onBindViewHolder(final ViewHolder viewHolder, final int position)
      viewHolder.imgViewIcon.setTag(itemsData.get(position).getTag());
      viewHolder.imgViewIcon.setImageResource(itemsData.get(position).getImageUrl());
      viewHolder.imgViewIcon.setOnClickListener(new View.OnClickListener()
      @Override
      public void onClick(View v)
      positionAdapter = viewHolder.getAdapterPosition();
      if(itemsData.get(position).getTag().contains("Add"))
      if(getItemCount() < 5)
      itemsData.get(position).setIngredientValue(viewHolder.txtIngredients.getText().toString());
      mListener.addBtnClicked(positionAdapter, getItemCount(), itemsData.get(position).getIngredientValue());


      else
      if(getItemCount() > 0)
      Log.d("adapterPosition:", String.valueOf(position));
      mListener.removeBtnClicked(positionAdapter, getItemCount());



      );


      // Return the size of your itemsData (invoked by the layout manager)
      @Override
      public int getItemCount()
      return itemsData.size();


      public interface AdapterButtonInterface
      void addBtnClicked(int position, int itemCount, String ingredientValue);
      void removeBtnClicked(int position, int itemCount);




      Basically calls the method in the interface, that is being implemented by my fragment. In my fragment, I am doing this:



      ArrayList<ItemData> itemsData;
      public void addBtnClicked(int position, int itemCount, String ingredientValue)
      if(itemCount == 4)
      //Requirement that there can only be 5 rows at any time.
      //Hence when itemCount == 4, we are adding the 5th row and it needs to be a remove instead of add
      itemsData.add(position + 1, new ItemData("Remove" + itemCount, R.mipmap.remove_ing, ""));

      else
      itemsData.add(position + 1, new ItemData("Add" + itemCount, R.mipmap.add_ing, ""));

      //When the row is added, the EditText's value isn't stored anywhere
      //So I am adding it to the arraylist I have
      itemsData.get(position).setIngredientValue(ingredientValue);
      itemsData.get(position).setImageUrl(R.mipmap.remove_ing);
      itemsData.get(position).setTag("Remove");
      mAdapter.notifyDataSetChanged();


      @Override
      public void removeBtnClicked(int position, int itemCount)
      Log.d("listenerPosition:", String.valueOf(position));
      itemsData.remove(position);
      if(itemCount == 5)
      itemsData.get(3).setImageUrl(R.mipmap.add_ing);
      itemsData.get(3).setTag("Add3");

      mAdapter.notifyDataSetChanged();



      A good bit of this works perfectly. When my app loads, I see one EditText and one ImageButton. When I click the ImageButton for the first time, it adds my second EditText and my second ImageButton underneath my first.



      BUT, the problem arises when I click my button the second time. The third row gets inserted in the top of the RecyclerView instead of the bottom. My already existent 1st and 2nd rows get pushed down (along with the contents of the EditText), and my newly added row goes to the top of the recycler.



      It gets even weirder, when I click on the "remove" button on my newly inserted top row. It removes one row (as expected) and it clears all my EditTexts (WHY).



      If anyone can look at this and tell me where I am making a mistake, I'd appreciate it. I can provide more of my code if needed.










      share|improve this question
















      I have a Fragment inside of my MainActivity that contains a RecyclerView being populated by a RecyclerView.Adapter. The RecyclerView inflates row_layout.xml file, which has one EditText and one ImageButton, like this:



      enter image description here



      The button is initially loaded with an "add" image. When clicked, my adapter does this:



      package com.fragments.homework04;

      import android.support.v7.widget.RecyclerView;
      import android.util.Log;
      import android.view.LayoutInflater;
      import android.view.View;
      import android.view.ViewGroup;
      import android.widget.EditText;
      import android.widget.ImageView;

      import java.util.ArrayList;

      public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder>
      public static int positionAdapter;

      private ArrayList<ItemData> itemsData;
      AdapterButtonInterface mListener;

      public MyAdapter(ArrayList<ItemData> itemsData, AdapterButtonInterface adapterButtonInterface)
      this.itemsData = itemsData;
      try
      mListener = adapterButtonInterface;

      catch(ClassCastException e)
      throw new ClassCastException("Class does not implement interface!");



      public static class ViewHolder extends RecyclerView.ViewHolder

      public EditText txtIngredients;
      public ImageView imgViewIcon;

      public ViewHolder(View itemLayoutView)
      super(itemLayoutView);
      txtIngredients = itemLayoutView.findViewById(R.id.etIngredientName);
      imgViewIcon = itemLayoutView.findViewById(R.id.btnAddRemoveIngredient);



      @Override
      public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
      View itemLayoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_layout, null);
      Log.d("itemsSizeAdapter", String.valueOf(itemsData.size()));
      ViewHolder viewHolder = new ViewHolder(itemLayoutView);
      return viewHolder;


      @Override
      public void onBindViewHolder(final ViewHolder viewHolder, final int position)
      viewHolder.imgViewIcon.setTag(itemsData.get(position).getTag());
      viewHolder.imgViewIcon.setImageResource(itemsData.get(position).getImageUrl());
      viewHolder.imgViewIcon.setOnClickListener(new View.OnClickListener()
      @Override
      public void onClick(View v)
      positionAdapter = viewHolder.getAdapterPosition();
      if(itemsData.get(position).getTag().contains("Add"))
      if(getItemCount() < 5)
      itemsData.get(position).setIngredientValue(viewHolder.txtIngredients.getText().toString());
      mListener.addBtnClicked(positionAdapter, getItemCount(), itemsData.get(position).getIngredientValue());


      else
      if(getItemCount() > 0)
      Log.d("adapterPosition:", String.valueOf(position));
      mListener.removeBtnClicked(positionAdapter, getItemCount());



      );


      // Return the size of your itemsData (invoked by the layout manager)
      @Override
      public int getItemCount()
      return itemsData.size();


      public interface AdapterButtonInterface
      void addBtnClicked(int position, int itemCount, String ingredientValue);
      void removeBtnClicked(int position, int itemCount);




      Basically calls the method in the interface, that is being implemented by my fragment. In my fragment, I am doing this:



      ArrayList<ItemData> itemsData;
      public void addBtnClicked(int position, int itemCount, String ingredientValue)
      if(itemCount == 4)
      //Requirement that there can only be 5 rows at any time.
      //Hence when itemCount == 4, we are adding the 5th row and it needs to be a remove instead of add
      itemsData.add(position + 1, new ItemData("Remove" + itemCount, R.mipmap.remove_ing, ""));

      else
      itemsData.add(position + 1, new ItemData("Add" + itemCount, R.mipmap.add_ing, ""));

      //When the row is added, the EditText's value isn't stored anywhere
      //So I am adding it to the arraylist I have
      itemsData.get(position).setIngredientValue(ingredientValue);
      itemsData.get(position).setImageUrl(R.mipmap.remove_ing);
      itemsData.get(position).setTag("Remove");
      mAdapter.notifyDataSetChanged();


      @Override
      public void removeBtnClicked(int position, int itemCount)
      Log.d("listenerPosition:", String.valueOf(position));
      itemsData.remove(position);
      if(itemCount == 5)
      itemsData.get(3).setImageUrl(R.mipmap.add_ing);
      itemsData.get(3).setTag("Add3");

      mAdapter.notifyDataSetChanged();



      A good bit of this works perfectly. When my app loads, I see one EditText and one ImageButton. When I click the ImageButton for the first time, it adds my second EditText and my second ImageButton underneath my first.



      BUT, the problem arises when I click my button the second time. The third row gets inserted in the top of the RecyclerView instead of the bottom. My already existent 1st and 2nd rows get pushed down (along with the contents of the EditText), and my newly added row goes to the top of the recycler.



      It gets even weirder, when I click on the "remove" button on my newly inserted top row. It removes one row (as expected) and it clears all my EditTexts (WHY).



      If anyone can look at this and tell me where I am making a mistake, I'd appreciate it. I can provide more of my code if needed.







      java android android-fragments android-recyclerview






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 25 at 7:53









      Sree

      1,0041022




      1,0041022










      asked Mar 25 at 0:09









      Crazy CucumberCrazy Cucumber

      390623




      390623






















          2 Answers
          2






          active

          oldest

          votes


















          2














          You specify that the position is final in the ViewHolder so you can reference it in the onClick() handler. Since that variable is final it will not change regardless of any new position that the ViewHolder may take. Instead of a final variable, try using getAdapterPosition().




          getAdapterPosition


          int getAdapterPosition ()


          Returns the Adapter position of the item represented by this ViewHolder.




          This may not be your problem, but give it a try.






          share|improve this answer























          • Thank you for that. A little bit of reading on that function taught me not use to the position variable to handle clicks. So I've changed my code to use getAdapterPosition. I still have the same problem, but I am glad I learned a better way to do this.

            – Crazy Cucumber
            Mar 25 at 1:03











          • @CrazyCucumber Post the entire adapter if you can.

            – Cheticamp
            Mar 25 at 2:11











          • Updated the question with the entire adapter.

            – Crazy Cucumber
            Mar 25 at 2:15











          • @CrazyCucumber It's not apparent to me what is wrong. I suggest that you check the value of position in addBtnClicked(). The position could be -1 (NO_POSITION) if the RecyclerView is mid-layout and -1+1 == 0, so adds would occur at the beginning. Not sure why this would be, but it's worth checking.

            – Cheticamp
            Mar 25 at 10:38












          • I had several Log messages earlier, some for the position variable, some for the getAdapterPosition variable, some in the fragment. They all seems to make perfect sense. The size of the ArrayList increases first, and then when the adapter is notified of data change, the size of the recycler view increases. Everything is as expected. I just don't know what else could possibly be wrong.

            – Crazy Cucumber
            Mar 25 at 13:42


















          0














          Use this two methods in adapter



          @Override
          public long getItemId(int position)
          return super.getItemId(position);


          @Override
          public int getItemViewType(int position)
          return super.getItemViewType(position);






          share|improve this answer























          • Thank you for your answer. What do you want me to do with these two functions? How does this help with my problem?

            – Crazy Cucumber
            Mar 25 at 5:21











          • all data will come with proper position in recyclerview,your data will not get change

            – Sejpal Pavan
            Mar 25 at 5:25











          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',
          autoActivateHeartbeat: false,
          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%2f55329793%2frecyclerview-adapter-inserting-rows-in-weird-positions%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          You specify that the position is final in the ViewHolder so you can reference it in the onClick() handler. Since that variable is final it will not change regardless of any new position that the ViewHolder may take. Instead of a final variable, try using getAdapterPosition().




          getAdapterPosition


          int getAdapterPosition ()


          Returns the Adapter position of the item represented by this ViewHolder.




          This may not be your problem, but give it a try.






          share|improve this answer























          • Thank you for that. A little bit of reading on that function taught me not use to the position variable to handle clicks. So I've changed my code to use getAdapterPosition. I still have the same problem, but I am glad I learned a better way to do this.

            – Crazy Cucumber
            Mar 25 at 1:03











          • @CrazyCucumber Post the entire adapter if you can.

            – Cheticamp
            Mar 25 at 2:11











          • Updated the question with the entire adapter.

            – Crazy Cucumber
            Mar 25 at 2:15











          • @CrazyCucumber It's not apparent to me what is wrong. I suggest that you check the value of position in addBtnClicked(). The position could be -1 (NO_POSITION) if the RecyclerView is mid-layout and -1+1 == 0, so adds would occur at the beginning. Not sure why this would be, but it's worth checking.

            – Cheticamp
            Mar 25 at 10:38












          • I had several Log messages earlier, some for the position variable, some for the getAdapterPosition variable, some in the fragment. They all seems to make perfect sense. The size of the ArrayList increases first, and then when the adapter is notified of data change, the size of the recycler view increases. Everything is as expected. I just don't know what else could possibly be wrong.

            – Crazy Cucumber
            Mar 25 at 13:42















          2














          You specify that the position is final in the ViewHolder so you can reference it in the onClick() handler. Since that variable is final it will not change regardless of any new position that the ViewHolder may take. Instead of a final variable, try using getAdapterPosition().




          getAdapterPosition


          int getAdapterPosition ()


          Returns the Adapter position of the item represented by this ViewHolder.




          This may not be your problem, but give it a try.






          share|improve this answer























          • Thank you for that. A little bit of reading on that function taught me not use to the position variable to handle clicks. So I've changed my code to use getAdapterPosition. I still have the same problem, but I am glad I learned a better way to do this.

            – Crazy Cucumber
            Mar 25 at 1:03











          • @CrazyCucumber Post the entire adapter if you can.

            – Cheticamp
            Mar 25 at 2:11











          • Updated the question with the entire adapter.

            – Crazy Cucumber
            Mar 25 at 2:15











          • @CrazyCucumber It's not apparent to me what is wrong. I suggest that you check the value of position in addBtnClicked(). The position could be -1 (NO_POSITION) if the RecyclerView is mid-layout and -1+1 == 0, so adds would occur at the beginning. Not sure why this would be, but it's worth checking.

            – Cheticamp
            Mar 25 at 10:38












          • I had several Log messages earlier, some for the position variable, some for the getAdapterPosition variable, some in the fragment. They all seems to make perfect sense. The size of the ArrayList increases first, and then when the adapter is notified of data change, the size of the recycler view increases. Everything is as expected. I just don't know what else could possibly be wrong.

            – Crazy Cucumber
            Mar 25 at 13:42













          2












          2








          2







          You specify that the position is final in the ViewHolder so you can reference it in the onClick() handler. Since that variable is final it will not change regardless of any new position that the ViewHolder may take. Instead of a final variable, try using getAdapterPosition().




          getAdapterPosition


          int getAdapterPosition ()


          Returns the Adapter position of the item represented by this ViewHolder.




          This may not be your problem, but give it a try.






          share|improve this answer













          You specify that the position is final in the ViewHolder so you can reference it in the onClick() handler. Since that variable is final it will not change regardless of any new position that the ViewHolder may take. Instead of a final variable, try using getAdapterPosition().




          getAdapterPosition


          int getAdapterPosition ()


          Returns the Adapter position of the item represented by this ViewHolder.




          This may not be your problem, but give it a try.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 25 at 0:37









          CheticampCheticamp

          31.4k53267




          31.4k53267












          • Thank you for that. A little bit of reading on that function taught me not use to the position variable to handle clicks. So I've changed my code to use getAdapterPosition. I still have the same problem, but I am glad I learned a better way to do this.

            – Crazy Cucumber
            Mar 25 at 1:03











          • @CrazyCucumber Post the entire adapter if you can.

            – Cheticamp
            Mar 25 at 2:11











          • Updated the question with the entire adapter.

            – Crazy Cucumber
            Mar 25 at 2:15











          • @CrazyCucumber It's not apparent to me what is wrong. I suggest that you check the value of position in addBtnClicked(). The position could be -1 (NO_POSITION) if the RecyclerView is mid-layout and -1+1 == 0, so adds would occur at the beginning. Not sure why this would be, but it's worth checking.

            – Cheticamp
            Mar 25 at 10:38












          • I had several Log messages earlier, some for the position variable, some for the getAdapterPosition variable, some in the fragment. They all seems to make perfect sense. The size of the ArrayList increases first, and then when the adapter is notified of data change, the size of the recycler view increases. Everything is as expected. I just don't know what else could possibly be wrong.

            – Crazy Cucumber
            Mar 25 at 13:42

















          • Thank you for that. A little bit of reading on that function taught me not use to the position variable to handle clicks. So I've changed my code to use getAdapterPosition. I still have the same problem, but I am glad I learned a better way to do this.

            – Crazy Cucumber
            Mar 25 at 1:03











          • @CrazyCucumber Post the entire adapter if you can.

            – Cheticamp
            Mar 25 at 2:11











          • Updated the question with the entire adapter.

            – Crazy Cucumber
            Mar 25 at 2:15











          • @CrazyCucumber It's not apparent to me what is wrong. I suggest that you check the value of position in addBtnClicked(). The position could be -1 (NO_POSITION) if the RecyclerView is mid-layout and -1+1 == 0, so adds would occur at the beginning. Not sure why this would be, but it's worth checking.

            – Cheticamp
            Mar 25 at 10:38












          • I had several Log messages earlier, some for the position variable, some for the getAdapterPosition variable, some in the fragment. They all seems to make perfect sense. The size of the ArrayList increases first, and then when the adapter is notified of data change, the size of the recycler view increases. Everything is as expected. I just don't know what else could possibly be wrong.

            – Crazy Cucumber
            Mar 25 at 13:42
















          Thank you for that. A little bit of reading on that function taught me not use to the position variable to handle clicks. So I've changed my code to use getAdapterPosition. I still have the same problem, but I am glad I learned a better way to do this.

          – Crazy Cucumber
          Mar 25 at 1:03





          Thank you for that. A little bit of reading on that function taught me not use to the position variable to handle clicks. So I've changed my code to use getAdapterPosition. I still have the same problem, but I am glad I learned a better way to do this.

          – Crazy Cucumber
          Mar 25 at 1:03













          @CrazyCucumber Post the entire adapter if you can.

          – Cheticamp
          Mar 25 at 2:11





          @CrazyCucumber Post the entire adapter if you can.

          – Cheticamp
          Mar 25 at 2:11













          Updated the question with the entire adapter.

          – Crazy Cucumber
          Mar 25 at 2:15





          Updated the question with the entire adapter.

          – Crazy Cucumber
          Mar 25 at 2:15













          @CrazyCucumber It's not apparent to me what is wrong. I suggest that you check the value of position in addBtnClicked(). The position could be -1 (NO_POSITION) if the RecyclerView is mid-layout and -1+1 == 0, so adds would occur at the beginning. Not sure why this would be, but it's worth checking.

          – Cheticamp
          Mar 25 at 10:38






          @CrazyCucumber It's not apparent to me what is wrong. I suggest that you check the value of position in addBtnClicked(). The position could be -1 (NO_POSITION) if the RecyclerView is mid-layout and -1+1 == 0, so adds would occur at the beginning. Not sure why this would be, but it's worth checking.

          – Cheticamp
          Mar 25 at 10:38














          I had several Log messages earlier, some for the position variable, some for the getAdapterPosition variable, some in the fragment. They all seems to make perfect sense. The size of the ArrayList increases first, and then when the adapter is notified of data change, the size of the recycler view increases. Everything is as expected. I just don't know what else could possibly be wrong.

          – Crazy Cucumber
          Mar 25 at 13:42





          I had several Log messages earlier, some for the position variable, some for the getAdapterPosition variable, some in the fragment. They all seems to make perfect sense. The size of the ArrayList increases first, and then when the adapter is notified of data change, the size of the recycler view increases. Everything is as expected. I just don't know what else could possibly be wrong.

          – Crazy Cucumber
          Mar 25 at 13:42













          0














          Use this two methods in adapter



          @Override
          public long getItemId(int position)
          return super.getItemId(position);


          @Override
          public int getItemViewType(int position)
          return super.getItemViewType(position);






          share|improve this answer























          • Thank you for your answer. What do you want me to do with these two functions? How does this help with my problem?

            – Crazy Cucumber
            Mar 25 at 5:21











          • all data will come with proper position in recyclerview,your data will not get change

            – Sejpal Pavan
            Mar 25 at 5:25















          0














          Use this two methods in adapter



          @Override
          public long getItemId(int position)
          return super.getItemId(position);


          @Override
          public int getItemViewType(int position)
          return super.getItemViewType(position);






          share|improve this answer























          • Thank you for your answer. What do you want me to do with these two functions? How does this help with my problem?

            – Crazy Cucumber
            Mar 25 at 5:21











          • all data will come with proper position in recyclerview,your data will not get change

            – Sejpal Pavan
            Mar 25 at 5:25













          0












          0








          0







          Use this two methods in adapter



          @Override
          public long getItemId(int position)
          return super.getItemId(position);


          @Override
          public int getItemViewType(int position)
          return super.getItemViewType(position);






          share|improve this answer













          Use this two methods in adapter



          @Override
          public long getItemId(int position)
          return super.getItemId(position);


          @Override
          public int getItemViewType(int position)
          return super.getItemViewType(position);







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 25 at 5:19









          Sejpal PavanSejpal Pavan

          717




          717












          • Thank you for your answer. What do you want me to do with these two functions? How does this help with my problem?

            – Crazy Cucumber
            Mar 25 at 5:21











          • all data will come with proper position in recyclerview,your data will not get change

            – Sejpal Pavan
            Mar 25 at 5:25

















          • Thank you for your answer. What do you want me to do with these two functions? How does this help with my problem?

            – Crazy Cucumber
            Mar 25 at 5:21











          • all data will come with proper position in recyclerview,your data will not get change

            – Sejpal Pavan
            Mar 25 at 5:25
















          Thank you for your answer. What do you want me to do with these two functions? How does this help with my problem?

          – Crazy Cucumber
          Mar 25 at 5:21





          Thank you for your answer. What do you want me to do with these two functions? How does this help with my problem?

          – Crazy Cucumber
          Mar 25 at 5:21













          all data will come with proper position in recyclerview,your data will not get change

          – Sejpal Pavan
          Mar 25 at 5:25





          all data will come with proper position in recyclerview,your data will not get change

          – Sejpal Pavan
          Mar 25 at 5:25

















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55329793%2frecyclerview-adapter-inserting-rows-in-weird-positions%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

          Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

          Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

          Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript