所以我有一个基本的自定义列表视图,其中包含每个列表项的图像和文本,我想知道如何使用edittext框动态过滤listview。我抬头看看怎么做,但我陷入了一个我不知道该怎么办的地方。
我的listview是从一个sqlitedatabase填充的,我在这里有一些方法可以从数据库中检索所有图像名称,文本和id,并将它们放入listview。到目前为止,这是我的代码,我不太确定我应该在publishResult()方法中做什么
public class MainActivity extends Activity {
ListView data;
EditText inputSearch;
ArrayList<String> names, images, ids;
ArrayAdapter<String> adapter;
MyDatabase helper;
ImageView sprite;
TextView name, id;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
data = (ListView) findViewById(R.id.listView1);
helper = new MyDatabase(this);
helper.openDataBase();
names = helper.getNameList();
images = helper.getImageList();
ids = helper.getIdList();
adapter = new MyArrayAdapter(this);
data.setAdapter(adapter);
inputSearch = (EditText) findViewById(R.id.edit_search);
inputSearch.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
adapter.getFilter().filter(cs);
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
}
private class MyArrayAdapter extends ArrayAdapter<String>{
public MyArrayAdapter(Context c) {
super(c, R.layout.item_view,names);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
if (convertView == null){
row = getLayoutInflater().inflate(R.layout.item_view, parent,false);
}
sprite = (ImageView) row.findViewById(R.id.sprite);
name = (TextView) row.findViewById(R.id.name);
id = (TextView) row.findViewById(R.id.ids);
name.setText(names.get(position).toString());
int resID = getResources().getIdentifier(images.get(position),"drawable", getPackageName());
sprite.setImageResource(resID);
id.setText(ids.get(position).toString());
return row;
}
@Override
public Filter getFilter() {
// TODO Auto-generated method stub
return new Filter(){
@Override
protected FilterResults performFiltering(CharSequence input) {
FilterResults results = new FilterResults();
int originalLength = names.size();
if (input == null || input.length() == 0){
results.values = names;
results.count = originalLength;
} else {
ArrayList<String> newList = new ArrayList<String>();
for (int i = 0; i < originalLength; i++){
String str = names.get(i);
if (str.toLowerCase(Locale.getDefault()).contains(input))
newList.add(str);
results.values = newList;
results.count = newList.size();
}
}
return results;
}
@Override
protected void publishResults(CharSequence charSequence,
FilterResults results) {
//Not sure what to do here
notifyDataSetChanged();
}
};
}
}
答案 0 :(得分:0)
那时候应该很简单......
编辑:
public class FakeAdapter extends ArrayAdapter<Model> {
public class Model {
String my_string;
int resource_id;
}
private Activity activity;
/* ArrayList to hold the original values before filtering. */
private ArrayList<Model> original_list;
/* Current Data Filter */
private Filter filter;
public RelationshipsAdapter(Activity activity, int resource,
ArrayList<Model> objects) {
super(activity, resource, objects);
this.activity = activity;
this.original_list = objects;
}
/**
* Viewholder pattern to minimize findViewById calls.
*/
private static class ViewHolder {
TextView name;
ImageView image;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Model currentmodel = getItem(position);
View rowView = null;
if(convertView == null){
rowView = activity.getLayoutInflater().inflate(R.layout.my_item, parent, false);
final ViewHolder holder = new ViewHolder();
holder.name = (TextView) rowView.findViewById(R.id.item_text);
holder.image = (ImageView) rowView.findViewById(R.id.item_image);
rowView.setTag(holder);
} else {
rowView = convertView;
}
ViewHolder holder = (ViewHolder) rowView.getTag();
holder.name.setText(currentmodel.my_string);
holder.friendship.setImageResource(currentmodel.resource_id);
return rowView;
};
/**
* Returns a filter to the calling activity to filter the underlying data set by name.
*/
@Override
public Filter getFilter(){
if(filter == null){
filter = new ModelFilter();
}
return filter;
}
/** Custom Filter implementation used to filter names alphabetically on a provided prefix. */
private class ModelFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence search) {
FilterResults filter_results = new FilterResults();
String prefix = search.toString();
/* If the search is empty, display the original dataset. */
if(UserInputValidation.checkText(prefix).equals(StatusCode.NULL_INPUT)){
filter_results.values = original_list;
filter_results.count = original_list.size();
} else {
//make a copy of the original items and use that array list
final ArrayList<RelationshipModel> results = new ArrayList<RelationshipModel>(original_list);
final ArrayList<RelationshipModel> results_holder = new ArrayList<RelationshipModel>();
int count = results.size();
for(int i = 0; i < count; i++){
final RelationshipModel model = results.get(i);
final String name = (model.first_name + " " + model.last_name).toLowerCase(Locale.US);
if(name.contains(prefix))
results_holder.add(model);
}
filter_results.values = results_holder;
filter_results.count = results_holder.size();
}
return filter_results;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
clear();
addAll((ArrayList<RelationshipModel>) results.values);
}
}
}
答案 1 :(得分:0)
试试这个,我觉得它对你很有帮助。
@Override
protected void publishResults(CharSequence charSequence,
FilterResults results) {
if(results.count==0)
{
notifyDataSetInvalidated();
}else
{
names=(ArrayList<String> names)results.values;
notifyDataSetChanged();
}
}
由于