I have two list<> With different data but that are equal in size , I want to use these two lists in one RecyclerView And I want to fill RecyclerView items using these two lists, but items that are filled with the second list are empty in RecyclerView and Just Items that are filled with the first list are show.
i dont know how to use both List in one recyclerView.
RecyclerViewAdapter :
public class RecyclerViewAdapter_StudentList extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
private List<StudentTable> studentData = new ArrayList<>();//First List
private List<PerformanceTable> performanceData = new ArrayList<>();//Second List
private Context context;
private DatabaseHandler database;
public RecyclerViewAdapter_StudentList(Context context , List<StudentTable> tableData , List<PerformanceTable> performanceData)
{
this.context = context;
this.studentData = tableData;
this.performanceData = performanceData;
database = new DatabaseHandler(context);
}
public class itemHolder extends RecyclerView.ViewHolder
{
TextView studentName ;
TextView studentPositive;
TextView studentNegative;
public itemHolder(View itemView)
{
super(itemView);
studentName = (TextView) itemView.findViewById(R.id.item_txt_StudentList_StudentName);
studentPositive = (TextView) itemView.findViewById(R.id.item_StudentList_Positive);
studentNegative = (TextView) itemView.findViewById(R.id.item_StudentList_Negative);
}
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.recyclerview_item_student_list, parent, false);
return new itemHolder(view);
}
#Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
final itemHolder itemHolder = (itemHolder) holder;
final String studentName = studentData.get(position).getStudentName();
itemHolder.studentName.setText(String.valueOf(studentName));
int pCount = performanceData.get(position).getPositive();
int nCount = performanceData.get(position).getNegative();
itemHolder.studentPositive.setText(String.valueOf(pCount));//These items are empty
itemHolder.studentNegative.setText(String.valueOf(nCount));//These items are empty
.
.
.
}
public int getItemCount()
{
return studentData.size();
}
#Nima Khalili
please go through below links this might what you r asking for.
https://stackoverflow.com/a/26245463/4361438
https://www.journaldev.com/12372/android-recyclerview-example
i will explain it if this will fix your problem.
You can do something like this....
List<Model1> list1 = new ArrayList<>();
List<Model2> list2 = new ArrayList<>();
List<BothModels> list3 = new ArrayList<>();
list1.add(new Model1(whatever here));
//add more to list1
list2.add(new Model2(whatever here));
//add more to list2
list3.addAll(list1);
list3.addAll(list2);
Now that everything is in list3, you can use that list to fill in your recycler view.
Its also possible to achieve it with polymorphism... using an interface. Might actually be a better way now that I think about it. If you have no idea what polymorphism is or if you never created an interface before, I would recommend reading up on it. I wont cover that in this answer.
Here is also another answer this is way better....
https://stackoverflow.com/a/13706974/8200290
Related
I am making an application that takes all users of a table in Firebase and shows them in CardView, but I would like administrators to appear before the other users.
For this differentiation, all users were saved to the system with an attribute called type which says whether they are normal users or administrators.
This is TelaChat.java file:
public class TelaChat extends AppCompatActivity {
private RecyclerView recyclerView;
private DatabaseReference databaseReference;
private FirebaseRecyclerOptions<Usuario> options;
private FirebaseRecyclerAdapter<Usuario, ChatAdapter> adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat);
setTitle("Tela de Chat");
recyclerView = findViewById(R.id.cm_rv_membros);
recyclerView.setHasFixedSize(true);
databaseReference = FirebaseDatabase.getInstance().getReference().child("usuario");
options = new FirebaseRecyclerOptions.Builder<Usuario>().setQuery(databaseReference, Usuario.class).build();
adapter = new FirebaseRecyclerAdapter<Usuario, ChatAdapter>(options) {
#Override
protected void onBindViewHolder(ChatAdapter holder, int position, Usuario model) {
Picasso.with(holder.ivFoto.getContext()).load(model.getImageURL()).centerCrop().resize(140, 140).transform(new ImageTrans_CircleTransform2()).into(holder.ivFoto);
holder.tvNome.setText(model.getNome());
holder.tvTipo.setText(model.getTipo());
}
#Override
public ChatAdapter onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.chat_card, parent, false);
return new ChatAdapter(view);
}
};
GridLayoutManager gridLayoutManager = new GridLayoutManager(getApplicationContext(), 1);
recyclerView.setLayoutManager(gridLayoutManager);
adapter.startListening();
recyclerView.setAdapter(adapter);
}
This is ChatAdapter.java file:
public class ChatAdapter extends RecyclerView.ViewHolder{
public ImageView ivFoto;
public TextView tvNome;
public TextView tvTipo;
public ChatAdapter(View itemView) {
super(itemView);
ivFoto = itemView.findViewById(R.id.cc_iv_foto);
tvNome = itemView.findViewById(R.id.cc_tv_nome);
tvTipo = itemView.findViewById(R.id.cc_tv_tipo);
}
}
I've looked for some tutorials on the internet but none has specified what I want to do. Can someone help me?
Try to add this: orderByChild("type")
databaseReference = FirebaseDatabase.getInstance().getReference().child("usuario").orderByChild("type");
Or here:
options = new FirebaseRecyclerOptions.Builder<Usuario>().setQuery(databaseReference.orderByChild("type"), Usuario.class).build();
Sorry I can't test it right now, hope it works.
I have an idea may help in this set a flag for example called position with initial value 0 and create a new list to order on it and check the type and if is an admin add him in the list with position value and increase the position value else add the value without index
private int position = 0;
private List<Users> myList = new ArrayList();
for(int i = 0 ; i < usersList.size ; i++){
if(userList.get(i).getType.equal("adminstrator")){
myList.add(position,userList.get(i))
position++;
}else{
myList.add(usetList.get(i))
}
}
after set this list "myList" to the recyclerview adapter
I am new in Android Development and working on a project where I need to call an API after every one second, in that API there is field "Amount"(dBID) which keeps on changing, so I need to update the latest Amount (dBID) in recyclerview.
In order to do so, I have called this API in a service after every interval of one second.
The data is Showing Properly no Issue.
But for Now I need to perform some action on the Old Amount and New Amount.
Action Required : I need to compare the old value (dBID) with the New Value (dBID).
If the New Value is greater then I need to change the Text Color of Amount (dBID) to BLUE.
If the New Value is smaller then I need to change the Text Color of Amount (dBID) to RED.
Tried to achieve this by storing the old data in a Variable and then Comparing it to the new Value.
Issue : This logic is working fine until there are 5 or less Items in recyclerview as soon as the sixth item is added the same logic does not work.
Help me if anyone knows how I can achieve this.
For Example you can refer an App Vertexfx : Quotes Tab.
Below is the Code which I Tried.
Adapter class of the RecyclerView:
public class QuoteAdapter extends RecyclerView.Adapter <QuoteAdapter.MyViewHolder>{
Context context;
List<QuoteData> data;
public QuoteAdapter(Context context,List<QuoteData> data)
{
this.data = data;
this.context = context;
}
class MyViewHolder extends RecyclerView.ViewHolder{
TextView time,symbol,sellmax,selllow,buymax,buylow,buy,sell,spread,lowtext,hightext;
LinearLayout layout,layoutbid,layoutask;
float currentbid,lastbid,currentask,lastask;
public MyViewHolder(#NonNull View itemView) {
super(itemView);
time = itemView.findViewById(R.id.TVTime);
symbol = itemView.findViewById(R.id.TVSymbol);
sellmax = itemView.findViewById(R.id.TVSELLMAX);
selllow = itemView.findViewById(R.id.TVSELLLOW);
buymax = itemView.findViewById(R.id.TVBUYMAX);
buylow = itemView.findViewById(R.id.TVBUYHIGH);
buy = itemView.findViewById(R.id.TVBUY);
sell = itemView.findViewById(R.id.TVSELL);
spread = itemView.findViewById(R.id.TVSpread1);
lowtext = itemView.findViewById(R.id.low);
hightext = itemView.findViewById(R.id.high);
layout = itemView.findViewById(R.id.layout);
layoutbid = itemView.findViewById(R.id.LLBid);
layoutask = itemView.findViewById(R.id.LLAsk);
currentbid = 0;
lastbid = 0;
currentask = 0;
lastask = 0;
}
}
#NonNull
#Override
public MyViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.quotelist,viewGroup,false);
return new MyViewHolder(view);
}
#SuppressLint("ResourceAsColor")
#Override
public void onBindViewHolder(#NonNull final MyViewHolder myViewHolder, final int i) {
final QuoteData data1 = data.get(i);
myViewHolder.time.setText(data1.dLut);
myViewHolder.symbol.setText(data1.dSymbol);
myViewHolder.sellmax.setText(data1.dBid); //Bid
myViewHolder.selllow.setText(data1.dLow);
myViewHolder.buymax.setText(data1.dAsk); //ask
myViewHolder.buylow.setText(data1.dHigh);
myViewHolder.currentbid = Float.parseFloat((data1.dBid));
myViewHolder.currentask = Float.parseFloat((data1.dAsk));
if (myViewHolder.currentbid > myViewHolder.lastbid)
{
myViewHolder.sellmax.setTextColor(Color.BLUE);
}
if (myViewHolder.currentbid < myViewHolder.lastbid)
{
myViewHolder.sellmax.setTextColor(Color.RED);
}
myViewHolder.lastbid = myViewHolder.currentbid;
myViewHolder.lastask = myViewHolder.currentask;
}
});
}
I suggest you take a look at those classes from the Android SDK:
DiffUtil
AsyncListDiffer
ItemAnimator
DiffUtil
DiffUtil is designed to compare existing and new recycler view items and fires appropriate events. You need to pass a callback that can tell if two items are the same and if their content has changed.
AsyncListDiffer
It wraps the DiffUtil and executes it's logic asynchronously, giving better performance.
ItemAnimator
The ItemAnimator for a given RecyclerView is called by default when change events are fired on it's items. You can provide an implementation of the animateChange method to change your color accordingly.
For Future reference I have resolved the above mentioned issue using the below code.
Defined two ArrayList of String in Adapter
public class QuoteAdapter extends RecyclerView.Adapter <QuoteAdapter.MyViewHolder>{
Context context;
List<QuoteData> data;
List<String> olddatabid = new ArrayList<String>();
List<String> newdatabid = new ArrayList<String>();
List<String> olddataask = new ArrayList<String>();
List<String> newdataask = new ArrayList<String>();
class MyViewHolder extends RecyclerView.ViewHolder{
TextView time,symbol,sellmax,selllow,buymax,buylow,buy,sell,spread,lowtext,hightext;
LinearLayout layout,layoutbid,layoutask;
public MyViewHolder(#NonNull View itemView) {
super(itemView);
time = itemView.findViewById(R.id.TVTime);
symbol = itemView.findViewById(R.id.TVSymbol);
sellmax = itemView.findViewById(R.id.TVSELLMAX);
selllow = itemView.findViewById(R.id.TVSELLLOW);
buymax = itemView.findViewById(R.id.TVBUYMAX);
buylow = itemView.findViewById(R.id.TVBUYHIGH);
buy = itemView.findViewById(R.id.TVBUY);
sell = itemView.findViewById(R.id.TVSELL);
spread = itemView.findViewById(R.id.TVSpread1);
lowtext = itemView.findViewById(R.id.low);
hightext = itemView.findViewById(R.id.high);
layout = itemView.findViewById(R.id.layout);
layoutbid = itemView.findViewById(R.id.LLBid);
layoutask = itemView.findViewById(R.id.LLAsk);
}
}
public QuoteAdapter(Context context,List<QuoteData> data)
{
this.data = data;
this.context = context;
}
#NonNull
#Override
public MyViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.quotelist,viewGroup,false);
return new MyViewHolder(view);
}
#SuppressLint("ResourceAsColor")
#Override
public void onBindViewHolder(#NonNull final MyViewHolder myViewHolder, final int i) {
final QuoteData data1 = data.get(i);
myViewHolder.time.setText(data1.dLut);
myViewHolder.symbol.setText(data1.dSymbol);
myViewHolder.sellmax.setText(data1.dBid); //Bid
myViewHolder.selllow.setText(data1.dLow);
myViewHolder.buymax.setText(data1.dAsk); //ask
myViewHolder.buylow.setText(data1.dHigh);
if (newdatabid.size()< data.size())
{
newdatabid.add(data1.dBid); //Insert Value in array for the first time
}
if (olddatabid.size()< data.size())
{
olddatabid.add(data1.dBid); //Insert Value in array for the first time
}
if (newdataask.size()< data.size())
{
newdataask.add(data1.dAsk); //Insert Value in array for the first time
}
if (olddataask.size()< data.size()) //Insert Value in array for the first time
{
olddataask.add(data1.dAsk);
}
newdatabid.set(i,data1.dBid); //Store Value in array
newdataask.set(i,data1.dAsk); //Store Value in array
//Compare and perform Logic
if (Float.valueOf(newdatabid.get(i)) > Float.valueOf(olddatabid.get(i)))
{
myViewHolder.sellmax.setTextColor(Color.BLUE);
}
if (Float.valueOf(newdatabid.get(i)) < Float.valueOf(olddatabid.get(i)))
{
myViewHolder.sellmax.setTextColor(Color.RED);
}
if (Float.valueOf(newdataask.get(i)) > Float.valueOf(olddataask.get(i)))
{
myViewHolder.buymax.setTextColor(Color.BLUE);
}
if (Float.valueOf(newdataask.get(i)) < Float.valueOf(olddataask.get(i)))
{
myViewHolder.buymax.setTextColor(Color.RED);
}
olddatabid.set(i,newdatabid.get(i));
olddataask.set(i,newdataask.get(i));
}
});
}
}
#Override
public int getItemCount() {
return data.size();
}
}
I'm using the cardslib library for having cards in my Android app. It has pre-defined card layouts or we have an option to customize the layout.
While creating the adapter for recycler view, only the list of card objects with pre-defined card layouts are accepted. I get an error when I try to pass a list of custom cards to the constructor.
CardArrayRecyclerViewAdapter cardArrayRecyclerViewAdapter = new CardArrayRecyclerViewAdapter(getApplicationContext(), customCards);
The above code shows an error. customCards is the list of custom card objects. How do I make it work ?
In your CardArrayRecyclerViewAdapter change List<Card> to List<CustomCard> in the constructor
you can write Custom card adapter bellow
card adapter call just like
List<CustomCard> cardlist = new ArrayList<>();
recyclerView.setHasFixedSize(true);
// recyclerView
recyclerView.setLayoutManager(new LinearLayoutManager(this));
// create an Object for Adapter
CardDataAdapter mAdapter = new CardDataAdapter(cardlist);
// set the adapter object to the Recyclerview
recyclerView.setAdapter(mAdapter);
custom Adapter
class CardDataAdapter extends RecyclerView.Adapter<CardDataAdapter.ViewHolder> {
private List<CustomCard> dataSet;
public CardDataAdapter(List<CustomCard> list) {
dataSet = list;
}
#Override
public CardDataAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
// create a new view
View itemLayoutView = LayoutInflater.from(viewGroup.getContext()).inflate(
R.layout.row_custom_card, null);
itemLayoutView.setLayoutParams(new RecyclerView.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT, RecyclerView.LayoutParams.WRAP_CONTENT));
// create ViewHolder
CardDataAdapter.ViewHolder viewHolder = new CardDataAdapter.ViewHolder(itemLayoutView);
return viewHolder;
}
#Override
public void onBindViewHolder(CardDataAdapter.ViewHolder viewHolder, int i) {
CustomCard fp = dataSet.get(i);
viewHolder.tv_title.setText(fp.getName());
viewHolder.feed = fp;
}
#Override
public int getItemCount() {
return dataSet.size();
}
// inner class to hold a reference to each item of RecyclerView
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView tv_title;
public NotificationModel feed;
public ViewHolder(View itemLayoutView) {
super(itemLayoutView);
tv_title = (TextView) itemLayoutView .findViewById(R.id.tv_title);
itemLayoutView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Write here
}
});
}
}
}
note : CustomCard is model & row_custom_card layout xml where name Textview resde
The second argument to the CardArrayRecyclerViewAdapter constructor must be of type List<Card>. I know it seems like List<CustomCard> should work here, but it will not.
It's impossible to say what the best way to solve this problem is without seeing more code, but you could do this:
List<Card> cards = new ArrayList<>();
cards.addAll(customCards);
// now call constructor with `cards` instead of `customCards`
You can read more about why List<CustomCard> is not accepted for the List<Card> argument here Is List<Dog> a subclass of List<Animal>? Why aren't Java's generics implicitly polymorphic?
i am showing arraylist getting from server to recyclerview.now i added pagination, its working fine and showing new list in recyclerview replacing previous list. but i want pagination like new list will concatenate to the existing list and show merged list(like facebok if i scrolldown after showing certain items it added new items to the list, concatenate with last list and show all the items) in recyclerview.
adapter constructor :
public NewsFeedAdapter (ArrayList<NewsFeedClass> newsFeedClassArrayList, Context context ){
this.context=context;
this.newsFeedClassArrayList=newsFeedClassArrayList;
}
adapter code:
public class NewsFeedAdapter extends RecyclerView.Adapter<NewsFeedAdapter.MyViewHolder>{
Context context;
private static ArrayList<NewsFeedClass> newsFeedClassArrayList=new ArrayList<>();
private NewsFeedClass newsFeedClass;
private String videoId="zDlMVlUriLw";
private int totalLikeWow; // total like,dislike,wow,bleh count;
AsyncTaskClass asyncTaskClass;
private NameShowWho_SharedPostAdapter nameShowWhoSharedPostAdapter;
NameShowWho_SharedPost_class nameShowWhoSharedPost_class;
private int changeValue;//like ad/less on button click
private ViewPagerAdapter viewPagerAdapter;
int previousItemsSize;
public NewsFeedAdapter (ArrayList<NewsFeedClass> newsFeedClassArrayList, Context context ){
this.context=context;
// this.newsFeedClassArrayList=newsFeedClassArrayList;
addItems(newsFeedClassArrayList);
}
public class MyViewHolder extends RecyclerView.ViewHolder {
public MyViewHolder(View itemView) {
super(itemView);
}
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.newsfeed_adapter,parent,false);
return new MyViewHolder(view);
}
#Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
}
#Override
public int getItemCount() {
return newsFeedClassArrayList.size();
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public int getItemViewType(int position) {
return position;
}
private void addItems(List<NewsFeedClass> newItems) {
previousItemsSize = newsFeedClassArrayList.size();
// Append the new items to the old items
newsFeedClassArrayList.addAll(newItems);
// Notify the adapter about the newly added items
notifyItemRangeInserted(previousItemsSize, newItems.size());
}
}
please i need help me on this issue. i have tried but did not find any.
Here's one way to achieve this:
Create a new empty List in your adapter class constructor.
Add a new public method to update the adapter data with new items.
For example:
private List<String> mItems; // Demo data source
// Adapter constructor creates an empty list
public MyAdapter() {
mItems = new ArrayList<>();
}
public void addItems(List<String> newItems) {
int previousItemsSize = mItems.size();
// Append the new items to the old items
mItems.addAll(newItems);
// Notify the adapter about the newly added items
notifyItemRangeInserted(previousItemsSize, newItems.size());
}
Now, When you need to add a new set of items - just call the AddItems() method.
You just add item in your adapter like this
public NewsFeedAdapter (ArrayList<NewsFeedClass> newsFeedClassArrayList, Context context ){
this.context=context;
this.newsFeedClassArrayList=newsFeedClassArrayList;
}
after when you get new record then simple to add as below. you need to do this things in you Activity/Fragment class where you can set your adapter in you recyclerview.
adapter.addItems(newsFeedClassArrayList);
You should use addAll() to add data to existing list after receiving data from server originalList.addAll(listFromServer);
and then notify your adapter.
Let me know if it helps.
file with working code to test my issue, you have to add 2 items, then delete any of those and then add a new one to see how the deleted gets on top of the newly addded
https://www.dropbox.com/s/ojuyz5g5f3kaz0h/Test.zip?dl=0
I have a problem when deleting an item from the recyclerview, when ever I delete an item, IF I add a new item, the deleted item will appear in top of the newly added item, how could I get a fresh view or avod this from happening as is a big issue.
this is how i add items from my main activity
if (!resta || (diff > (3*60*1000)))
{
Ri.add(dataroot);
Integer position = adapter.getItemCount() + 1;
adapter.notifyItemInserted(position);
}
here my Adapter
public class ComandaAdapter extends RecyclerView.Adapter<ComandaAdapter.ComandaAdapterViewHolder>{
private Context mContext;
private ArrayList<Integer> lista_entradas = new ArrayList<>();
private ArrayList<Integer> lista_fondos = new ArrayList<>();
private ArrayList<Integer> lista_postres= new ArrayList<>();
private Boolean primeritem;
private ArrayList<DataRoot> Rir;
private TextView txt_comandas;
private TextView txt_entracola;
private TextView txt_fondocola;
private TextView txt_postrecola;
public ComandaAdapter(Context context, TextView tx_entracola, TextView tx_fondocola, TextView tx_postrecola, TextView tx_comandas, ArrayList<DataRoot> Rir)
{
this.mContext = context;
this.txt_comandas = tx_comandas;
this.txt_entracola = tx_entracola;
this.txt_fondocola = tx_fondocola;
this.txt_postrecola = tx_postrecola;
this.Rir= Rir;
}
#Override
public ComandaAdapter.ComandaAdapterViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
{
return new ComandaAdapter.ComandaAdapterViewHolder(LayoutInflater.from(mContext).inflate(R.layout.row,parent,false));
}
#Override
public void onBindViewHolder(final ComandaAdapter.ComandaAdapterViewHolder holder, final int position)
{
DataRoot Rdata = Rir.get(position);
holder.setdata(Rdata);
}
public void delete(int position)
{
Rir.remove(position);
notifyItemRemoved(position);
}
public class ComandaAdapterViewHolder extends RecyclerView.ViewHolder
{
Button btn_cerrar;
public ComandaAdapterViewHolder(View itemView)
{
super(itemView);
btn_cerrar = (Button) itemView.findViewById(R.id.btn_cerrar);
void setData(final DataRoot Rdata)
{
btn_cerrar.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
btn_cerrar.setEnabled(false);
btn_cerrar.setBackgroundTintList(mContext.getColorStateList(R.color.cboff));
updateRetrofitEstadoorden(Rdata.get_id());
updateRetrofitNrocomanda(Rdata.get_id(), txt_comanda.getText().toString());
delete(getAdapterPosition());
}
});
Rdata.gerOrder();
creaboton():
and here my recyler
private void setAdapter()
{
adapter = new ComandaAdapter(this, txt_entracola, txt_fondocola, txt_postrecola, txt_comandas, Ri);
recyclerView.getRecycledViewPool().setMaxRecycledViews(-1, Ri.size());//va en 0 supuestamente -1 es default
recyclerView.setItemViewCacheSize(Ri.size()); //ver si hay que cambiar con cada item
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(adapter);
}
Thanks in advance for any help.
Images to show the problem
You are using the Recyclerview in a very non standard way. The issue you are seeing is because the views are being recycled (as they should be in a Recyclerview) but you are not clearing out the items from the previous view.
The problem in in this method:
public void setData(String value) {
container.removeAllViews(); // Remove all previously added views
textview.setText(value);
Random r = new Random();
int i1 = r.nextInt(5 - 1) + 1;
for (int i = 0; i < i1; i++) {
be = new Button(mContext);
be.setText("Boton " + i);
be.setLayoutParams(new LinearLayout.LayoutParams(240, LinearLayout.LayoutParams.WRAP_CONTENT));
container.addView(be);
}
}
By calling container.addView(be), you are manually adding extra views to the views that the Recyclerview creates. When you remove these views, they are cached and reused the next time you press "Add". The problem is that the cached view still contains all of the manually added views so you are then adding more views under the existing ones.
As you can see in the code above, i added container.removeAllViews(); which removes the views that were added previously ensuring that "Container" is empty before you start adding your extra views to it.
Also, unless you have a very specific reason for doing so, I would removes these lines as I believe you are hurting performance by having them:
list.getRecycledViewPool().setMaxRecycledViews(-1, index);
list.setItemViewCacheSize(index);