我想制作一个电子学习的Android应用程序。 我将字符串数据从数据库设置为gridview项。但是我想设置onitemclick来为每个项目的原始文件夹播放音频,其中包含大约40个数据。 怎么设置?
这是我的代码:
public class DaftarHuruf extends Fragment {
private static final String ARG_SECTION_NUMBER = "section_number";
private GridView gv;
Cursor cursor;
DBHelper dbHelper;
ListAdapter list_adapter;
String audio;
MediaPlayer mp;
public DaftarHuruf() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater layoutInflater,
ViewGroup viewGroup, Bundle bundle) {
View view = layoutInflater.inflate(R.layout.fragment_daftarhuruf,
viewGroup, false);
gv = (GridView) view.findViewById(R.id.gridview);
dbHelper = new DBHelper(getActivity());
dbHelper.open();
cursor = dbHelper.getDataHuruf();
cursor.moveToFirst();
SimpleCursorAdapter list_adapter = new SimpleCursorAdapter(
getActivity(), R.layout.grid_item, cursor,
new String[] { "huruf", "romanisasi" }, new int[] {
R.id.hurufHangeul, R.id.romawi });
gv.setAdapter(list_adapter);
gv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
audio = getString(R.id.hurufHangeul);
audio.replaceAll(".MP3", "");
int i;
i = getResources().getIdentifier(audio, "raw", getActivity().getPackageName());
mp = MediaPlayer.create(getActivity(), i);
if (mp != null) {
mp.start();
} else {
Toast.makeText(getActivity(), "Data suara tidak ada",
Toast.LENGTH_SHORT).show();
}
}
});
return view;
}
public static DaftarHuruf newInstance(int i) {
DaftarHuruf frag = new DaftarHuruf();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, i);
frag.setArguments(args);
return frag;
}
}
答案 0 :(得分:0)
替换
audio = getString(R.id.hurufHangeul);
audio.replaceAll(".MP3", "");
int i;
i = getResources().getIdentifier(audio, "raw", getActivity().getPackageName());
mp = MediaPlayer.create(getActivity(), i);
使用:
mp = MediaPlayer.create(this, R.raw.*name of the audio file*);
将媒体播放器设置为指定的声音。