如何使此适配器可搜索?
How to make this Adapter searchable?
这是我的列表视图的适配器..
我想向其中添加过滤器或搜索功能。请帮我。
但是我看过很多代码但是 none 帮助我实现了它。而且我还没有看到扩展 ArrayAdapter 的适配器,其中有一个 getFilter。所以请帮助我。
public class FullNameAdapter extends ArrayAdapter<Name> implements Filterable {
public FullNameAdapter(Context context, ArrayList<Name> words){
super(context, 0, words);
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View listItemView = convertView;
if(listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(R.layout.main_listview, parent, false);
}
Name currentWord = getItem(position);
TextView mainNumber = (TextView) listItemView.findViewById(R.id.mainNumber_page);
mainNumber.setText(currentWord.getMainNumber());
TextView sanskritName = (TextView) listItemView.findViewById(R.id.sanskrit_name_text_view_ml);
sanskritName.setText(currentWord.getSanskritName());
TextView scientificName = (TextView) listItemView.findViewById(R.id.scientific_name_text_view_ml);
scientificName.setText(currentWord.getScientificName());
TextView familyName = (TextView) listItemView.findViewById(R.id.family_text_view_ml);
familyName.setText(currentWord.getFamilyName());
ShapeableImageView panel_IMG_back = (ShapeableImageView) listItemView.findViewById(R.id.mainList_imageView);
Glide.with(getContext()).load(currentWord.getPicId()).into(panel_IMG_back);
panel_IMG_back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Dialog builder = new Dialog(getContext());
builder.requestWindowFeature(Window.FEATURE_NO_TITLE);
builder.getWindow().setBackgroundDrawable(
new ColorDrawable(android.graphics.Color.TRANSPARENT));
builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialogInterface) {
//nothing;
}
});
ZoomageView imageView = new ZoomageView(getContext());
imageView.setImageResource(currentWord.getPicId());
builder.addContentView(imageView, new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
builder.setCanceledOnTouchOutside(true);
builder.show();
}
});return listItemView;}}
请帮忙!
在MAINACTIVITY中,Action Bar SEARCH VIEW在Mainactivity中,应该怎么办?这里
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.list_menu, menu);
MenuItem menuItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) menuItem.getActionView();
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
return true;
}
});
return super.onCreateOptionsMenu(menu);
}
这些是我添加到我的数组列表中的对象,我想搜索其中的一些对象,如果数组中的字符串匹配,则数组列表可以显示在列表中。但如果名称没有匹配的字符串,它应该去!
private void smallaaray() {
array = new ArrayList<Name>();
array.add(new Name("1", "akhil","", "", R.drawable.0,
R.string.0, "",
"","","","","","","","","","","","","",R.drawable.0,R.drawable.0,R.drawable.0,R.drawable.0));
请帮忙!
适配器只关心数据和视图。过滤和搜索应该是对数据源的操作。因此,您应该在对数据源进行操作后更新适配器
您可以像下面这样创建自定义过滤器,
将您的列表传递给 searching/filtering
适配器的 setFilterList() 方法
触发搜索:
adapter.getFilter().filter();
public class FullNameAdapter extends ArrayAdapter<Name> implements Filterable {
public FullNameAdapter(Context context, ArrayList<Name> words) {
super(context, 0, words);
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View listItemView = convertView;
if (listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(R.layout.item_bonus, parent, false);
}
Name currentWord = getItem(position);
return listItemView;
}
CustomFilter filter;
public void setFilterList(List<Name> filterList) {
this.filterList = filterList;
}
List<Name> filterList;
@NonNull
@Override
public CustomFilter getFilter() {
return filter;
}
public class CustomFilter extends Filter {
ArrayList<Name> filterList;
public CustomFilter(ArrayList<Name> filterList) {
this.filterList = filterList;
}
//FILTERING OCCURS
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
//CHECK CONSTRAINT VALIDITY
if (constraint != null && constraint.length() > 0) {
//CHANGE TO UPPER
constraint = constraint.toString().toUpperCase();
//STORE OUR FILTERED PLAYERS
ArrayList<Name> filteredPlayers = new ArrayList<>();
if (filterList != null) {
for (int i = 0; i < filterList.size(); i++) {
//CHECK
try {
if (filterList.get(i).getNameName().toUpperCase().contains(constraint)) {
//ADD PLAYER TO FILTERED PLAYERS
filteredPlayers.add(filterList.get(i));
}
} catch (Throwable e) {
e.printStackTrace();
}
}
}
results.count = filteredPlayers.size();
results.values = filteredPlayers;
} else {
results.count = filterList.size();
results.values = filterList;
}
return results;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
clear();
if (results != null && results.count > 0) {
add((Name) results.values);
notifyDataSetChanged();
}
}
}
}
找到一个简单而复杂的解决方案。
SearchView searchView = (SearchView) menuItem.getActionView();
searchView.setQueryHint("Search All Data!");
searchView.setOnQueryTextListener (new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String q) {
return false;
}
@Override
public boolean onQueryTextChange(String s) {
if (BUYED_APP == false){
Toast.makeText(NonDetailDrugActivity.this, "Your are Searching on a Short list of 10 Drugs Only.\nPlease Subscribe to Gain Access to Full Data!", Toast.LENGTH_LONG).show();
}
filter = new ArrayList<>();
filter.clear();
if (s.length() == 0) {
filter.addAll(array);
} else {
for (Name post : array) {
String filterPattern = s.toLowerCase().trim();
if (post.getSanskritName().toLowerCase().contains(filterPattern)) {
filter.add(post);
} else {
filter.retainAll(array);
if (post.getScientificName().toLowerCase().contains(filterPattern)) {
filter.add(post);
} else {
filter.retainAll(array);
if (post.getFamilyName().toLowerCase().contains(filterPattern)) {
filter.add(post);
} else {
filter.retainAll(array);
if (post.getOtherName().toLowerCase().contains(filterPattern)) {
filter.add(post);
}else {
filter.retainAll(array);
if (post.getAmayikaPrayogam().toLowerCase().contains(filterPattern)) {
filter.add(post);
}else {
filter.retainAll(array);
if (post.getYogas().toLowerCase().contains(filterPattern)) {
filter.add(post);
}else {
filter.retainAll(array);
if (post.getKarma().toLowerCase().contains(filterPattern)) {
filter.add(post);
} else {
filter.retainAll(array);
if (post.getMalayalamNameId().toLowerCase().contains(filterPattern)) {
filter.add(post);
}
}
}
}
}
}
}
}
}
}
adap.clear();
adap.addAll(filter);
adap.notifyDataSetChanged();
return true;
}
});
searchView.setOnCloseListener(new SearchView.OnCloseListener() {
@Override
public boolean onClose() {
clearSearch();
searchView.onActionViewCollapsed();
return true;
}
});
&
private void clearSearch(){
adap.clear();
filter.clear();
smallaaray();
adap.addAll(array);
adap.notifyDataSetChanged(); }
这样!我让我的搜索成为可能。!非常感谢大家的帮助。
这是我的列表视图的适配器.. 我想向其中添加过滤器或搜索功能。请帮我。 但是我看过很多代码但是 none 帮助我实现了它。而且我还没有看到扩展 ArrayAdapter 的适配器,其中有一个 getFilter。所以请帮助我。
public class FullNameAdapter extends ArrayAdapter<Name> implements Filterable {
public FullNameAdapter(Context context, ArrayList<Name> words){
super(context, 0, words);
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View listItemView = convertView;
if(listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(R.layout.main_listview, parent, false);
}
Name currentWord = getItem(position);
TextView mainNumber = (TextView) listItemView.findViewById(R.id.mainNumber_page);
mainNumber.setText(currentWord.getMainNumber());
TextView sanskritName = (TextView) listItemView.findViewById(R.id.sanskrit_name_text_view_ml);
sanskritName.setText(currentWord.getSanskritName());
TextView scientificName = (TextView) listItemView.findViewById(R.id.scientific_name_text_view_ml);
scientificName.setText(currentWord.getScientificName());
TextView familyName = (TextView) listItemView.findViewById(R.id.family_text_view_ml);
familyName.setText(currentWord.getFamilyName());
ShapeableImageView panel_IMG_back = (ShapeableImageView) listItemView.findViewById(R.id.mainList_imageView);
Glide.with(getContext()).load(currentWord.getPicId()).into(panel_IMG_back);
panel_IMG_back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Dialog builder = new Dialog(getContext());
builder.requestWindowFeature(Window.FEATURE_NO_TITLE);
builder.getWindow().setBackgroundDrawable(
new ColorDrawable(android.graphics.Color.TRANSPARENT));
builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialogInterface) {
//nothing;
}
});
ZoomageView imageView = new ZoomageView(getContext());
imageView.setImageResource(currentWord.getPicId());
builder.addContentView(imageView, new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
builder.setCanceledOnTouchOutside(true);
builder.show();
}
});return listItemView;}}
请帮忙!
在MAINACTIVITY中,Action Bar SEARCH VIEW在Mainactivity中,应该怎么办?这里
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.list_menu, menu);
MenuItem menuItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) menuItem.getActionView();
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
return true;
}
});
return super.onCreateOptionsMenu(menu);
}
这些是我添加到我的数组列表中的对象,我想搜索其中的一些对象,如果数组中的字符串匹配,则数组列表可以显示在列表中。但如果名称没有匹配的字符串,它应该去!
private void smallaaray() {
array = new ArrayList<Name>();
array.add(new Name("1", "akhil","", "", R.drawable.0,
R.string.0, "",
"","","","","","","","","","","","","",R.drawable.0,R.drawable.0,R.drawable.0,R.drawable.0));
请帮忙!
适配器只关心数据和视图。过滤和搜索应该是对数据源的操作。因此,您应该在对数据源进行操作后更新适配器
您可以像下面这样创建自定义过滤器, 将您的列表传递给 searching/filtering
适配器的 setFilterList() 方法触发搜索:
adapter.getFilter().filter(
public class FullNameAdapter extends ArrayAdapter<Name> implements Filterable {
public FullNameAdapter(Context context, ArrayList<Name> words) {
super(context, 0, words);
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View listItemView = convertView;
if (listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(R.layout.item_bonus, parent, false);
}
Name currentWord = getItem(position);
return listItemView;
}
CustomFilter filter;
public void setFilterList(List<Name> filterList) {
this.filterList = filterList;
}
List<Name> filterList;
@NonNull
@Override
public CustomFilter getFilter() {
return filter;
}
public class CustomFilter extends Filter {
ArrayList<Name> filterList;
public CustomFilter(ArrayList<Name> filterList) {
this.filterList = filterList;
}
//FILTERING OCCURS
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
//CHECK CONSTRAINT VALIDITY
if (constraint != null && constraint.length() > 0) {
//CHANGE TO UPPER
constraint = constraint.toString().toUpperCase();
//STORE OUR FILTERED PLAYERS
ArrayList<Name> filteredPlayers = new ArrayList<>();
if (filterList != null) {
for (int i = 0; i < filterList.size(); i++) {
//CHECK
try {
if (filterList.get(i).getNameName().toUpperCase().contains(constraint)) {
//ADD PLAYER TO FILTERED PLAYERS
filteredPlayers.add(filterList.get(i));
}
} catch (Throwable e) {
e.printStackTrace();
}
}
}
results.count = filteredPlayers.size();
results.values = filteredPlayers;
} else {
results.count = filterList.size();
results.values = filterList;
}
return results;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
clear();
if (results != null && results.count > 0) {
add((Name) results.values);
notifyDataSetChanged();
}
}
}
}
找到一个简单而复杂的解决方案。
SearchView searchView = (SearchView) menuItem.getActionView();
searchView.setQueryHint("Search All Data!");
searchView.setOnQueryTextListener (new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String q) {
return false;
}
@Override
public boolean onQueryTextChange(String s) {
if (BUYED_APP == false){
Toast.makeText(NonDetailDrugActivity.this, "Your are Searching on a Short list of 10 Drugs Only.\nPlease Subscribe to Gain Access to Full Data!", Toast.LENGTH_LONG).show();
}
filter = new ArrayList<>();
filter.clear();
if (s.length() == 0) {
filter.addAll(array);
} else {
for (Name post : array) {
String filterPattern = s.toLowerCase().trim();
if (post.getSanskritName().toLowerCase().contains(filterPattern)) {
filter.add(post);
} else {
filter.retainAll(array);
if (post.getScientificName().toLowerCase().contains(filterPattern)) {
filter.add(post);
} else {
filter.retainAll(array);
if (post.getFamilyName().toLowerCase().contains(filterPattern)) {
filter.add(post);
} else {
filter.retainAll(array);
if (post.getOtherName().toLowerCase().contains(filterPattern)) {
filter.add(post);
}else {
filter.retainAll(array);
if (post.getAmayikaPrayogam().toLowerCase().contains(filterPattern)) {
filter.add(post);
}else {
filter.retainAll(array);
if (post.getYogas().toLowerCase().contains(filterPattern)) {
filter.add(post);
}else {
filter.retainAll(array);
if (post.getKarma().toLowerCase().contains(filterPattern)) {
filter.add(post);
} else {
filter.retainAll(array);
if (post.getMalayalamNameId().toLowerCase().contains(filterPattern)) {
filter.add(post);
}
}
}
}
}
}
}
}
}
}
adap.clear();
adap.addAll(filter);
adap.notifyDataSetChanged();
return true;
}
});
searchView.setOnCloseListener(new SearchView.OnCloseListener() {
@Override
public boolean onClose() {
clearSearch();
searchView.onActionViewCollapsed();
return true;
}
});
&
private void clearSearch(){
adap.clear();
filter.clear();
smallaaray();
adap.addAll(array);
adap.notifyDataSetChanged(); }
这样!我让我的搜索成为可能。!非常感谢大家的帮助。