我正在使用recyclerview来显示片段上的项目。我已经使用addtextchangedlistener在我的应用中实现了searh功能。当我要在edittext中输入文字时,recycler视图中的所有项目都会消失,我无法获得过滤结果。这是我的addtextchangedlistener
的代码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中获得过滤结果。
答案 0 :(得分:0)
删除此代码
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);
答案 1 :(得分:0)
在您的代码中,您一次又一次地调用以下代码;
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();