设置适配器时ListView不会更新

时间:2014-08-26 13:24:35

标签: android android-listview

我在一个片段中有一个名为lvSongs的ListView,它将被另一个片段中的一个动作更新。更新的方法称为精细,它填充ArrayList以便用作适配器的数据处理程序。我创建了一个新的ArrayAdapter实例,这就出现了问题:当我将它设置为ListView时,它不会更新。我正在使用AndroidAnnotations。代码如下:

fragment_song_view.xml

...
<ListView
        android:id="@+id/lvSongs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/btSyncSongs"
        android:layout_alignParentBottom="true"
        android:layout_below="@+id/btSyncSongs" >
    </ListView>
...

SongViewFragment.java

    package fragment;

    import java.io.File;
    import java.util.ArrayList;
    import java.util.List;

    import org.androidannotations.annotations.AfterViews;
    import org.androidannotations.annotations.Click;
    import org.androidannotations.annotations.EFragment;
    import org.androidannotations.annotations.InstanceState;
    import org.androidannotations.annotations.ViewById;

    import android.content.Context;
    import android.content.DialogInterface;
    import android.content.DialogInterface.OnClickListener;
    import android.content.Intent;
    import android.media.MediaPlayer;
    import android.net.Uri;
    import android.support.v4.app.Fragment;
    import android.util.Log;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.AdapterView.OnItemClickListener;
    import android.widget.AdapterView.OnItemLongClickListener;
    import android.widget.ArrayAdapter;
    import android.widget.ListView;
    import android.widget.Toast;
    import myproject.MainActivity;
    import myproject.R;
    import myproject.connector.JoinTagSongConnector;
    import myproject.connector.SongConnector;
    import myproject.connector.TagConnector;
    import myproject.model.JoinTagSong;
    import myproject.model.Song;
    import myproject.model.Tag;
    import myproject.util.ChooseFoldersActivity_;
    import myproject.util.UtilDialog;

    @EFragment(R.layout.fragment_song_view)
        private SongConnector songConn;

        private Song curSong;

        private String[] folderPathsToSync;

        private List<Song> songs;

        private ArrayAdapter<Song> adapter;

        private Tag defaultTag = null;

        private TagConnector tagConn;

        private JoinTagSongConnector jtsConn;

        @ViewById
        protected ListView lvSongs;

        @Click
        protected void btPreviousSong() {
    //unimportant
        }

        @Click
        protected void btPlayPause() {
    //unimportant
        }

        private void toastShow(String text, int duration) {
    //unimportant; shows a toast with defined message and duration
        }

        @Click
        protected void btNextSong() {
    //unimportant
        }

        protected MediaPlayer mp;

        @InstanceState
        String[] values;

    //this is called after onCreate, which is implemented automatically by AndroidAnnotations
        @AfterViews
        public void init() {
            if (jtsConn == null)
                jtsConn = new JoinTagSongConnector(getActivity());
            if (tagConn == null)
                tagConn = new TagConnector(getActivity());
            folderPathsToSync = new Intent().getStringArrayExtra("folderPathsToSync");
            if (songConn == null)
                songConn = new SongConnector(getActivity());
            if (values == null) {
                loadListView();
            } else {
                ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), R.layout.text_view_layout,
                        R.id.textView, values);
                lvSongs.setAdapter(adapter);
            }
            setLvSongsAttrs();
        }

        @Click
        public void btSelectFolders() {
    //unimportant
        }

        public void updateListView(List<Tag> tags) {
            List<Song> aux = songConn.findAll();
            if (tags.size() != 0) {
                List<Song> filteredSongs = new ArrayList<Song>();
                for (Song song : aux) {
                    if (song.containsTags(tags)) {
                        if (!filteredSongs.contains(song))
                            filteredSongs.add(song);
                    }
                }
                adapter = new ArrayAdapter<Song>(getActivity(), R.layout.text_view_layout, R.id.textView,
                        filteredSongs);
            } else {
                adapter = new ArrayAdapter<Song>(getActivity(), R.layout.text_view_layout, R.id.textView, aux);
            }
            lvSongs.setAdapter(adapter);
            setLvSongsAttrs();
        }

        @Click
        public void btSyncSongs() {
    //unimportant   
    }

        private class PositiveClickListener implements OnClickListener {
            //unimportant
        }

        private void loadListView() {
            this.songs = this.songConn.findAll();
            this.adapter = new ArrayAdapter<Song>(getActivity(), R.layout.text_view_layout, R.id.textView, this.songs);
            this.lvSongs.setAdapter(this.adapter);
            setLvSongsAttrs();
        }

        private void setLvSongsAttrs() {
            lvSongs.setOnItemLongClickListener(new SongLongClickListener());
            lvSongs.setOnItemClickListener(new ItemClickListener());
        }

        private class ItemClickListener implements OnItemClickListener {
    //unimportant
        }

        private void stopAndPlay(Song song) {
    //      unimportant
        }

        private void loadListView(Song song) {
//unimportant       }

        public class SongLongClickListener implements OnItemLongClickListener {
    //unimportant
        }

        public ArrayList<File> synchronize(File file) {
    //unimportant
        }
    }

[编辑1] =============================== 我将实现改为@ michal.z所说的,但仍未更新......代码如下:

@AfterViews
    public void init() {
        if (jtsConn == null)
            jtsConn = new JoinTagSongConnector(getActivity());
        if (tagConn == null)
            tagConn = new TagConnector(getActivity());
        folderPathsToSync = new Intent().getStringArrayExtra("folderPathsToSync");
        if (songConn == null)
            songConn = new SongConnector(getActivity());
        loadListView();
    }


    private void loadListView() {
        this.songs = this.songConn.findAll();
        this.adapter = new ArrayAdapter<Song>(getActivity(), R.layout.text_view_layout, R.id.textView,
                this.songs);
        this.lvSongs.setAdapter(this.adapter);
        lvSongs.setOnItemLongClickListener(new SongLongClickListener());
        lvSongs.setOnItemClickListener(new ItemClickListener());
    }
public void updateListView(List<Tag> tags) {
        adapter.clear();
        List<Song> aux = songConn.findAll();
        if (tags.size() != 0) {
            List<Song> filteredSongs = new ArrayList<Song>();
            for (Song song : aux) {
                if (song.containsTags(tags)) {
                    if (!filteredSongs.contains(song)) {
                        filteredSongs.add(song);
                        adapter.add(song);
                    }
                }
            }
        } else {
            for (Song song : aux) {
                adapter.add(song);
            }
        }
        adapter.notifyDataSetChanged();
    }

[编辑2] =============================

@AfterViews
public void init() {
        if (jtsConn == null)
            jtsConn = new JoinTagSongConnector(getActivity());
        if (tagConn == null)
            tagConn = new TagConnector(getActivity());
        folderPathsToSync = new Intent().getStringArrayExtra("folderPathsToSync");
        if (songConn == null)
            songConn = new SongConnector(getActivity());
        loadListView();
    }

    private void loadListView() {
        this.songs = this.songConn.findAll();
        this.adapter = new ArrayAdapter<Song>(getActivity(), R.layout.text_view_layout, R.id.textView,
                this.songs);
        this.lvSongs.setAdapter(this.adapter);
        lvSongs.setOnItemLongClickListener(new SongLongClickListener());
        lvSongs.setOnItemClickListener(new ItemClickListener());
    }

    public void updateListView(List<Tag> tags) {
        songs.clear();
        List<Song> aux = songConn.findAll();
        if (tags.size() != 0) {
            List<Song> filteredSongs = new ArrayList<Song>();
            for (Song song : aux) {
                if (song.containsTags(tags)) {
                    if (!filteredSongs.contains(song)) {
                        filteredSongs.add(song);
                        songs.add(song);
                    }
                }
            }
        } else {
            songs.addAll(aux);
        }
        adapter.notifyDataSetChanged();
    }

2 个答案:

答案 0 :(得分:0)

您应该只使用1个适配器。然后你的Adapter和Fragment应该共享对同一个ArrayList的引用。在更新时,只需在该ArrayList中设置新值,并记得在Adapter上调用notifydatasetchanged

答案 1 :(得分:0)

首先,你必须全局声明Listview对象,每个类都可以访问它。然后初始化它并在活动类中设置适配器。在此之后,当listview将被更改时,在已全局声明的对象上调用notifydatasetchanged方法。请注意,不要重新创建对象。全局使用它。

希望它有所帮助。