使用recyclerview搜索过滤器

时间:2016-08-27 09:33:42

标签: android android-recyclerview retrofit

我想使用改造从我的recyclerview列表中创建搜索过滤器功能,查询recyclerview.getItemAtPosition();仍然存在错误 我按照this tutorial和代码方法getFilter()使用this answer

任何解决方案? 感谢

public class MainActivity extends AppCompatActivity{

    private RecyclerView recyclerView;
    private ArrayList<AndroidVersion> data;
    private DataAdapter adapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        recyclerView.setAdapter(adapter);
        recyclerView.setOnClickListener(new AdapterView.OnItemClickListener(){
            public void onItemClick(AdapterView<?> parent, View v, int position, long id){
                String text = (AndroidVersion) recyclerView.getItemAtPosition();
                Toast.makeText(getApplicationContext(), text,
                        Toast.LENGTH_SHORT).show();
            }
        });


        initViews();
    }
    private void initViews(){
        recyclerView = (RecyclerView)findViewById(R.id.card_recycler_view);
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setHasFixedSize(true);
        loadJSON();
    }
    private void loadJSON(){
        OkHttpClient.Builder client = new OkHttpClient.Builder();
        client.readTimeout(60, TimeUnit.SECONDS);
        client.connectTimeout(60, TimeUnit.SECONDS);
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://192.168.1.14")
                .client(client.build())
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        RequestInterface request = retrofit.create(RequestInterface.class);
        Call<JSONResponse> call = request.getJSON();
        call.enqueue(new Callback<JSONResponse>() {
            @Override

            public void onResponse(Call<JSONResponse> call, Response<JSONResponse> response) {
                JSONResponse jsonResponse = response.body();
                data = new ArrayList<>(Arrays.asList(jsonResponse.getPerson()));
                adapter = new DataAdapter(data);
                recyclerView.setAdapter(adapter);
            }

            @Override
            public void onFailure(Call<JSONResponse> call, Throwable t) {
                Log.d("Error",t.getMessage());
            }
        });
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu){
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.search, menu);
        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        SearchView searchView = (SearchView) menu.findItem(R.id.listsearch).getActionView();
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
        searchView.setIconifiedByDefault(false);
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) { if ( TextUtils.isEmpty ( newText ) ) {
                adapter.getFilter().filter("");
            } else {
                adapter.getFilter().filter(newText.toString());
            }
                return true;
            }
        });
        return true;
    }
}

这是我的DataAdapter.java:

public class DataAdapter extends      
RecyclerView.Adapter<DataAdapter.ViewHolder> implements Filterable {
    private ArrayList<AndroidVersion> person;
    private ArrayList<AndroidVersion> orig;
    public DataAdapter(ArrayList<AndroidVersion> person) {
        this.person = person;
    }

    @Override
    public DataAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.card_row, viewGroup, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(DataAdapter.ViewHolder viewHolder, int i) {
        viewHolder.p_id.setText(person.get(i).getId());
    }

    @Override
    public int getItemCount() {
        return person.size();
    }

    @Override
    public Filter getFilter() {
        return new Filter() {
            @Override
            protected FilterResults performFiltering(CharSequence charSequence) {
                final FilterResults oReturn = new FilterResults();
                final ArrayList<AndroidVersion> results = new ArrayList<AndroidVersion>();
                if (orig == null)
                    orig = person;
                if (charSequence != null){
                    if(orig !=null & orig.size()>0){
                        for(final AndroidVersion g:orig){
                            if(g.getId().toLowerCase().contains(charSequence.toString()))results.add(g);
                        }
                    }
                    oReturn.values = results;
                }

                return oReturn;
            }

            @Override
            protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
                person = (ArrayList<AndroidVersion>)filterResults.values;
                notifyDataSetChanged();
            }
        };
    }

    public class ViewHolder extends RecyclerView.ViewHolder{
        private TextView p_id;
        public ViewHolder(View view) {
            super(view);

            p_id = (TextView)view.findViewById(R.id.p_id);

        }
    }
}

0 个答案:

没有答案