Recyclerview在以前的数据末尾更新数据

时间:2016-03-22 12:29:55

标签: android android-recyclerview

在请求一些新数据时,Recycler View适配器正在更新先前数据末尾的数据,因为我只想在屏幕上显示最新/新请求的数据并删除以前的数据。 下面是适配器代码,如果需要,可以显示其他代码。

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

    }
}

}

活动代码是:

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

}

任何帮助都将受到高度赞赏。

4 个答案:

答案 0 :(得分:1)

我认为您在获取新数据后会将所有数据添加到列表中。而不是清除arraylist并将新数据添加到它并调用notify datasetChanged。那应该可以解决你的问题。如果没有,请显示包含回收站视图的活动或片段。

答案 1 :(得分:0)

不要使用构造函数来更新RecyclerView。构造函数应该是这样的。

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

将适配器设置为RecyclerView

List<Data> dataArray;

dataArray =  new ArrayList<>();
adapter = new RecyclerViewAdapter(context, dataArray);

recylerView.setAdapter(adapter);

添加和更新数据:

dataArray.clear();
dataArray.add(data);
adapter. notifyDataSetChanged();

答案 2 :(得分:0)

因为您的CustomViewHolder是一个静态类。

将其更改为

public class CustomViewHolder extends RecyclerView.ViewHolder

答案 3 :(得分:0)

在适配器中使用此代码 -

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

    }
}

清除 列表每次在您添加新元素之前 -

活动片段

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

希望它能奏效。