我正在努力使用EditText上的TextWatcher 过滤 listView,List由 SimpleAdapter 填充,并从Web服务器获取数据。实际上,我在SimpleAdapter和ArrayList中使用 ListAdapter (参见下面的代码)。
我搜索了SO here& here以及更多网站,但未找到我的错误解决方案。
错误位于TextWatcher方法中的以下行:
grabedDb.this.adapter.getFilter().filter(s);
getFilter()函数无效。(无法解析方法'getFilter()')。请帮我解决这个错误。
grabDb.java
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_LINK_HERE";
ArrayList<HashMap<String, String>> Item_List;
ProgressDialog PD;
ListAdapter 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);
/////////////////////
//////////TextWatcher////////////
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 {
////////////////// Error on getFilter() method ////////////
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);
}
}
我不想使用ListActivity
答案 0 :(得分:2)
只需将 ListAdapter 替换为 SimpleAdapter ,并保持一致。 它会起作用。