搜索项目在Recyclerview中,SearchView过滤国家/地区列表

时间:2017-03-05 21:48:48

标签: android

我是新来的,我是android的新手,我有来自服务器获取数据的国家列表,尝试了所有解决方案,没有任何作用,我需要过滤提取的列表, 我已经应用了这一课 Search through RecyclerView using Searchview

任何解决方案将不胜感激, 提前谢谢。

public class Cities extends AppCompatActivity  {

    private static RecyclerView.Adapter adapter;
    private RecyclerView.LayoutManager layoutManager;
    private  RecyclerView recyclerView;
    private static ArrayList<CitiesModel> cititesNames;
    private RecyclerViewUtils.ShowHideToolbarOnScrollingListener showHideToolbarListener;

    private EditText mSearchView;
    private CitiesAdapter citiesAdapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cities);

        setRecycUp();
        getCities();


    }

    public void setRecycUp() {
        mSearchView = (EditText) findViewById(R.id.search_view);
        recyclerView = (RecyclerView) findViewById(R.id.city);
        layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setHasFixedSize(true);

    }

    public void getCities() {
        JsonArrayRequest arrayRequest=new JsonArrayRequest(Config.Root+ Config.FITCHCITIES, new Response.Listener<JSONArray>() {
            @Override
            public void onResponse(JSONArray response) {

                cititesNames=new ArrayList<CitiesModel>();


                for(int i=0;i<response.length();i++){

                    try {

                        JSONObject jsonObject= (JSONObject) response.get(i);

                        CitiesModel citiesModel=new CitiesModel();

                        String CityName = jsonObject.getString("city");

                        citiesModel.setCity(CityName);


                        cititesNames.add(citiesModel);


                       adapter = new CitiesAdapter(Cities.this,cititesNames);
                        recyclerView.setAdapter(adapter);


                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

                if (error instanceof TimeoutError) {
                    Toast.makeText(getApplicationContext(), "time out Error", Toast.LENGTH_LONG).show();
                } else if (error instanceof NoConnectionError) {
                    Toast.makeText(getApplicationContext(), "no Connection Error", Toast.LENGTH_LONG).show();
                } else if (error instanceof AuthFailureError) {
                    Toast.makeText(getApplicationContext(), "Authenticatin Failure Error", Toast.LENGTH_LONG).show();
                } else if (error instanceof NetworkError) {
                    Toast.makeText(getApplicationContext(), "Network Error", Toast.LENGTH_LONG).show();
                } else if (error instanceof ServerError) {
                    Toast.makeText(getApplicationContext(), "Server Error", Toast.LENGTH_LONG).show();
                } else if (error instanceof ParseError) {
                    Toast.makeText(getApplicationContext(),"JSon Error", Toast.LENGTH_LONG).show();
                }
            }
        })
                ;

        MySingleton.getInstance(getApplicationContext()).addToRequestQueue(arrayRequest);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        return true;
    }

}

这是我的适配器

public class CitiesAdapter extends RecyclerView.Adapter<CitiesAdapter.MyViewHolder> {

    private List<CitiesModel> dataSet;

    private static Context mCtx;

    public static class MyViewHolder extends RecyclerView.ViewHolder {

        TextView textViewCity;
        private  Activity mActivity;

        public MyViewHolder(View itemView,Activity mActivity) {
            super(itemView);
            this.textViewCity = (TextView) itemView.findViewById(R.id.cities);
            mCtx = itemView.getContext();
            this.mActivity=mActivity;
        }
    }
    private  Activity mActivity;

    public CitiesAdapter(Activity mActivity,List<CitiesModel> data) {

        this.dataSet = data;
        this.mActivity=mActivity;

    }

    @Override
    public CitiesAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent,
                                                         int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.cities, parent, false);
        CitiesAdapter.MyViewHolder myViewHolder = new CitiesAdapter.MyViewHolder(view,mActivity);
        return myViewHolder;
    }

    @Override
    public void onBindViewHolder(final CitiesAdapter.MyViewHolder holder, final int listPosition) {

        CitiesModel citiesModel = dataSet.get(listPosition);

        TextView textViewcity = holder.textViewCity;
        textViewcity.setText(citiesModel.getCity().toString().trim());


        holder.itemView.setOnClickListener(new
                                                   View.OnClickListener() {
                                                       @Override
                                                       public void onClick(View view) {
                                                           mCtx = holder.itemView.getContext();

                                                           String prodtype = dataSet.get(listPosition).getCity();
                                                           Intent type = new Intent(mCtx, SignUp.class);
                                                           type.putExtra("TYPE", prodtype);
                                                           mActivity.setResult(0, type);
                                                           mActivity.finish();

//                                                           Toast.makeText(mCtx,dataSet.get(listPosition).getCity(), Toast.LENGTH_SHORT).show();
                                                       }
                                                   });

    }

    @Override
    public int getItemCount() {

        return (null != dataSet ? dataSet.size() : 0);
    }

}

的DataModel

public class CitiesModel {
        public String getCity() {
            return city;
        }

        public void setCity(String city) {
            this.city = city;
        }

        String city;
    }

0 个答案:

没有答案