当来自另一个列表视图的 select 值时,列表视图显示重复值

Listview show repeated value when select value from another listview

我正在为我的项目开发音乐播放器 app.According 我有一个显示歌曲列表的列表视图和另一个显示最近播放的列表视图 songs.So,当我 select 一个特定的来自歌曲列表视图的歌曲显示在最近播放的歌曲 listview.But 中,当我 select 来自歌曲列表视图的同一首歌曲时,它也可以显示在最近的列表视图中,并且我正在使用自定义列表视图适配器。

所以,我想只显示一首不重复select最近歌曲列表中的歌曲。

这是我最近的歌单Screenshote

这是歌曲列表片段

public class Playlists extends ListFragment {


EditText edtSearch;
SongAdapter songAdapter;
SongsManager songsManager = new SongsManager();
private static final int ALERT_DIALOG = 1;

// Songs list
public ArrayList<SongModel> songsList = new ArrayList<>();

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View windows = inflater.inflate(R.layout.playlist, container, false);
    return windows;
}

@Override
public void onViewCreated(View v, Bundle savedInstanceState) {
    super.onViewCreated(v, savedInstanceState);

    getListView().setFastScrollEnabled(true);

    //ListView animation
    LayoutAnimationController controller
            = AnimationUtils.loadLayoutAnimation(
            getActivity(), R.anim.list_layout_controller);
    getListView().setLayoutAnimation(controller);

    edtSearch = (EditText)getView().findViewById(R.id.search);
    final ArrayList<SongModel> songsListData = songsManager.songList;
    Log.i("songsListData...",""+songsListData.size());
    SongsManager plm = new SongsManager();

    // get all songs from sdcard
    this.songsList = plm.getPlayList();

    // looping through playlist
    for (int i = 0; i < songsListData.size(); i++) {
        SongModel song = songsListData.get(i);
        songsListData.add(song);
    }

    //Songlist custom array adapter
    songAdapter = new SongAdapter(getActivity(),songsList);
    setListAdapter(songAdapter);

    // selecting single ListView item
    ListView lv = getListView();

    // listening to single listitem click
    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View arg0,
                                int position, long id) {

            Log.i("Index", "..." + position);
            songAdapter.setSelectedIndex(position);
            Intent i = new Intent(getActivity().getApplicationContext(),Main.class);
            i.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
            SongModel songModel = (SongModel) songAdapter.getItem(position);
            int indexOfSong = songAdapter.songsList.indexOf(songModel);

            // Sending songIndex to PlayerActivity
            i.putExtra("songIndex", indexOfSong);
            getActivity().setResult(100,i);
            startActivityForResult(i, 100);
            Player.mp.stop();

            Constant.status = 0;

            //putting song in recentSongList arraylist
            SongModel model = (SongModel) songAdapter.getItem(position);
            model.setSongTitle(songModel.getSongTitle());
            model.setSongPath(songModel.getSongPath());
            Constant.recentSongList.add(model);
            Log.i("recentSongList...", "..." + Constant.recentSongList.size());
            getActivity().finish();
        }
    });
    lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {

            removeItemFromList(position);
            return true;
        }

        private void removeItemFromList(int position) {

            final int deletePosition = position;

            Dialog dialog = null;
            ContextThemeWrapper ctw = new ContextThemeWrapper(getActivity(), R.style.MyTheme );
            CustomBuilder builder = new CustomBuilder( ctw );

            builder.setTitle("Delete");
            builder.setMessage("Do you want delete this song?");
            builder.setPositiveButton("YES", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TOD O Auto-generated method stub
                    songsList.remove(deletePosition);
                    songAdapter.notifyDataSetChanged();
                    songAdapter.notifyDataSetInvalidated();

                }
            });
            builder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub
                    dialog.dismiss();
                }
            });

            builder.show();
        }
    });
    edtSearch.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence cs, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            String text = edtSearch.getText().toString().toLowerCase(Locale.getDefault());
            songAdapter.filter(text);
        }
    });
}
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
    super.setUserVisibleHint(isVisibleToUser);

    if (this.isVisible()) {
        // If we are becoming invisible, then...
        Log.d("setUserVisibleHint()...", "PlayList...Visible");
        if (!isVisibleToUser) {
            Log.d("setUserVisibleHint()...", "PlayList...notVisible");
            // TODO stop audio playback
        }
    }
}
 }

以及最近的歌单片段

public class RecentSongList extends ListFragment{


ResentSongListAdapter adapter;
public ArrayList<SongModel> recentSongList = Constant.recentSongList;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View windows = inflater.inflate(R.layout.recent_playlist, container, false);
    Log.i("recent Song List...", "..." + recentSongList.size());
    return windows;
}
@Override
public void onViewCreated(View v, Bundle savedInstanceState) {

    //ListView animation
    LayoutAnimationController controller = AnimationUtils.loadLayoutAnimation(getActivity(), R.anim.list_layout_controller);
    getListView().setLayoutAnimation(controller);

    adapter = new ResentSongListAdapter(getActivity(), recentSongList);
    setListAdapter(adapter);

    ListView lv = getListView();

    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            Log.i("Index", "..." + position);
            Intent i = new Intent(getActivity().getApplicationContext(),Main.class);
            i.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
            SongModel songModel = (SongModel) adapter.getItem(position);
            int indexOfSong = recentSongList.indexOf(songModel);
            // Sending songIndex to PlayerActivity
            i.putExtra("songIndex", indexOfSong);
            getActivity().setResult(100, i);
            startActivityForResult(i, 100);
            Player.mp.stop();
            getActivity().finish();
            Constant.status = 1;
        }
    });
}
}

所以,如果有人知道我该怎么做,请告诉我。

In Song Fragment ,
When you add Model to RecentSongList in it 1st check songModel.getSongTitle Already exist in the recent song list .
If it exists dont add it again.
Else add it to the adapter.
boolean flag =false;
for(int i=0;i<recentSongList.size();i++){
     // here check whether the songTitle already exists in the list or not
if it does, ignore and break else continue and add to the list

if(recentSongList.get(i).getSongTitle.equals(model.getSongTitle())){
//don't add 
flag = true;
break;
}
}

if(!flag){
Constant.recentSongList.add(model);
flag = false;
}

Hope this might help you.