在我的应用程序中,顺利显示 CSV文件中的数据。但无法添加搜索功能。任何帮助将不胜感激。
如何实施getFilter().filter(s.toString())
?
在主Java类中:
search_edit.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// Call back the Adapter with current character to Filter
mAdapter.getFilter().filter(s.toString());
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
我的适配器:
public class CSVAdapter extends ArrayAdapter<State> {
Context ctx;
private static LayoutInflater inflater=null;
public CSVAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
this.ctx = context;
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
loadArrayFromFile();
}
@Override
public View getView(final int pos, View convertView, final ViewGroup parent){
View rowView;
rowView = inflater.inflate(R.layout.city_element, null);
TextView city_name_txtObj=(TextView)rowView.findViewById(R.id.city_name_txt);
city_name_txtObj.setText(getItem(pos).getName());
return rowView;
}
private void loadArrayFromFile(){
try {
// Get input stream and Buffered Reader for our data file.
InputStream is = null;
try {
is = ctx.getAssets().open("area.csv");
} catch (IOException e) {
e.printStackTrace();
}
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
String line;
//Read each line
while ((line = reader.readLine()) != null) {
//Split to separate the name from the capital
String[] RowData = line.split(",");
//Create a State object for this row's data.
State cur = new State();
cur.setName(RowData[1]);
cur.setPin(RowData[2]);
cur.setLocation(RowData[3]);
cur.setLatitude(RowData[4]);
cur.setLongitude(RowData[5]);
//Add the State object to the ArrayList (in this case we are the ArrayList).
this.add(cur);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
State是我的模特课:
public class State {
private String name,pinCode,location,latitude,longitude;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPin() { return pinCode;}
public void setPin(String pinCode) {
this.pinCode = pinCode;
}
public String getLocation() { return location;}
public void setLocation(String location) {
this.location = location;
}
public String getLatitude() { return latitude;}
public void setLatitude(String latitude) {
this.latitude = latitude;
}
public String getLongitude() { return longitude;}
public void setLongitude(String longitude) {
this.longitude = longitude;
}
}
也许这个问题太基础了,但我找不到合适的解决方案。请帮帮我。
答案 0 :(得分:0)
要进行过滤,您需要实施Filterable
,还需要创建两个数据副本
public class CSVAdapter extends ArrayAdapter<State> implements Filterable {
@Override
public Filter getFilter() {
if (valueFilter == null) {
valueFilter = new ValueFilter();
}
return valueFilter;
}
private class ValueFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
if (constraint != null && constraint.length() > 0) {
ArrayList<Country> filterList = new ArrayList<Country>();
for (int i = 0; i < mStringFilterList.size(); i++) {
if ( (mStringFilterList.get(i).getName().toUpperCase() )
.contains(constraint.toString().toUpperCase())) {
Country country = new Country(mStringFilterList.get(i)
.getName() , mStringFilterList.get(i)
.getIso_code() , mStringFilterList.get(i)
.getFlag());
filterList.add(country);
}
}
results.count = filterList.size();
results.values = filterList;
} else {
results.count = mStringFilterList.size();
results.values = mStringFilterList;
}
return results;
}
@Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
countrylist = (ArrayList<Country>) results.values;
notifyDataSetChanged();
}
完整示例Visit