如何实现onTextChangedListenerItem来搜索RecyclerView中的项目
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何实现onTextChangedListenerItem来搜索RecyclerView中的项目相关的知识,希望对你有一定的参考价值。
我正在使用recyclerview来显示片段上的项目。我已经使用addtextchangedlistener在我的应用程序中实现了searh功能。当我要在edittext中键入文本时,recycler视图中的所有项目都消失了,我无法获得过滤结果。这是myaddtextchangedlistener的代码
private void addTextListener() {
search.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) {
s=s.toString().toLowerCase();
final ArrayList<CurrentEntry> filter=new ArrayList<>();
for(int i=0;i<filter.size();i++)
{
final String text=current.get(i).toString().toLowerCase();
if(text.contains(s))
{
filter.add(current.get(i));
}
}
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
adapt=new NewAdapter(filter,getActivity());
recyclerView.setAdapter(adapt);
adapt.notifyDataSetChanged();
}
@Override
public void afterTextChanged(Editable s) {
//filter(s.toString());
}
});
}
这是我使用jsonrequest的volley
private void getData() {
String json_req = "json_req";
String url =getContext().getString(R.string.CURRENT_STATUS);
JsonObjectRequest req = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try
{
JSONObject object=new JSONObject(response.toString());
JSONArray data=object.getJSONArray("results");
for(int i=0;i<data.length();i++)
{
JSONObject json = data.getJSONObject(i);
final CurrentEntry c=new CurrentEntry();
userUrl=json.getString("name");
JsonObjectRequest request=new JsonObjectRequest(Request.Method.GET, userUrl, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
String foodie_name=response.getString("username");
c.setName(name);
} catch (JSONException e) {
e.printStackTrace();
}
adapt.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}
)
{
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> headers = new HashMap<>();
String credentials = ""+":"+"";
String auth = "Basic " + Base64.encodeToString(credentials.getBytes(), Base64.NO_WRAP);
headers.put("Content-type", "application/json");
headers.put("Authorization", auth);
return headers;
}
};
AppController.getInstance().addToRequestQueue(request,second_req);
c.setPeople(json.getString("my_people"));
String temp=json.getString("my_timetime");
temp=temp.substring(11);
temp=temp.substring(0,temp.length()-4);
c.setEstimate(temp);
current.add(c);
}
recyclerView.setAdapter(adapt);
adapt.notifyDataSetChanged();
}
catch (JSONException e)
{
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
})
{
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> headers = new HashMap<>();
String credentials = ""+":"+"";
String auth = "Basic " + Base64.encodeToString(credentials.getBytes(), Base64.NO_WRAP);
headers.put("Content-type", "application/json");
headers.put("Authorization", auth);
return headers;
}
};
AppController.getInstance().addToRequestQueue(req, json_req);
}
如何解决这个问题,以便在recyclerview中获得过滤结果。
答案
删除此代码
adapt=new NewAdapter(filter,getActivity());
recyclerView.setAdapter(adapt);
adapt.notifyDataSetChanged();
每次更改文本时都不需要创建NewAdapter。相反,尝试更改适配器列表的值并调用notifyDataSetChanged(),如下所示。
adapt.filter=filter;
adapt.notifyDataSetChanged();
并在你的onCreate()中添加这些行
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
adapt=new NewAdapter(filter,getActivity());
recyclerView.setAdapter(adapt);
另一答案
在您的代码中,您一次又一次地调用以下代码;
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
adapt=new NewAdapter(data,getActivity());
recyclerView.setAdapter(adapt);
adapt.notifyDataSetChanged();
只需在片段的onCreateView中编写此代码一次:
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
adapt=new NewAdapter(data,getActivity());
recyclerView.setAdapter(adapt);
现在,在您的适配器类中,您需要拥有一个数据设置器,可用于设置从您的API中获取的更新数据,以及您视图中的搜索过滤器。并发布你只需要调用adapt.notifyDataSetChanged():
adapt.setNewData(data);
adapt.notifyDataSetChanged();
以上是关于如何实现onTextChangedListenerItem来搜索RecyclerView中的项目的主要内容,如果未能解决你的问题,请参考以下文章