我想过滤使用volley填充的Listview。我正在使用 ListAdapter 和 SimpleAdapter 来填充listView及其工作正常......但是当我在editText上实现TEXTWATCHER并添加 getFilter()时对它...它给了一个空气(无法解决方法' getFilter()')
帮助我...我怎样才能过滤我的清单? 代码低于......
import android.app.ProgressDialog;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class grabedDb extends AppCompatActivity {
EditText grabResults;
String url = "http://MY_URL_HERE";
ArrayList<HashMap<String, String>> Item_List;
ProgressDialog PD;
ArrayAdapter adapter;
// JSON Node names
public static final String ITEM_ID = "name";
public static final String ITEM_NAME = "rate";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_grabed_db);
grabResults = (EditText) findViewById(R.id.grabed_et_search);
grabResults.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
//perform Search
if (s.toString().equals("")){
//Reset List
ReadDataFromDB();
} else {
grabedDb.this.adapter.getFilter().filter(s);
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
Item_List = new ArrayList<HashMap<String, String>>();
PD = new ProgressDialog(this);
PD.setMessage("Please Wait...");
PD.setCancelable(false);
ReadDataFromDB();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_main_actions, menu);
return super.onCreateOptionsMenu(menu);
}
private void ReadDataFromDB() {
PD.show();
JsonObjectRequest jreq = new JsonObjectRequest(Request.Method.GET, url,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
int success = response.getInt("success");
if (success == 1) {
JSONArray ja = response.getJSONArray("mydrugs");
for (int i = 0; i < ja.length(); i++) {
JSONObject jobj = ja.getJSONObject(i);
HashMap<String, String> item = new HashMap<String, String>();
item.put(ITEM_ID, jobj.getString(ITEM_ID));
item.put(ITEM_NAME,
jobj.getString(ITEM_NAME));
Item_List.add(item);
} // for loop ends
String[] from = { ITEM_ID, ITEM_NAME };
int[] to = { R.id.item_name, R.id.item_id };
adapter = new SimpleAdapter(
getApplicationContext(), Item_List,
R.layout.list_items, from, to);
ListView myList = (ListView) findViewById(R.id.list_list_listView);
myList.setAdapter(adapter);
PD.dismiss();
myList.setOnItemClickListener(new AdapterView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Intent modify_intent = new Intent(grabedDb.this,
updatedata.class);
modify_intent.putExtra("item", Item_List.get(position));
startActivity(modify_intent);
}
});
} // if ends
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
PD.dismiss();
}
});
// Adding request to request queue
MyApplication.getInstance().addToReqQueue(jreq);
}
}
答案 0 :(得分:1)
我认为使用ListAdapter和SimpleAdapter不是一个好选择......你必须用SimpleAdapter替换ListAdapter ......
它对我有用......