您好我正在尝试学习android工作室java编程,我正在制作简单的音频播放器一个列表视图和一个播放按钮,所以我需要帮助音频播放在另一个活动,如何设置onitemclicklistener播放音频在另一个活动请帮助我
public void doStuff() {
listView = (ListView) findViewById(R.id.listview);
arrayList = new ArrayList<>();
getVideo();
adapter= new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, arrayList);
listView.setAdapter(adapter);
}
//这里写的代码是什么,在另一个活动中播放音频只是简单的没有标题只显示播放按钮
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView adapterView, View view, int i,
long l) {
}
});
}
这是我的第二个活动,我想只添加一个按钮播放,我是新的所以请理解这个
public class Main2Activity extends AppCompatActivity {
MediaPlayer mp;
Button bt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
bt = (Button)findViewById(R.id.play);
Intent intent=getIntent();
int position = (int) intent.getLongExtra("position", 0);
if(position!=0){
//get your song from the position variable 'position' received here
//and you can start playing your song
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (mp.isPlaying()){
mp.pause();
}else {
mp.start();
}
答案 0 :(得分:0)
您可以在onItemClick
中向其他活动发送意图listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView adapterView, View view, int i,
long l) {
Intent intent = new Intent(getActivity(), YouOtherActivity.class);
intent.putExtra("position", i);
startActivity(intent);
}
});
并在你的其他活动的onCreate中获得该位置并播放该歌曲
protected void onCreate(Bundle savedInstanceState) {
.
.
.
Intent intent=getIntent();
int postion = intent.getLongExtra("position", 0);
if(position!=0){
//get your song from the position variable 'position' received here
//and you can start playing your song
}
}