getFilter() 不适用于 ListAdapter
Posted
技术标签:
【中文标题】getFilter() 不适用于 ListAdapter【英文标题】:getFilter() is not working with ListAdapter 【发布时间】:2016-12-01 13:11:39 【问题描述】:我想过滤使用 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);
【问题讨论】:
【参考方案1】:我认为将 ListAdapter 与 SimpleAdapter 一起使用不是很好的选择...您必须将 ListAdapter 替换为 SimpleAdapter...
它对我有用...
【讨论】:
以上是关于getFilter() 不适用于 ListAdapter的主要内容,如果未能解决你的问题,请参考以下文章
如何在 BaseAdapter 上实现 getFilter?
自定义 ArrayAdapter 上的 getFilter() 不起作用
Android listview 不能用 android 中的 getFilter() 更新
android中自定义ArrayAdapter中的自定义getFilter