我正在制作一个音乐播放器,我放置了一个随机播放Button
。
case R.id.ib3:
// long s1=System.nanoTime();
setSuffle(); // its is a function just ignore this
Collections.shuffle(index);
newlist(); // its is a function just ignore this
listView.setAdapter(new Song_Adapter(this,al_new));
我的歌曲已添加到ArrayList
中,我将List
传递给我的自定义适配器,并将其设置为ListView
。
我想用索引来实现列表的随机播放,这样当我点击混洗列表时,应该根据新索引播放歌曲。
来自评论的编辑:
public void onItemClick(AdapterView<?> parent, View v, int l, long m) {
Bundle bundle = new Bundle();
bundle.putInt("id",y);
Intent i=new Intent(this,Second.class); i.putExtras(bundle);
//startActivity(i);
// globle.setSong_id(l);
startService(i); }
答案 0 :(得分:0)
您不必为Adapter
设置新的ListView
。保留对适配器的引用并在其上调用notifyDataSetChanged();
。
//above onCreate, declare:
private MyCustomAdapter adapter;
//set the adapter ONCE, the first time you need it:
adapter = new Song_Adapter(this,al_new);
listView.setAdapter(adapter);
//call this everytime your dataset has changed:
adapter.notifyDataSetChanged();
随机播放一首歌:
int randomIndex = new Random().nextInt(mySongList.size());
答案 1 :(得分:0)
首先需要对ArrayList进行随机播放,然后使用 notifyDataSetChanged();
private Song_Adapter songAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
case R.id.ib3:
shuffleMySongs();
break;
}
private void shuffleMySongs(){
//to shuffle your arraylist
Collections.shuffle(yourArrayList);
songAdapter = new Song_Adapter(this, yourArrayList);
songAdapter.notifyDataSetChanged();
listView.setAdapter(songAdapter);
}