如何将数组用于媒体播放器使用自定义列表适配器用于不同的选项卡



我有三个选项卡式活动,每个都有自己的图像和声音数组。图像和声音被传递到 CustomListAdapter 并作为列表返回:

选项卡活动:

public  class Tab1Activity extends Fragment {
MediaPlayer mp = new MediaPlayer();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.tab1, container, false);
String[] sound = {"path/to/media/file.mp3wl.gif",
"path/to/media/file.mp3",
"",
"path/to/media/file.mp3",
"path/to/media/file.mp3",
"path/to/media/file.mp3",
"path/to/media/file.mp3",
"path/to/media/file.mp3",
"path/to/media/file.mp3",
"path/to/media/file.mp3",
"path/to/media/file.mp3",
"path/to/media/file.mp3",
"path/to/media/file.mp3",
"path/to/media/file.mp3"
};

String[] images = {"ImagePathwl.gif",
"ImagePath/image.jpg",
"",
"ImagePath/image.jpg",
"ImagePath/image.jpg",
"ImagePath/image.jpg",
"ImagePath/image.jpg",
"ImagePath/image.jpg",
"ImagePath/image.jpg",
"ImagePath/image.jpg",
"ImagePath/image.jpg",
"ImagePath/image.jpg",
"ImagePath/image.jpg",
"ImagePath/image.jpg"
};
CustomListAdapter adapter=new CustomListAdapter(this.getActivity(), sound, images);
list=(ListView) rootView.findViewById(R.id.list_view);
list.setAdapter(adapter);

list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
final int position, long id) {
// TODO Auto-generated method stub

try {
Thread t = new Thread(new Runnable() {
@Override
public void run() {
try {
mp.setDataSource(sound[+position]);
mp.prepare();
mp.start();
} catch (Exception e) {
e.printStackTrace();
Log.e("GGGGGG", "prepare() failed");
}
}
});
t.start();
} catch (Exception e) {
e.printStackTrace();
}
}
});
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
mp.reset();
}
});

return rootView;
}

自定义列表适配器:

public class CustomListAdapter extends ArrayAdapter<String> {
private final String[] sound;
private final String[] images;
public CustomListAdapter(Activity context, String[] sound, images) {
super(context, R.layout.animal_list, itemname);
// TODO Auto-generated constructor stub
this.context=context;
this.sound=sound;
this.image=image;
}
public View getView(int position,View view,ViewGroup parent) {
LayoutInflater inflater=context.getLayoutInflater();
View rowView=inflater.inflate(R.layout.animal_list, null,false);
imageView = (ImageView) rowView.findViewById(R.id.icon);
if (!imgid[position].isEmpty()) {
Picasso.with(context) // Context
.load(imgid[position]) // URL or file
.into(imageView); // An ImageView object to show the loaded image
}
}
}

如何用图像播放声音在自定义适配器中为不同的选项卡查看?

我将媒体播放器功能移动到适配器并在那里单击并将数组声音传递给适配器。

最新更新