搜索在ArrayAdapter和ListView不起作用 [英] Searching doesn't work in ArrayAdapter and ListView
本文介绍了搜索在ArrayAdapter和ListView不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
不幸的是我无法实现在ArrayAdapter里面一个ListView搜索功能。
这里是我的MainActivity的一部分:
Unfortunately I couldn't implement a search functionality in a ListView inside an ArrayAdapter. Here is a part of my MainActivity:
public class MainPageActivity extends ActionBarActivity implements View.OnClickListener {
private static String TAG = "MainPageActivity";
private ListView lv_medicines;
private MedicineAdapter medicineAdapter;
private JSONParser jsonParser;
private List<Medicine> medicines;
private EditText inputSearch;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_page);
init();
inputSearch.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable arg0) {
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1,
int arg2, int arg3) {
}
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2,
int arg3) {
MainPageActivity.this.medicineAdapter.getFilter().filter(cs);
}
});
/*...*/
private void init() {
lv_medicines = (ListView) findViewById(R.id.lv_medicines);
lv_medicines.setTextFilterEnabled(true);
inputSearch = (EditText) findViewById(R.id.inputSearch);
medicineAdapter = new MedicineAdapter(getApplicationContext(), hatoanyagok);
lv_medicines.setAdapter(medicineAdapter);
}
/*...*/
}
这是我的ArrayAdapter:
And here is my ArrayAdapter:
public class MedicineAdapter extends ArrayAdapter<String> implements Filterable{
private final Context context;
private List<String> elements;
private List<String> filteredData;
public MedicineAdapter(Context context, List<String> elements) {
super(context, R.layout.row_item, elements);
this.context = context;
this.elements = elements;
this.filteredData = elements;
}
public String getItem(int pos) {
return filteredData.get(pos);
}
public int getCount() {
return filteredData.size();
}
public long getItemId(int position) {
return position;
}
public void deleteRow(String element) {
if (filteredData.contains(element)) {
filteredData.remove(element);
}
}
public List<String> getObjects() {
return filteredData;
}
public void setObjects(List<String> objects) {
this.elements = elements;
}
public List<String> getValues() {
return filteredData;
}
private static class ViewHolder {
TextView tv_medicine_name;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
viewHolder = new ViewHolder();
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.row_item, parent, false);
viewHolder.tv_medicine_name = (TextView) convertView.findViewById(R.id.tv_medicine_name);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.tv_medicine_name.setText(filteredData.get(position));
return convertView;
}
public void filter(String charText) {
charText = charText.toLowerCase(Locale.getDefault());
filteredData.clear();
if (charText.length() == 0) {
filteredData.addAll(elements);
}
else
{
for (String s : elements)
{
if (s.toLowerCase(Locale.getDefault()).contains(charText))
{
filteredData.add(s);
}
}
}
notifyDataSetChanged();
}
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
}
}
会是什么问题呢?
在此先感谢您的答案!
What would be the problem? Thanks in advance for the answers!
推荐答案
您需要作一次延伸类过滤器
public class filter_here extends Filter{
private List<String> Original_Names;
private MedicineAdapter medicineAdapter;
public filter_here(List<String> Original_Names, MedicineAdapter ma){
this.Original_Names = Original_Names;
medicineAdapter = ma;
}
@Override
protected FilterResults performFiltering(CharSequence constraint) {
// TODO Auto-generated method stub
FilterResults Result = new FilterResults();
// if constraint is empty return the original names
if(constraint.length() == 0 ){
Result.values = Original_Names;
Result.count = Original_Names.size();
return Result;
}
ArrayList<String> Filtered_Names = new ArrayList<String>();
String filterString = constraint.toString().toLowerCase();
String filterableString;
for(int i = 0; i<Original_Names.size(); i++){
filterableString = Original_Names.get(i);
if(filterableString.toLowerCase().contains(filterString)){
Filtered_Names.add(filterableString);
}
}
Result.values = Filtered_Names;
Result.count = Filtered_Names.size();
return Result;
}
@Override
protected void publishResults(CharSequence constraint,FilterResults results) {
// TODO Auto-generated method stub
medicineAdapter.setObjects((ArrayList<String>) results.values);
medicineAdapter.notifyDataSetChanged();
}
}
在你的化妆适配器对象的 filter_here 类,这行添加到您的适配器的构造
In your adapter make object of filter_here class and add this line to your adapter's constructor
filterObj = new filter_here(elements, this);
此外更改适配器类的这个方法。
Also change this method of your adapter class
public void setObjects(List<String> objects) {
this.filteredData = objects;
}
此方法添加到您的适配器
Add this method to your adapter
@Override
public Filter getFilter() {
// TODO Auto-generated method stub
return filterObj;
}
您不需要过滤方法在你的适配器。
保留一切,因为它是。
You dont need filter method in your adapter. Keep everything else as it is.
这篇关于搜索在ArrayAdapter和ListView不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文