列表视图显示从另一个列表视图中选择值时的重复值

vis*_*tel 0 android listview audio-player android-music-player

我正在开发音乐播放器应用程序.根据我的项目,我有一个列表视图,显示歌曲列表和另一个列表视图,显示最近播放的歌曲.所以,当我从歌曲列表视图列表中选择一个特定的歌曲,显示在最近播放的歌曲listview.But当我从歌曲列表视图中选择相同的歌曲时,它也可以显示在最近的列表视图上,我使用自定义列表视图适配器.

所以,我想在最近的歌曲列表中只显示一首不重复选择的歌曲.

这是我最近的歌曲列表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
        }
    }
}
 }
Run Code Online (Sandbox Code Playgroud)

和最近的歌曲列表片段

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;
        }
    });
}
}
Run Code Online (Sandbox Code Playgroud)

所以,如果有人知道我怎么做,这告诉我.

Roh*_*hah 5

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.
Run Code Online (Sandbox Code Playgroud)