使用自定义适配器从列表视图中搜索项目

Search items from listview with custom adapter

我正在尝试使用自定义适配器从列表视图中进行搜索。我在工具栏菜单中有搜索小部件,我可以显示小部件。当我单击搜索图标时,它会展开,但是当我开始输入时,搜索不会发生并且列表会被清除。有人可以追踪这个问题吗?

这是我来自 Main Activity 的代码:

    public class VideoActivity extends BaseActivity {
    ListView listView;
    ArrayList<Video> videoArrayList;
    public VideoListAdapter videoListAdapter;

    String vid_id, vid_title, vid_type, vid_path, vid_img;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video);

        initializeToolbar();

        listView = (ListView) findViewById(R.id.listview_video);
        videoArrayList = new ArrayList<Video>();
        videoListAdapter = new VideoListAdapter(VideoActivity.this, videoArrayList);

        jsonParser = new JSONParser();

        new VideoTask().execute();

    public class VideoTask extends AsyncTask<String, String, JSONObject> {

        @Override
        protected JSONObject doInBackground(String... strings) {
            jsonObject = jsonParser.makeHttpRequest2(Network_constants.video_list_page, "POST");

            Log.e("json data", "" + jsonObject);

            return jsonObject;
        }


        @Override
        protected void onPostExecute(JSONObject jsonObject) {
            super.onPostExecute(jsonObject);
            progressDialog.dismiss();
            if (jsonObject != null) {
                try {
                    jsonArray = jsonObject.getJSONArray("videos");
                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject jsonObject1 = jsonArray.getJSONObject(i);
                        Log.d("jsonObject1", "" + jsonObject1);

                        Video video = new Video();

                        vid_id = jsonObject1.getString("video_id");
                        vid_title = jsonObject1.getString("video_channel_name");
                        vid_type = jsonObject1.getString("video_channel_link");
                        vid_path = jsonObject1.getString("video_channel_description");
                        vid_img = jsonObject1.getString("video_img_path");

                        video.set_vTitle(vid_title);

                        TextView t1 = (TextView) findViewById(R.id.video_title);
                        TextView t2 = (TextView) findViewById(R.id.video_type);

                        video.set_vArtist(vid_title);
                        video.set_vType(vid_type);

                        videoArrayList.add(video);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                listView.setAdapter(videoListAdapter);
                listView.setTextFilterEnabled(true);
                listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {


                        TextView cnt = (TextView) view.findViewById(R.id.video_type);
                        String cn = cnt.getText().toString();

                        Intent i = new Intent(VideoActivity.this, Youtube.class);
                        i.putExtra("url", cn);
                        startActivity(i);
                    }
                });
            }
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        menu.clear();
        getMenuInflater().inflate(R.menu.main,menu);

        MenuItem menuItem = menu.findItem(R.id.menu_search);
        final SearchView searchView = (SearchView) menuItem.getActionView();

        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                if(TextUtils.isEmpty(newText)){
                    LogHelper.e("Query Result","Filter is Empty");
                }else{
                    videoListAdapter.filter(newText);
                }
                return true;
            }
        });

        return true;
    }
}

这是我的 CustomAdapter class :

    public class VideoListAdapter extends BaseAdapter {
    private Activity activity;
    private static LayoutInflater inflater = null;
    ImageView imageView;
    private String mCurrentArtUrl;
    private ArrayList<Video> video;
    private ArrayList<Video> searchListView = null;



    public VideoListAdapter(Activity a, ArrayList<Video> b){

        this.activity = a;
        this.video = b;
        this.searchListView = new ArrayList<Video>();
        this.searchListView.addAll(video);
        inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    }

    @Override
    public int getCount() {
        return video.size();
    }

    @Override
    public Object getItem(int position) {
        return video.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View vi = convertView;
        if (convertView == null)
            vi = inflater.inflate(R.layout.video_list_item, null);

        final TextView v_title = (TextView)vi.findViewById(R.id.video_title);
        final TextView v_type = (TextView)vi.findViewById(R.id.video_type);
        final TextView v_artist = (TextView)vi.findViewById(R.id.video_artist);
        imageView = (ImageView) vi.findViewById(R.id.video_image);

        Video video1 = video.get(position);

        v_title.setText(video1.get_vTitle());
        v_type.setText(video1.get_vType());
        v_artist.setText(video1.get_vArtist());

        String img = video1.get_vImage();
        String profile = Network_constants.image_url + img;
        fetchImageAsync(profile);

        return vi;
    }

    // Filter method
    public void filter(String charText) {
        charText = charText.toLowerCase(Locale.getDefault());
        video.clear();

        if(charText.length() == 0){
            video.addAll(searchListView);
        }else{
            for(Video v : searchListView){
                LogHelper.e("Query","Entered For Loop");
                    if(v.get_vTitle().contains(charText)){
                        video.add(v);
                }else{
                    LogHelper.e("Query","Could not create list");
                }

            }
        }
        notifyDataSetChanged();
    }
}

我想我的代码无法使用过滤器函数中的 for 循环。

您应该在您的适配器

中实现 Filterable