我想在recyclerview中实现搜索功能,使用volly来自srver的数据

时间:2019-04-20 14:05:35

标签: android search android-recyclerview server android-volley

我想在recyclerview中实现搜索功能。我正在从服务器获取数据,并使用edittext进行搜索。 我遵循本教程https://www.simplifiedcoding.net/search-functionality-recyclerview/ 在此示例中,教师在recyclerview中使用静态数据,但我使用的是单独的方法和volly。

错误:此行出现错误

  

AdapterAll.filterList(filteredList);消息为“无法解析方法filterList”

public class HomeFragment extends Fragment implements View.OnClickListener {

EditText searchEdittext;

private String DATA_URL_ALL = "http://www.shakeel.com/Recipes/all_recipes.php";
private RecyclerView allRecyclerView;
private RecyclerView.Adapter AdapterAll;
private List<ListItem> listItemsAll, filteredList;

public HomeFragment() {
    // Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_home, container, false);

    searchEdittext = view.findViewById(R.id.search_edittext);

    allRecyclerView = view.findViewById(R.id.all_recipes_recycleview);
    allRecyclerView.setLayoutManager(new GridLayoutManager(getContext(), 2));
    listItemsAll = new ArrayList<>();

    popularRecycleview = view.findViewById(R.id.popular_recipes_recycleview);
    popularRecycleview.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayout.HORIZONTAL, false));

    filteredList = new ArrayList<>();
    listItemsPopular = new ArrayList<>();



    searchEdittext.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) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            filter(s.toString());
        }
    });

    loadRecycleDataAll();
    loadPopularRecipesData();

    return view;
}

   private void filter(String text) 
   {
       for (ListItem item : listItemsAll){
            if (item.getTitle().toLowerCase().contains(text.toLowerCase())){
            filteredList.add(item);
        }
    }
    AdapterAll.filterList(filteredList);
}

private void loadRecycleDataAll() {
    final ProgressDialog progressDialog = new ProgressDialog(getContext());
    progressDialog.setMessage("Loading...");
    progressDialog.show();

    RequestQueue requestQueue = Volley.newRequestQueue(getContext());
    StringRequest stringRequest = new StringRequest(Request.Method.GET, DATA_URL_ALL,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    progressDialog.dismiss();
                    try {

                        JSONArray array = new JSONArray(response);

                        for (int i = 0; i < array.length(); i++) {
                            JSONObject o = array.getJSONObject(i);
                            ListItem items = new ListItem(
                                    o.getInt("id"),
                                    o.getString("title"),
                                    o.getString("time"),
                                    o.getString("serves"),
                                    o.getString("ingredients"),
                                    o.getString("directions"),
                                    o.getString("image"),
                                    o.getString("thumbnail"),
                                    o.getString("calories"),
                                    o.getString("fats"),
                                    o.getString("carbohydrates"),
                                    o.getString("protein"),
                                    o.getString("notes"),
                                    o.getInt("likes")
                            );
                            listItemsAll.add(items);
                        }

                        AdapterAll = new AllRecipesAdapter(listItemsAll, getContext());
                        allRecyclerView.setAdapter(AdapterAll);

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    progressDialog.dismiss();
                }
            }
    );
    requestQueue.add(stringRequest);
}
}
  1. 适配器类

    公共类AllRecipesAdapter扩展了RecyclerView.Adapter {

    private List<ListItem> recipesListItems;
    private Context context;
    
    public AllRecipesAdapter(List<ListItem> recipesListItems, Context context) {
        this.recipesListItems = recipesListItems;
        this.context = context;
    }
    
    
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view = inflater.inflate(R.layout.all_recipes_list_item, parent, false);
        return new MyViewHolder(view);
    }
    
    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {
        final ListItem listItem = recipesListItems.get(position);
    
        holder.recipeTitle.setText(listItem.getTitle());
        holder.recipeTime.setText(listItem.getTime());
        holder.recipeServe.setText(listItem.getServe());
        Picasso.with(context).load(listItem.getImg()).into(holder.imgView);
    }
    
    @Override
    public int getItemCount() {
        return recipesListItems.size();
    }
    
    public class MyViewHolder extends RecyclerView.ViewHolder{
    
        public TextView recipeTitle, recipeTime, recipeServe;
        public FrameLayout itemLayout;
        ImageView imgView;
    
        public MyViewHolder(View itemView) {
            super(itemView);
            recipeTitle = itemView.findViewById(R.id.titleTextView);
            recipeTime = itemView.findViewById(R.id.timeTextView);
            recipeServe = itemView.findViewById(R.id.serveTextView);
            itemLayout = itemView.findViewById(R.id.frameLayout);
            imgView = itemView.findViewById(R.id.recipeImgView);
        }
    }
    
    public void filterList(List<ListItem> filterList){
        recipesListItems = filterList;
        notifyDataSetChanged();
    }
    }
    

0 个答案:

没有答案