Reputation: 151
I am making an app using firebase. Here I am connecting recyclerview with firebase real-time database. But the recyclerview is repeating, don't know why. Here on clicking on download another same recycler view element is created. Pls, help to solve this problem. Thanks in Advance. :)
Adapter code:-
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.card_view, parent, false);
ViewHolder viewHolder = new ViewHolder(v);
return viewHolder;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
elements elements = elementses.get(position);
holder.t1.setText(elements.getTitle());
final String s1 = elements.getPdf();
holder.b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
CustomTabsIntent customTabsIntent = builder.build();
customTabsIntent.launchUrl(context, Uri.parse(s1));
}
});
}
@Override
public int getItemCount() {
return elementses.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
public TextView t1;
public Button b1;
public ViewHolder(View itemView) {
super(itemView);
t1 = (TextView)itemView.findViewById(R.id.title);
b1 = (Button)itemView.findViewById(R.id.download_btn);
}
}
@Override
public int getItemViewType(int position) {
return position;
}
Main Activity Recycler view:-
recyclerView = (RecyclerView) findViewById(R.id.recycler_main);
recyclerView.hasFixedSize();
recyclerView.setLayoutManager(new
LinearLayoutManager(getApplicationContext()));
elementsList = new ArrayList<>();
mDatabase = FirebaseDatabase.getInstance().getReference();
main_adapter = new Main_Adapter(getApplicationContext(),elementsList);
recyclerView.setLayoutManager(new
LinearLayoutManager(getApplicationContext(),
LinearLayoutManager.VERTICAL, false));
recyclerView.setAdapter(main_adapter);
main_adapter.notifyDataSetChanged();
recyclerView.setNestedScrollingEnabled(false);
recyclerView.setHasFixedSize(true);
Upvotes: 0
Views: 3859
Reputation: 999
// try to add this hope it will work
recyclerView.setHasFixedSize(true);
Upvotes: 0
Reputation: 795
Wherever you are loading data, it has been loading multiple times creating duplicate entries in elementList
(data source of your recycler view).
Make sure to check if the list is empty or for duplicate entries before filling the data.
Upvotes: 0
Reputation: 18276
Debug your application and check if the element list is getting duplicated or you are reusing the adapter data that contains the previous set (you may clear it before adding again)
Also, please post where you refresh the content.
You may only change the RecyclerAdapter instead of creating a new RecyclerView
Upvotes: 0
Reputation: 1217
First of, your adapter code is weird. Especially
@Override
public int getItemViewType(int position) {
return position;
}
Basically, what you are doing is telling the adapter that for each index there is a new item type. This is wrong. Best practice is to have an enum
with all possible (even if there is only one) cell types.
As for the rest of the code, it looks ok, however I would have a look at online-available tutorials how to make a decent adapter
Upvotes: 1