我在整个StackOverflow上进行了搜索,试图解决我的问题,但是我似乎无法使我的过滤器正常工作。 我正在使用edittext来过滤我的json列表视图,该视图使用截击填充, 我希望能够搜索我的“ titulo”或“ dificuldade”,如果要删除EditText,我想从列表视图中重新加载数据。
以下是我到目前为止尝试过的内容。
我的适配器类:
public class AdapterTrilhos extends BaseAdapter implements Filterable {
private final Activity activity;
private List<String>originalData = null;
private LayoutInflater inflater;
private List<Trilhos> triList;
private ItemFilter mFilter = new ItemFilter();
private List<Trilhos> originalTrilhosList;
public AdapterTrilhos(List<Trilhos> triList, Activity activity) {
this.activity = activity;
this.triList = triList;
this.originalTrilhosList = new ArrayList<Trilhos>(triList);
}
//Add Below Method
public void reloadData(){
this.originalTrilhosList = new ArrayList<Trilhos>(triList);
notifyDataSetChanged();
}
@Override
public int getCount() {
return this.triList.size();
}
@Override
public Object getItem(int position) {
return this.triList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (inflater == null)
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
convertView = inflater.inflate(R.layout.dadoslista, null);
TextView idtxt = (TextView) convertView.findViewById(R.id.txtid);
TextView tittxt = (TextView) convertView.findViewById(R.id.txttit);
// TextView usertxt = convertView.findViewById(R.id.txtuser);
TextView diftxt = (TextView) convertView.findViewById(R.id.txtdif);
RatingBar ratess = (RatingBar) convertView.findViewById(R.id.ratings);
Trilhos t = triList.get(position);
idtxt.setText(String.valueOf(t.getId()));
tittxt.setText(t.getTitulo());
// usertxt.setText(String.valueOf(t.getId_user()));
diftxt.setText(t.getDificuldade());
ratess.setRating(t.getMedia());
return convertView;
}
public Filter getFilter() {
return mFilter;
}
private class ItemFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
String filterString = constraint.toString().toLowerCase();
FilterResults results = new FilterResults();
//results.values = nlist;
//results.count = nlist.size();
results.values = originalTrilhosList;
results.count = originalTrilhosList.size();
return results;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
//filteredData = (ArrayList<String>) results.values;
//movieItems = (ArrayList<Movie>) results.values;
triList.clear();
triList.addAll((ArrayList<Trilhos>) results.values);
notifyDataSetChanged();
}
}
这是我的类ListarTrilhos,在该类中我填充了列表视图,并尝试使用edittext进行过滤。
private ListView listView;
private AdapterTrilhos adapter;
SharedPreferences dados, n_trilho;
private List<Trilhos> TrilhosList = new ArrayList<Trilhos>();
private List<Trilhos> originalTrilhos= new ArrayList<Trilhos>();
这是搜索:
search.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int start, int before, int count) {
// TODO Auto-generated method stub
// When user changed the Text
String filterString = cs.toString().toLowerCase();
Log.e("TAG", "filterString:" + filterString);
TrilhosList.clear();
if (TextUtils.isEmpty(filterString)) {
TrilhosList.addAll(originalTrilhos);
}
String filterableString;
for (Trilhos tril : originalTrilhos) {
//search from the title field
if (tril.getTitulo().toLowerCase().contains(filterString)) {
TrilhosList.add(tril);
}
//search from the year field
else if (String.valueOf(tril.getDificuldade()).toLowerCase().contains(filterString))
{
TrilhosList.add(tril);
}
}
adapter.notifyDataSetChanged();
//FLAGS Cannot resolve method 'getFilter()' here
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
// String text = search.getText().toString().toLowerCase(Locale.getDefault());
// adapter.filter(text);
}
});
criarLista();
最后是我在凌空填入列表的地方
public void criarLista() {
RequestQueue mRequestQueue = Volley.newRequestQueue(getApplicationContext());
JsonArrayRequest request = new JsonArrayRequest(
urlget,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
int count = response.length();
for (int i = 0; i < count; i++) {
try {
JSONObject jo = response.getJSONObject(i);
Trilhos tri = new Trilhos();
tri.setId(jo.getInt("id"));
tri.setTitulo(jo.getString("titulo"));
tri.setId_user(jo.getInt("id_user"));
tri.setDificuldade(jo.getString("dificuldade"));
float test = BigDecimal.valueOf(jo.getDouble("media")).floatValue();
tri.setMedia(test);
TrilhosList.add(tri);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// Toast.makeText(getApplicationContext(), response.toString(), Toast.LENGTH_LONG).show();
adapter.notifyDataSetChanged();
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(), "onErrorResponse", Toast.LENGTH_LONG).show();
}
}
);
Object TAG_REQUEST_QUEUE = new Object();
request.setTag(TAG_REQUEST_QUEUE);
mRequestQueue.add(request);
mRequestQueue.start();
}
列表视图可以完美加载,但是当我在edittext中插入任何内容后,它就变成空白,
感谢您的帮助
答案 0 :(得分:0)
尝试使用text watcher类。您可以查看文档https://developer.android.com/reference/android/text/TextWatcher
答案 1 :(得分:0)
我在解决问题时将所有列表更改为arraylists!我会留下我的答案,以防万一有人需要,它可以完美地工作!
在我的主线上
search.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int start, int before, int count) {
// TODO Auto-generated method stub
// When user changed the Text
adapter.getFilter().filter(cs.toString());
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
在适配器上
private ArrayList<Trilhos> triList;
private ArrayList <Trilhos> mStringList;
private ValueFilter valueFilter;
public AdapterTrilhos(ArrayList<Trilhos> triList, Activity activity) {
this.activity = activity;
this.triList = triList;
mStringList=triList;
getFilter();
}
@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<Trilhos> filterList= new ArrayList<Trilhos>();
for(int i=0; i<mStringList.size();i++){
if ((mStringList.get(i).getTitulo().toLowerCase()).contains(constraint.toString().toLowerCase())){
Trilhos trilhos = new Trilhos();
trilhos.setTitulo(mStringList.get(i).getTitulo());
filterList.add(trilhos);
}
}
results.count=filterList.size();
results.values=filterList;
}else {
results.count=mStringList.size();
results.values=mStringList;
}
return results;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
triList=(ArrayList<Trilhos>) results.values;
notifyDataSetChanged();
}
}