我有三个选项卡式活动,每个活动都有自己的图像和声音数组。图像和声音将传递给CustomListAdapter并作为List返回:
TabActivity:
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;
}
CustomListAdapter:
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
}
}
}
如何使用customAdapter中的imageView为不同的标签播放声音?
答案 0 :(得分:0)
我将媒体播放器功能移动到适配器并进入onclick,然后将数组声音传递给适配器。