Reputation: 438
Upon requesting some new data, Recycler View adapter is updating data at the end of previous data where as I only wants to show the latest/fresh requested data on screen and delete the previous data. Below is the adapter code, other code can be shown if needed.
private LayoutInflater inflater;
Context context;
List<Data> dataArray, dataArray1, dataArray2;
public RecyclerViewAdapter(Context context, List<Data> dataArray) {
swap(dataArray1);
this.dataArray1 = dataArray;
this.context = context;
inflater = LayoutInflater.from(context);
}
private void swap(List<Data> dataArray1){
if (this.dataArray1 != null) {
this.dataArray.clear();
System.out.println(dataArray1);
}
notifyDataSetChanged();
}
@Override
public RecyclerViewAdapter.CustomViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview, null);
CustomViewHolder holder = new CustomViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(CustomViewHolder holder, int position) {
Data current = dataArray1.get(position);
//holder.image.setImageResource(current.Limage);
holder.textView1.setText(current.heading);
holder.textView2.setText(current.date);
}
@Override
public int getItemCount() {
return dataArray1.size();
}
public static class CustomViewHolder extends RecyclerView.ViewHolder {
//ImageView image;
TextView textView1, textView2;
public CustomViewHolder(View itemView) {
super(itemView);
// image = (ImageView)itemView.findViewById(R.id.Limage);
textView1 = (TextView) itemView.findViewById(R.id.heading);
textView2 = (TextView) itemView.findViewById(R.id.date);
}
}
}
Activity code is :
public RecyclerViewAdapter recyclerViewAdapter;
Toolbar toolbar;
public RecyclerView recyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_news);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
String url = "*****";
new JSONAsync(getApplicationContext()).execute(url);
recyclerView = (RecyclerView)findViewById(R.id.recyclerView);
recyclerViewAdapter = new RecyclerViewAdapter(MainNews.this, JSONAsync.dataArray);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(recyclerViewAdapter);
recyclerView.setItemAnimator(new DefaultItemAnimator());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
return super.onOptionsItemSelected(item);
}
}
Any help would be highly appreciated.
Upvotes: 0
Views: 1524
Reputation: 4069
Use this code inside your adapter-
private LayoutInflater inflater;
Context context;
List<Data> dataArray;
public RecyclerViewAdapter(Context context, List<Data> dataArray) {
this.dataArray = dataArray;
this.context = context;
inflater = LayoutInflater.from(context);
}
@Override
public RecyclerViewAdapter.CustomViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview, null);
CustomViewHolder holder = new CustomViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(CustomViewHolder holder, int position) {
Data current = dataArray.get(position);
//holder.image.setImageResource(current.Limage);
holder.textView1.setText(current.heading);
holder.textView2.setText(current.date);
}
@Override
public int getItemCount() {
return dataArray.size();
}
public static class CustomViewHolder extends RecyclerView.ViewHolder {
//ImageView image;
TextView textView1, textView2;
public CustomViewHolder(View itemView) {
super(itemView);
// image = (ImageView)itemView.findViewById(R.id.Limage);
textView1 = (TextView) itemView.findViewById(R.id.heading);
textView2 = (TextView) itemView.findViewById(R.id.date);
}
}
And clear the List everytime before you add the new element on it like-
In Activity or Fragment
List<Data> dataArray = new List();
// your other code here..
private void yourMethodName() {
dataArray.clear();
try{
// your code to get data
// new add your new data to list
dataArray.add(yourData);
}catch (Exception ex){
ex.getMessage();
}
// Notify your adapter here
adapter.notifyDataSetChanged();
}
Hope it will work.
Upvotes: 0
Reputation: 3520
I think you are adding all data to list once you get new data. Instead of that clear the arraylist and add the new data to it and call notify datasetChanged. That should solve your problem. If it doesnt please show your Activity or fragment that contains the recycler view.
Upvotes: 2
Reputation: 661
Because your CustomViewHolder is a static class.
Change it to
public class CustomViewHolder extends RecyclerView.ViewHolder
Upvotes: 0
Reputation: 6791
Don't use constructor to update RecyclerView
. Constructor should be like this.
private LayoutInflater inflater;
Context context;
List<Data> dataArray;
public RecyclerViewAdapter(Context context, List<Data> dataArray) {
this.dataArray = dataArray;
this.context = context;
inflater = LayoutInflater.from(context)
}
Setting adapter to RecyclerView
:
List<Data> dataArray;
dataArray = new ArrayList<>();
adapter = new RecyclerViewAdapter(context, dataArray);
recylerView.setAdapter(adapter);
to add and update data:
dataArray.clear();
dataArray.add(data);
adapter. notifyDataSetChanged();
Upvotes: 0