我有这个适配器,我正在尝试在媒体播放器准备时显示进度对话框。但由于某种原因,我的对话框不会显示。谁能告诉我我错在哪里?我也没有得到任何 logCat 错误输出。
这是我的适配器:
public class AlbumDetailsAdapter extends BaseAdapter {
private Activity activity;
private static ArrayList<Songs> data;
private static LayoutInflater inflater = null;
private MediaPlayer mediaPlayer;
private ImageView playSong;
private Albums album;
private ImageLoader loader;
private int oldPosition = -1;
private DatabaseHelper helper;
private ImageView downloadSong;
private boolean exists;
Handler handler = new Handler();
public AlbumDetailsAdapter(Activity a, ArrayList<Songs> products,
Albums album) {
activity = a;
this.album = album;
loader = new ImageLoader(activity);
mediaPlayer = new MediaPlayer();
data = products;
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return data.size();
}
public Object getItem(int position) {
return position;
}
public void stopMusic() {
try {
mediaPlayer.stop();
} catch (Exception e) {
}
}
public long getItemId(int position) {
return position;
}
public View getView(final int position, View convertView,
final ViewGroup parent) {
View vi = convertView;
if (convertView == null)
vi = inflater.inflate(R.layout.songs_items, parent, false);
ImageView songthums = (ImageView) vi.findViewById(R.id.song_item_thumb);
TextView singer = (TextView) vi
.findViewById(R.id.songs_item_album_singer);
TextView songNumber = (TextView) vi
.findViewById(R.id.songs_item_number);
TextView albumTitle = (TextView) vi
.findViewById(R.id.songs_item_album_title);
TextView feat = (TextView) vi.findViewById(R.id.songs_item_feat);
feat.setText(data.get(position).getFeat());
if (data.get(position).getFeat().length() <= 1) {
feat.setVisibility(View.GONE);
}
singer.setText(data.get(position).getSONG_ARTIST());
loader.DisplayImage(data.get(position).getSONG_THUMB(), songthums);
helper = new DatabaseHelper(activity);
helper.openDB();
playSong = (ImageView) vi.findViewById(R.id.songs_item_play);
downloadSong = (ImageView) vi.findViewById(R.id.songs_item_download);
if (helper.getDownloadedSong(album.getTAG_ID(), data.get(position)
.getSONG_TITLE())) {
Log.v("--", "Y");
downloadSong.setBackgroundResource(R.drawable.down_no);
} else
downloadSong.setBackgroundResource(R.drawable.down_yes);
helper.closeDB();
if (data.get(position).getSONG_MP3().length() > 0) {
if (!data.get(position).getIsPlaying())
playSong.setBackgroundResource(R.drawable.play_nr);
else
playSong.setBackgroundResource(R.drawable.stop);
} else
playSong.setBackgroundResource(R.drawable.play_pr);
downloadSong.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
if (!helper.isDBOpen()) {
helper.openDB();
}
if (helper.isSongShared(data.get(position).getSONG_TITLE()
.toString().replace("'", ""), album.getTAG_ID())) {
if (helper.getDownloadedSong(album.getTAG_ID(),
data.get(position).getSONG_TITLE())) {
Toast.makeText(activity,
R.string.song_already_downloaded,
Toast.LENGTH_SHORT).show();
}
} else {
shareAlbum(data.get(position).getSONG_TITLE(),
album.getTAG_ID(), position);
}
helper.closeDB();
}
});
playSong.setOnClickListener(new OnClickListener() {
// final ProgressDialog bufferingDialog = new
// ProgressDialog(activity);
final ProgressDialog bufferingDialog = new ProgressDialog(
activity);
@Override
public void onClick(View v) {
if (mediaPlayer != null && mediaPlayer.isPlaying()) {
try {
mediaPlayer.stop();
((AlbumDetails) activity).changePicToPlay(position);
if (oldPosition != position) {
bufferingDialog.setMessage(activity
.getString(R.string.buffering));
bufferingDialog.show();
((AlbumDetails) activity)
.changePicToPlay(oldPosition);
mediaPlayer = new MediaPlayer();
try {
mediaPlayer.setDataSource(data.get(position)
.getSONG_MP3());
mediaPlayer.prepare();
mediaPlayer.start();
postData(1, position);
// on complete
mediaPlayer
.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(
MediaPlayer mp) {
mediaPlayer.stop();
((AlbumDetails) activity)
.changePicToPlay(position);
}
});
bufferingDialog.dismiss();
} catch (IOException e) {
Log.v("--", "padna menuvam vo stop");
((AlbumDetails) activity)
.changePicToPlay(position);
mediaPlayer.stop();
bufferingDialog.dismiss();
Toast.makeText(activity,
"Song not existing",
Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
/*
* if (bufferingDialog.isShowing())
* bufferingDialog.dismiss();
*/
// mediaPlayer.release();
Log.v("--", "played before1");
} catch (Exception e) {
}
} else {
if (data.get(position).getIsPlaying()) {
Log.v("--", "played before0");
mediaPlayer.stop();
Log.v("--", "sviri smeni vo stop");
((AlbumDetails) activity)
.changePicToPlay(position);
if (bufferingDialog.isShowing())
bufferingDialog.dismiss();
} else {
Log.v("--", "not played before");
bufferingDialog.setMessage(activity
.getString(R.string.buffering));
bufferingDialog.show();
mediaPlayer = new MediaPlayer();
try {
mediaPlayer.setDataSource(data.get(position)
.getSONG_MP3());
((AlbumDetails) activity).changePicToPlay(position);
mediaPlayer.prepare();
mediaPlayer.start();
postData(1, position);
// on complete
mediaPlayer
.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
mediaPlayer.stop();
((AlbumDetails) activity)
.changePicToPlay(position);
}
});
bufferingDialog.dismiss();
} catch (IOException e) {
Log.v("--", "padna menuvam vo stop");
((AlbumDetails) activity)
.changePicToPlay(position);
mediaPlayer.stop();
Toast.makeText(activity,
"Song not existing",
Toast.LENGTH_SHORT).show();
bufferingDialog.dismiss();
e.printStackTrace();
}
}
}
oldPosition = position;
}
});
ImageView youtube = (ImageView) vi.findViewById(R.id.songs_item_video);
if (data.get(position).getSONG_MP4().length() > 0) {
youtube.setBackgroundResource(R.drawable.video_nr);
youtube.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
postData(2, position);
Intent intent;
if (!data.get(position).getSONG_MP4().startsWith("http://")) {
intent = new Intent(Intent.ACTION_VIEW, Uri
.parse("http://"
+ data.get(position).getSONG_MP4()));
} else {
intent = new Intent(Intent.ACTION_VIEW, Uri.parse(data
.get(position).getSONG_MP4()));
}
v.getContext().startActivity(intent);
}
});
}
albumTitle.setText(Html.fromHtml(data.get(position).getSONG_TITLE()));
singer.setText(Html.fromHtml(data.get(position).getSONG_ARTIST()));
// songNumber.setText(position + 1 + "");
songNumber.setVisibility(View.GONE);
helper.closeDB();
return vi;
}
private void shareAlbum(final String songId, final String albumID,
final int position) {
DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case DialogInterface.BUTTON_POSITIVE:
Intent i = new Intent(activity.getApplicationContext(),
ShareOnFacebook.class);
i.putExtra("songs", data.get(position));
i.putExtra("album", album);
i.putExtra("position", position);
activity.startActivity(i);
if (!helper.isDBOpen())
helper.openDB();
if (helper == null) {
helper = new DatabaseHelper(activity);
helper.openDB();
}
if (mediaPlayer.isPlaying()) {
((AlbumDetails) activity).changePicToPlay(position);
}
helper.addSharedSong(songId, albumID);
helper.closeDB();
break;
case DialogInterface.BUTTON_NEGATIVE:
dialog.dismiss();
break;
}
}
};
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
builder.setMessage(
activity.getString(R.string.do_you_want_to_share_this_song_on_facebook_))
.setPositiveButton(activity.getString(R.string.yes),
dialogClickListener)
.setNegativeButton(activity.getString(R.string.no),
dialogClickListener).show();
}
public void exists(boolean b) {
exists = b;
}
public void toastMsg() {
Toast.makeText(activity,
activity.getString(R.string.song_not_available),
Toast.LENGTH_SHORT).show();
}
public boolean getExists() {
return exists;
}
public boolean isSongAvailable(final int position) {
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
try {
HttpURLConnection.setFollowRedirects(false);
HttpURLConnection con = (HttpURLConnection) new URL(data
.get(position).getSONG_MP3()).openConnection();
con.setRequestMethod("HEAD");
exists(false);
} catch (Exception e) {
e.printStackTrace();
exists(true);
}
return null;
}
protected void onPostExecute(Void result) {
if (getExists() == false) {
toastMsg();
}
};
}.execute();
return getExists();
}
public static void postData(final int choice, final int position) {
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(
"http://nelu.burduja.com/musica/counting_song.php");
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
2);
switch (choice) {
case 1:
nameValuePairs.add(new BasicNameValuePair(
"song_streaming", "1"));
nameValuePairs.add(new BasicNameValuePair("song_id",
data.get(position).getSONG_ID()));
break;
case 2:
nameValuePairs.add(new BasicNameValuePair("song_video",
"1"));
nameValuePairs.add(new BasicNameValuePair("song_id",
data.get(position).getSONG_ID()));
break;
case 3:
nameValuePairs.add(new BasicNameValuePair(
"song_download", "1"));
nameValuePairs.add(new BasicNameValuePair("song_id",
data.get(position).getSONG_ID()));
break;
}
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
@SuppressWarnings("unused")
HttpResponse response = httpclient.execute(httppost);
} catch (ClientProtocolException e) {
} catch (IOException e) {
}
return null;
}
}.execute();
}
}