前提:我是 Java 和 Android 的新手,我对此进行了很多搜索,但我不明白如何在我的代码中实现 getFilter。
这是MainActivity(相关代码):
public void loadList() {
/* allProd and allSpec are ArrayList<String> */
String[] allProdString = allProd.toArray(new String[allProd.size()]);
String[] allSpecString = allSpec.toArray(new String[allSpec.size()]);
listViewAdapter = new ListViewAdapter(this, allProdString, allSpecString);
listView.setAdapter(listViewAdapter);
}
这是自定义适配器:
public class ListViewAdapter extends BaseAdapter {
Activity context;
String title[];
String description[];
public ListViewAdapter(Activity context, String[] title, String[] description) {
super();
this.context = context;
this.title = title;
this.description = description;
}
public int getCount() {
return title.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
private class ViewHolder {
TextView txtViewTitle;
TextView txtViewDescription;
}
public View getView(int position, View convertView, ViewGroup parent) {
Typeface typeface = Typeface.createFromAsset(context.getAssets(), "font/Simple_Print.ttf");
ViewHolder holder;
LayoutInflater inflater = context.getLayoutInflater();
if (convertView == null) {
convertView = inflater.inflate(R.layout.tabella_prodotti, null);
holder = new ViewHolder();
holder.txtViewTitle = (TextView) convertView.findViewById(R.id.titoloProd);
holder.txtViewDescription = (TextView) convertView.findViewById(R.id.subtitoloProd);
holder.txtViewDescription.setTypeface(typeface);
holder.txtViewTitle.setTypeface(typeface);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
holder.txtViewTitle.setText(title [position]);
holder.txtViewDescription.setText(description [position]);
return convertView;
}
}
如何实现此函数以能够在数组中搜索,并在 ListView 中显示过滤结果?
如果您需要任何其他信息,请询问!谢谢
最佳答案
我认为你的适配器应该是这样的。另外我不建议在 getView() 中实例化对象,即字体对象
public class ListViewAdapter extends BaseAdapter {
String title[];
String description[];
ArrayList<String> filteredTitleList;
ArrayList<String> filteredDescripionList;
Typeface typeface;
LayoutInflater inflater;
public ListViewAdapter(Activity context, String[] title, String[] description) {
super();
this.context = context;
this.title = title;
this.description = description;
typeface = Typeface.createFromAsset(context.getAssets(),"font/Simple_Print.ttf");
inflater = context.getLayoutInflater();
this.filteredTitleList = new ArrayList<String>();
this.filteredDescripionList = new ArrayList<String>();
applyFilter(null);
}
public void applyFilter(String filter){
filteredTitleList.clear();
filteredDescripionList.clear();
for(int i=0; i < this.title.length; i++){
String tempTitle = title[i];
String tempDesc = description[i];
if(filter == null || filter.equals("")){
this.filteredTitleList.add(tempTitle);
this.filteredDescripionList.add(tempDesc);
}
else if(tempTitle.toLowerCase().contains(filter.toLowerCase()) || tempDesc.toLowerCase().contains(filter.toLowerCase())){
this.filteredTitleList.add(tempTitle);
this.filteredDescripionList.add(tempDesc);
}
}
this.notifyDataSetChanged();
}
public int getCount() {
return filteredTitleList.size();
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return position;
}
private class ViewHolder {
TextView txtViewTitle;
TextView txtViewDescription;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.tabella_prodotti, null);
holder = new ViewHolder();
holder.txtViewTitle = (TextView) convertView.findViewById(R.id.titoloProd);
holder.txtViewDescription = (TextView) convertView.findViewById(R.id.subtitoloProd);
holder.txtViewDescription.setTypeface(typeface);
holder.txtViewTitle.setTypeface(typeface);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
holder.txtViewTitle.setText(this.filteredTitleList.get(position));
holder.txtViewDescription.setText(this.filteredDescriptionList.get(position));
return convertView;
}
}
关于java - 在扩展 BaseAdapter 的 customAdapter 中实现 getFilter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21164147/