请不要将此标记为重复。我有搜索&没有得到我的答案。请帮我。
我的问题是我使用了RecyclerView,我在其中添加联系人详细信息并显示。我将实际数据作为ArrayList传递给MyAdapter类。在那里,我创建了另一个ArrayList,并将原始列表的所有项目复制到该列表中,并执行所有必要的操作,如添加删除和移动等。这对我来说也很好。但是当我搜索列表中没有的内容时,我想显示一些文本消息,例如“找不到搜索记录”。我在onCreateViewHolder()方法中做了那件事。 如果找不到元素,那么getItemCount()也会返回原始内容。我怎么能实现这一点。请帮帮我。
这是我将实际数据传递给我的适配器类
的地方adapter = new MyAdapter(MainActivity.this, modelData);
mRecyclerView.setAdapter(adapter);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
然后使用onQueryTextChange()我在RecyclerView中搜索元素。
@Override
public boolean onQueryTextChange(String newText) {
List<MyModel> filteredModelList = filter(modelData, newText);
if(filteredModelList.size()>0) {
adapter.animateTo(filteredModelList);
mRecyclerView.scrollToPosition(0);
return true;
}else{
Toast.makeText(this,"EmptyList",Toast.LENGTH_SHORT).show();
return true;
}
}
private List<MyModel> filter(List<MyModel> models, String query) {
query = query.toLowerCase();
final List<MyModel> filteredModelList = new ArrayList<>();
for (MyModel model : models) {
final String text = model.getName().toLowerCase();
if (text.contains(query) || phone.contains(query)) {
filteredModelList.add(model);
} else {
}
}
return filteredModelList;
}
然后在我的适配器构造函数中创建另一个arraylist并将实际数据复制到此arraylist中。
public MyAdapter(Context context, List<MyModel> mModels) {
myModels = new ArrayList<>(mModels);
this.context = context;
}
我这样使用了getCount()。
@Override
public int getItemCount() {
Log.e("Size", "" + myModels.size());
if (myModels.size() > 0) {
Log.e("Size", "" + myModels.size());
return myModels.size();
}
return 1;
}
@Override
public int getItemViewType(int position) {
if (myModels.size() == 0) {
return VIEW_TYPE_EMPTY_LIST_PLACEHOLDER;
} else {
return VIEW_TYPE_OBJECT_VIEW;
}
}
我正在对arraylist的副本进行搜索操作。搜索很好。但是当搜索不匹配时,它应该显示一些单独的布局,说“找不到结果”(或一些错误)。我这样做了。
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v;
switch (viewType) {
case VIEW_TYPE_EMPTY_LIST_PLACEHOLDER:
// return view holder for your placeholder
v = LayoutInflater.from(parent.getContext()).inflate(R.layout.no_contacts, parent, false);
EmptyViewHolder evh = new EmptyViewHolder(v);
return evh;
case VIEW_TYPE_OBJECT_VIEW:
// return view holder for your normal list item
v = LayoutInflater.from(parent.getContext()).inflate(R.layout.contacts_row, parent, false);
ViewHolder vh = new ViewHolder(v);
return vh;
}
return null;
}
这是MyAdapter Class
public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private static final int VIEW_TYPE_EMPTY_LIST_PLACEHOLDER = 0;
private static final int VIEW_TYPE_OBJECT_VIEW = 1;
List<MyModel> myModels;
Context context;
public MyAdapter(Context context, List<MyModel> mModels) {
myModels = new ArrayList<>(mModels);
this.context = context;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v;
switch (viewType) {
case VIEW_TYPE_EMPTY_LIST_PLACEHOLDER:
// return view holder for your placeholder
v = LayoutInflater.from(parent.getContext()).inflate(R.layout.no_contacts, parent, false);
EmptyViewHolder evh = new EmptyViewHolder(v);
return evh;
case VIEW_TYPE_OBJECT_VIEW:
// return view holder for your normal list item
v = LayoutInflater.from(parent.getContext()).inflate(R.layout.contacts_row, parent, false);
ViewHolder vh = new ViewHolder(v);
return vh;
}
return null;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if (holder instanceof ViewHolder) {
ViewHolder vh = (ViewHolder) holder;
MyModel model = myModels.get(position);
vh.bind(model);
}
}
@Override
public int getItemCount() {
Log.e("Size", "" + myModels.size());
if (myModels.size() > 0) {
Log.e("Size", "" + myModels.size());
return myModels.size();
}
else if(removeModel.size()==0){
Log.e("Size remove", "" + removeModel.size());
Log.e("","");
return 1;
}
return 0;
}
@Override
public int getItemViewType(int position) {
if (myModels.size() == 0) {
return VIEW_TYPE_EMPTY_LIST_PLACEHOLDER;
} else {
return VIEW_TYPE_OBJECT_VIEW;
}
}
public void animateTo(List<MyModel> models) {
applyAndAnimateRemovals(models);
applyAndAnimateAdditions(models);
applyAndAnimateMovedItems(models);
}
private void applyAndAnimateRemovals(List<MyModel> newModels) {
if (newModels.size() > 0) {
for (int i = myModels.size() - 1; i >= 0; i--) {
final MyModel model = myModels.get(i);
if (!newModels.contains(model))
removeItem(i);
}
} else {
removeModel = new ArrayList<>(myModels);
Log.e("removeModel 1", "" + removeModel);
for (int i = removeModel.size() - 1; i >= 0; i--) {
removeAllItem(i);
}
Log.e("removeModel 2 ", "" + removeModel);
Log.e("myModels", "" + myModels);
}
}
private void applyAndAnimateAdditions(List<MyModel> newModels) {
for (int i = 0, count = newModels.size(); i < count; i++) {
final MyModel model = newModels.get(i);
if (!myModels.contains(model)) {
addItem(i, model);
}
}
}
private void applyAndAnimateMovedItems(List<MyModel> newModels) {
for (int toPosition = newModels.size() - 1; toPosition >= 0; toPosition--) {
final MyModel model = newModels.get(toPosition);
final int fromPosition = myModels.indexOf(model);
if (fromPosition >= 0 && fromPosition != toPosition) {
moveItem(fromPosition, toPosition);
}
}
}
public MyModel removeAllItem(int position) {
MyModel model = removeModel.remove(position);
notifyItemRemoved(position);
return model;
}
public MyModel removeItem(int position) {
final MyModel model = myModels.remove(position);
notifyItemRemoved(position);
return model;
}
public void addItem(int position, MyModel model) {
myModels.add(position, model);
notifyItemInserted(position);
}
public void moveItem(int fromPosition, int toPosition) {
final MyModel model = myModels.remove(fromPosition);
myModels.add(toPosition, model);
notifyItemMoved(fromPosition, toPosition);
}
public class EmptyViewHolder extends RecyclerView.ViewHolder {
TextView textView;
public EmptyViewHolder(View itemView) {
super(itemView);
textView = (TextView) itemView.findViewById(R.id.no_contact);
textView.setText("No Contacts to Display");
}
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView tvName;
TextView tvPhoneNumber;
ImageView ivImage;
public ViewHolder(View itemView) {
super(itemView);
tvName = (TextView) itemView.findViewById(R.id.tv_name);
tvPhoneNumber = (TextView) itemView.findViewById(R.id.phone_number);
ivImage = (ImageView) itemView.findViewById(R.id.ivPic);
}
public void bind(MyModel model) {
Log.e("Name ", model.getName());
tvName.setText(model.getName());
tvPhoneNumber.setText(model.getPhoneNumber());
Log.e("Image getImagePath()", model.getImagePath());
Log.e("Image URI()", "" + Uri.parse(model.getImagePath()));
if (model.getImagePath().startsWith("/")) {
ivImage.setImageURI(Uri.parse(model.getImagePath()));
} else {
ivImage.setImageResource(R.drawable.default_contact_pic);
}
}
}