过滤从Android中的PagerAdaper加载的ListView

时间:2015-03-22 16:42:43

标签: java android listview android-viewpager android-pageradapter

我正在使用this项目并以我的方式对其进行编辑。我为每个页面使用了通用布局。该布局中有一个ListView。我正在检查页面的位置,并根据该位置设置不同的视图。

我的操作栏中有一个editText。我想根据editText的onTextChanged过滤当前页面的列表视图。一切都很好,但过滤没有发生。

这是我的完整代码:

public class MainActivity extends ActionBarActivity {

    public static TextView actionbarTitle;
    public static EditText listViewEditText;
    public static ImageButton listViewSearch;
    public static Context context;
    public static ListView songslist;
    public static final int MAX_PAGES = 1;

    private int num_pages = 5;
    public ArrayList<String> title;
    public ArrayList<Long> albumId;
    public ArrayList albumName;
    public ArrayList songs;
    public ArrayList artist;
    public ArrayList songsPath;
    public ArrayList songsDuration;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        context = this;

        populate();
        ActionBar actionBar = getSupportActionBar();
        LayoutInflater inflator = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = inflator.inflate(R.layout.customactionbar, null);
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setDisplayShowCustomEnabled(true);
        actionBar.setDisplayShowTitleEnabled(false);
        actionBar.setIcon(R.drawable.ic_launcher);
        actionBar.setCustomView(v);
        listViewSearch = (ImageButton) findViewById(R.id.listViewSearch);
        listViewEditText = (EditText) findViewById(R.id.editText);
        actionbarTitle = (TextView) findViewById(R.id.text_left);

        listViewSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                actionbarTitle.setVisibility(View.GONE);
                listViewEditText.setVisibility(View.VISIBLE);
            }
        });


        final ViewPagerParallax pager = (ViewPagerParallax) findViewById(R.id.pager);
        pager.set_max_pages(MAX_PAGES);
        pager.setBackgroundAsset(R.drawable.picture);
        pager.setAdapter(new my_adapter());

        pager.getAdapter().notifyDataSetChanged();

        if (savedInstanceState != null) {
            num_pages = savedInstanceState.getInt("num_pages");
            pager.setCurrentItem(savedInstanceState.getInt("current_page"), false);
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt("num_pages", num_pages);
        final ViewPagerParallax pager = (ViewPagerParallax) findViewById(R.id.pager);
        outState.putInt("current_page", pager.getCurrentItem());
    }

    private class my_adapter extends PagerAdapter {
        @Override
        public int getCount() {
            return num_pages;
        }

        @Override
        public boolean isViewFromObject(View view, Object o) {
            return view == o;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

        @Override
        public Object instantiateItem(final ViewGroup container, final int position) {
            View new_view = null;

            LayoutInflater inflater = getLayoutInflater();
            new_view = inflater.inflate(R.layout.page, null);
            songslist = (ListView) new_view.findViewById(R.id.listView);
            if (position == 0) {
                songslist.setAdapter(new CustomListViewSongs(context, title, artist, songsDuration));
            } else if (position == 1) {
                songslist.setAdapter(new CustomListViewSongs(context, songsDuration, artist, songsDuration));
            } else if (position == 2) {
                songslist.setAdapter(new CustomListViewSongs(context, songsPath, artist, songsDuration));
            }


            listViewEditText.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) {
                    ArrayList<String> temp = new ArrayList<String>();
                    int length = listViewEditText.getText().length();
                    temp.clear();
                    for (int t = 0; t < title.size(); t++) {
                        if (length <= title.get(t).length()) {
                            if (listViewEditText.getText().toString().equalsIgnoreCase((String) title.get(t).substring(0, length))) {
                                temp.add(title.get(t));
                            }
                        }

                    }
                    songslist.setAdapter(new CustomListViewSongs(context, temp, artist, songsDuration));

                }

                @Override
                public void afterTextChanged(Editable s) {

                }
            });
            container.addView(new_view);
            Log.e("TAG", "" + position);
            return new_view;
        }
    }


    private void populate() {

        ArrayList<Long> albumId = new ArrayList<Long>();
        ArrayList artist = new ArrayList();
        ArrayList title = new ArrayList();
        ArrayList album = new ArrayList();
        ArrayList songs = new ArrayList();
        ArrayList songsPath = new ArrayList();
        ArrayList songsDuration = new ArrayList();


        String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";
        Cursor cursor;
        String[] projection = {
                MediaStore.Audio.Media._ID,
                MediaStore.Audio.Media.ARTIST,
                MediaStore.Audio.Media.TITLE,
                MediaStore.Audio.Media.DATA,
                MediaStore.Audio.Media.DISPLAY_NAME,
                MediaStore.Audio.Media.DURATION,
                MediaStore.Audio.Media.ALBUM,
                MediaStore.Audio.Media.ALBUM_ID,
                MediaStore.Audio.Media.TITLE
        };

        cursor = this.getContentResolver().query(
                MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
                projection,
                selection,
                null,
                null);
        while (cursor.moveToNext()) {
            artist.add(cursor.getString(1));
            songsPath.add(cursor.getString(3));
            songs.add(cursor.getString(4));
            songsDuration.add(cursor.getString(5));
            album.add(cursor.getString(6));
            albumId.add(cursor.getLong(7));
            title.add(cursor.getString(2));
            //songUri.add(cursor.getString(3));
        }
        cursor.close();
        this.title = title;
        this.albumId = albumId;
        this.albumName = album;
        this.songs = songs;
        //this.songs = title;
        this.artist = artist;
        this.songsPath = songsPath;
        this.songsDuration = songsDuration;
        //Collections.sort(this.songs, String.CASE_INSENSITIVE_ORDER);
        //Collections.sort(this.albumName, String.CASE_INSENSITIVE_ORDER);
    }

}

1 个答案:

答案 0 :(得分:0)

使用自动填充textView会更容易。