在我的片段中,当我运行app时,RecyclerView已经显示了一些数据。实际上我想要在某些类别基础上刷新数据时,如果特定类别中不存在数据,我会得到空数组。现在我想将null设置为RecyclerView适配器,这样它就会因为null数组而刷新并显示任何内容。
这是片段中的刷新代码函数,它无法正常工作:
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Log.d(TAG, "run: fragment 2"+searchTxt);
if(dataStoredArrayList!=null) {
dataStoredArrayList.clear();
myRecyclerAdapter.notifyDataSetChanged();
}
mparser = new SecondParseDataClass(context);
mparser.setOnDataRetrievalCallback(new OnDataRetrievalCallback() {
@Override
public void onDataRetrieval(ArrayList<DataStored> dataSet) {
if(dataSet!=null) {
dataStoredArrayList.addAll(dataSet);
myRecyclerAdapter.notifyDataSetChanged();
}
else{
dataStoredArrayList=null;
myRecyclerAdapter.notifyDataSetChanged();
}
}
});
mparser.execute("http://192.168.3.10/testing.php", "2",searchTxt);
}
}, 0);
答案 0 :(得分:1)
使用dataStoredArrayList.clear()
代替dataStoredArrayList=null
。
答案 1 :(得分:0)
我使用这个完美运行的适配器类
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.ArrayList;
import in.kpis.structure.R;
/**
* Created by user on 6/21/2016.
*/
public class AdapterDemo extends BaseRecyclerViewAdapter<AdapterDemo.MyViewHolder> {
private ArrayList<String> list = new ArrayList<>();
private Context context;
class MyViewHolder extends RecyclerView.ViewHolder {
// private ImageView ivHorizontalImage;
MyViewHolder(View view) {
super(view);
// ivHorizontalImage = (ImageView) view.findViewById(R.id.ivHorizontalImage);
}
}
public void insertItemInList(String beanChat) {
if (list == null) list = new ArrayList<>();
list.add(beanChat);
notifyItemInserted(list.size() - 1);
}
public void insertItemsInList(ArrayList<String> myList) {
for (String beanChat : myList) {
insertItemInList(beanChat);
}
}
public ArrayList<String> getList() {
if (list == null) list = new ArrayList<>();
return list;
}
public void clearList() {
if (list == null) return;
list.clear();
notifyDataSetChanged();
}
public AdapterDemo(Context context) {
this.context = context;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.layout_nothing_to_display, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
}
@Override
public int getItemCount() {
return list.size();
}
}
,您的代码将缩减为
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Log.d(TAG, "run: fragment 2"+searchTxt);
adapterDemo.clearList();
mparser = new SecondParseDataClass(context);
mparser.setOnDataRetrievalCallback(new OnDataRetrievalCallback() {
@Override
public void onDataRetrieval(ArrayList<DataStored> dataSet) {
if(dataSet!=null) {
adapterDemo.insertItemsInList(dataSet);
}
else{
adapterDemo.clearList();
}
}
});
mparser.execute("http://192.168.3.10/testing.php", "2",searchTxt);
}
}, 0);
我逐个插入项目,因为它使用recycler view
提供默认动画效果。