在我的应用程序中,我从解析数据库中检索所有餐馆名称,并在列表顶部添加了一个搜索栏,以便我可以在列表中搜索名称。我在ListViewAdpater中遇到问题。
public class ListViewAdapter extends BaseAdapter implements Filterable {
// Declare Variables
Context mContext;
LayoutInflater inflater;
//private List<Names> namelist = null;
// private ArrayList arraylist; 私有过滤器过滤器;
private List<Names> originalData = null;
private List<Names> filteredData = null;
public ListViewAdapter(Activity context, List<Names> namelist) {
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.filteredData = namelist;
this.originalData = namelist;
mContext = context;
}
public class ViewHolder {
TextView name;
}
@Override
public int getCount() {
return filteredData.size();
}
@Override
public Object getItem(int position) {
return filteredData.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
public View getView(final int position, View view, ViewGroup parent) {
final ViewHolder holder;
if (view == null) {
holder = new ViewHolder();
view = inflater.inflate(R.layout.listview_item, null);
// Locate the TextView in listview_item.xml
holder.name = (TextView) view.findViewById(R.id.name);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
// Set the results into TextView
holder.name.setText(filteredData.get(position).getName());
// Listen for ListView Item Click
view.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// Send single item click data to SingleItemView Class
Intent intent = new Intent(mContext, SingleItemView.class);
// Pass all data number
intent.putExtra("name", (filteredData.get(position).getName()));
// Start SingleItemView Class
mContext.startActivity(intent);
}
});
return view;
}
@Override
public Filter getFilter() {
if (filter == null) filter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
String constraintStr = constraint.toString().toLowerCase(Locale.getDefault());
FilterResults result = new FilterResults();
if (constraintStr != null & constraintStr.length() > 0) {
ArrayList<Names> filterItems = new ArrayList<Names>();
synchronized (this) {
for (Names item : originalData) {
if (item.getName().toLowerCase(Locale.getDefault()).startsWith(constraintStr)) {
filterItems.add(item);
}
}
result.count = filterItems.size();
result.values = filterItems;
}
} else {
synchronized (this) {
result.count = originalData.size();
result.values = originalData;
}
}
return result;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
if (results.values != null) {
filteredData= (List<Names>) results.values;
notifyDataSetChanged();
}
}
};
return filter;
}
答案 0 :(得分:0)
使用两个arraylist一个用于原始数据,另一个用于过滤。
更新您的适配器构造函数。
private ArrayList<Names> originalData = null;
private ArrayList<Names> filteredData = null;
public adapter(Activity c, ArrayList<Names> list) {
infalter = (LayoutInflater) c
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.filteredData = list;
this.originalData = list;
mContext = c;
}
@Override
public int getCount() {
return filteredData.size();
}
@Override
public Object getItem(int position) {
return filteredData.get(position);
}
过滤器
@Override
public Filter getFilter() {
if (filter == null) filter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
String constraintStr = constraint.toString().toLowerCase(Locale.getDefault());
FilterResults result = new FilterResults();
if (constraintStr != null & constraintStr.length() > 0) {
ArrayList<Names> filterItems = new ArrayList<Names>();
synchronized (this) {
for (ContactInfo item : originalData) {
if (item.name.toLowerCase(Locale.getDefault()).startsWith(constraintStr)) {
filterItems.add(item);
}
}
result.count = filterItems.size();
result.values = filterItems;
}
} else {
synchronized (this) {
result.count = originalData.size();
result.values = originalData;
}
}
return result;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
if (results.values != null) {
namelist = (List<Names>) results.values;
notifyDataSetChanged();
}
}
};
return filter;
}