我已经实现了两个RecyclerView适配器,一个是RecyclerViewAdapter,另一个是EmptyRecyclerViewAdapter。我要做的是在删除所有项目时显示文本select * from tbl_fruit_data f
left join tbl_fruit_info i ON i.sId = f.fruit_id
where f.Date =
(Select max(date) from tbl_fruit_data
where fruit_id = f.fruit_id)
。
Empty Text Here!
正在运行,如果列表中还没有项目,它会显示Image 2
下面显示的文字Empty Text Here!
但是,如果列表中已有项目如果我将删除列表中的所有项目,Image 2
将是没有显示任何内容的结果。
EmptyRecyclerViewAdapter.class
Image 1
RecyclerViewAdapter.class
public class EmptyRecyclerViewAdapter extends RecyclerView.Adapter<EmptyRecyclerViewAdapter.ViewHolder> {
private String mMessage;
public EmptyRecyclerViewAdapter(){}
public EmptyRecyclerViewAdapter(String message){ mMessage = message;}
@Override
public EmptyRecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.empty_item, parent, false);
ViewHolder viewHolder = new ViewHolder(view);
if(mMessage != null){
viewHolder.mMessageView.setText(mMessage);
}
return viewHolder;
}
@Override
public void onBindViewHolder(EmptyRecyclerViewAdapter.ViewHolder holder, int position) {}
@Override
public int getItemCount() { return 1; }
public class ViewHolder extends RecyclerView.ViewHolder {
final View mView;
final TextView mMessageView;
public ViewHolder(View view) {
super(view);
mView = view;
mMessageView = (TextView) view.findViewById(R.id.empty_item_message);
}
}
}
ActivityA.class
public class RecyclerViewAdapter extends RecyclerView.Adapter <RecyclerView.ViewHolder> {
private List<ItemModel> list;
private Context mContext = null;
public RecyclerViewAdapter (Context context, List<ItemModel> itemList) {
mContext = context;
this.list = itemList;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent, false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
final ItemModel l = list.get(holder.getAdapterPosition());
final DataDb dataDb = new DataDb (mContext);
((MyViewHolder) holder).title.setText(l.getTitle());
((MyViewHolder) holder).caption.setText(l.getCaption());
((MyViewHolder) holder).removeBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String confirmText = "Are you sure you want to remove this item ?";
AlertDialog.Builder builder = new AlertDialog.Builder(mContext, R.style.AlertDialogStyle);
builder.setTitle("Deleting..");;
builder.setPositiveButton("Remove", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
list.remove(holder.getAdapterPosition());
dataDb.removeItem(l.getId());
notifyDataSetChanged();
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
AlertDialog alert = builder.create();
alert.getWindow().setLayout(600, 400);
alert.show();
}
});
}
@Override
public int getItemCount() {
return (list != null? list.size():0);
}
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView title;
public TextView caption;
ImageButton removeBtn;
public MyViewHolder(View itemView) {
super(itemView);
title = (TextView) itemView.findViewById(R.id.title);
caption = (TextView) itemView.findViewById(R.id.caption);
removeBtn = (ImageButton) itemView.findViewById(R.id.btnRemoveItem);
}
}
}
图像1 - 输出结果
图片2 - 输出目标
答案 0 :(得分:2)
您必须让您的活动知道适配器中设置的列表已更改且项目计数降至零。当列表大小变空时,您需要从Activity中切换适配器。
您可以通过界面实现此目的。基本上,首先声明一个接口并在Adapter中保存它的实例。无论何时调用notifyDataSetChanged,都应该调用这个Interface实例。这将触发Activity中实现的Interface方法。您可以从该点切换适配器。
修改您的类,如下所示。
RecyclerViewAdapter.class
public class RecyclerViewAdapter extends RecyclerView.Adapter <RecyclerView.ViewHolder> {
//Declare an Interface which will work as a listener of notifyDataSetChanged.
// We'll then switch the adapter of Activity accordingly
public interface OnNotifyDataSetChanged {
void OnNotifyDataSetChangedFired(int dataSize);
}
private OnNotifyDataSetChanged onNotifyDataSetChanged;
private List<ItemModel> list;
private Context mContext = null;
public RecyclerViewAdapter (Context context, List<ItemModel> itemList, OnNotifyDataSetChanged onNotifyDataSetChanged) {
mContext = context;
this.list = itemList;
this.onNotifyDataSetChanged = onNotifyDataSetChanged;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent, false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
final ItemModel l = list.get(holder.getAdapterPosition());
final DataDb dataDb = new DataDb (mContext);
((MyViewHolder) holder).title.setText(l.getTitle());
((MyViewHolder) holder).caption.setText(l.getCaption());
((MyViewHolder) holder).removeBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String confirmText = "Are you sure you want to remove this item ?";
AlertDialog.Builder builder = new AlertDialog.Builder(mContext, R.style.AlertDialogStyle);
builder.setTitle("Deleting..");;
builder.setPositiveButton("Remove", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
list.remove(holder.getAdapterPosition());
dataDb.removeItem(l.getId());
notifyDataSetChanged();
// Trigger our Interface instance to let the Activity know that the adapter is updated
if(onNotifyDataSetChanged != null){
onNotifyDataSetChanged.OnNotifyDataSetChangedFired(list.size());
}
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
AlertDialog alert = builder.create();
alert.getWindow().setLayout(600, 400);
alert.show();
}
});
}
@Override
public int getItemCount() {
return (list != null? list.size():0);
}
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView title;
public TextView caption;
ImageButton removeBtn;
public MyViewHolder(View itemView) {
super(itemView);
title = (TextView) itemView.findViewById(R.id.title);
caption = (TextView) itemView.findViewById(R.id.caption);
removeBtn = (ImageButton) itemView.findViewById(R.id.btnRemoveItem);
}
}
}
ActivityA.class
public class ActivityA extends Fragment implements OnNotifyDataSetChanged {
List<ItemModel> list;
RecyclerView mRecyclerView;
RecyclerViewAdapter adapter;
ProgressBar progressBar;
DataDb dataDb;
EmptyRecyclerViewAdapter emptyRecyclerViewAdapter;
public ActivityA() {}
@Override
void OnNotifyDataSetChangedFired(int dataSize){
//Now, switch the adapter if needed
if(dataSize <= 0){
mRecyclerView.setAdapter(new EmptyRecyclerViewAdapter("Empty Text Here!"));
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View rootView = inflater.inflate(activity_one_layout, container, false);
progressBar = (ProgressBar) rootView.findViewById(R.id.progressBar);
mRecyclerView = (RecyclerView) rootView.findViewById(R.id.recyclerview);
dataDb = new DataDb(getActivity());
list= dataDb .getList();
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
mRecyclerView.setLayoutManager(linearLayoutManager);
mRecyclerView.setHasFixedSize(true);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
if(list == null || list.size() == 0) {
progressBar.setVisibility(View.GONE);
emptyRecyclerViewAdapter = new EmptyRecyclerViewAdapter("Empty Text Here!");
mRecyclerView.setAdapter(emptyRecyclerViewAdapter);
} else {
new LoadList().execute();
}
return rootView;
}
@Override
public void onResume() {
super.onResume();
}
private class LoadList extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
progressBar.setVisibility(View.VISIBLE);
}
@Override
protected Void doInBackground(Void... voids) {
try {
Thread.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void params){
progressBar.setVisibility(View.GONE);
if(!list.isEmpty()) {
// New updated constructor of the Adapter
adapter = new RecyclerViewAdapter(getActivity(), list, ActivityA.this);
mRecyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
} else {
Toast.makeText(getActivity(), "", Toast.LENGTH_SHORT).show();
}
}
}
}