Abdoulaye Diallo Abdoulaye Diallo - 4 months ago 94
Android Question

How to refresh a viewFlipper when add new imageView and videoView programmatically?

I have a viewFlipper and i populate it with List and List. When I update image or video they do it well. Then when I add a new image or video they don't add it in viewFlipper automatically Also I have a timerTask to run function every x second
My function to init slide flipper is:

public void initSlide() {

try {

runOnUiThread(new Runnable() {
public void run() {

String[] typePub = new String[0];
publiciteList = null;
publiciteDAO = null;
publiciteDAO = new PubliciteDAO(mContext);
publiciteList = publiciteDAO.getAllPublicite();


try {


mediacontroller = new MediaController(
mContext);
if (publiciteList != null) {

int count = 0;

for (final Publicite pub : publiciteList) {


Log.d(TAG, "passage de pub =>" + pub.getIntituleFile() + "-" + publiciteList.size());

count += pub.getDureeAffichage();
URL url = null;


typePub = pub.getTypePub().split("/");
imagePath = Environment.getExternalStorageDirectory().toString() + "/" + ShowBoxApplication.getEvenement().getCodeEvenement() + "/" + pub.getIntituleFile();

File f = new File(imagePath);
if (f.exists()) {

if ("image".equalsIgnoreCase(typePub[0])) {

timeImage = pub.getDureeAffichage() * 1000;
bm = ViewUtil.decodeBitmapPath(imagePath, 100, 200);
imageView = new ImageView(mContext);
imageView.setImageBitmap(bm);
imageView.setTag(pub.getId());

mViewFlipper.addView(imageView);
mViewFlipper.setFlipInterval(timeImage);
mViewFlipper.setInAnimation(AnimationUtils.loadAnimation(mContext, R.anim.slide_in_from_right));
mViewFlipper.startFlipping();

} else if ("video".equalsIgnoreCase(typePub[0])) {


myVideoView = new VideoView(mContext);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.weight = 1.0f;
params.gravity = Gravity.CENTER;
myVideoView.setLayoutParams(params);

mediacontroller.setAnchorView(myVideoView);
myVideoView.setVideoPath(imagePath);
myVideoView.setTag(pub.getId() + "video");

mViewFlipper.addView(myVideoView);
timeVideo = pub.getDureeAffichage() * 1000;
mViewFlipper.setFlipInterval(timeVideo);


}

final String[] finalTypePub = typePub;
mViewFlipper.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
@Override
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
mViewFlipper.getDisplayedChild();

if (mViewFlipper.getCurrentView() != null && mViewFlipper.getCurrentView().getTag().equals(pub.getId() + "video")) {
final VideoView vidView = (VideoView) mViewFlipper.getCurrentView();
// Toast.makeText(MainActivity.this," new video "+timeVideo,Toast.LENGTH_LONG).show();

vidView.start();
mViewFlipper.stopFlipping();
if ("video".equalsIgnoreCase(finalTypePub[0])) {

timeVideo = pub.getDureeAffichage() * 1000;

// Toast.makeText(getApplicationContext()," video "+timeVideo,Toast.LENGTH_LONG).show();
}
countDownTimer = new CountDownTimer(timeVideo, 1000) {
@Override
public void onTick(long millisUntilFinished) {
compteur = millisUntilFinished / 1000;
// Toast.makeText(getApplicationContext()," compteur "+compteur,Toast.LENGTH_SHORT).show();
// textViewMessage.setText(" compteur "+compteur +" time video "+vidView.getDuration());
if (compteur == 1) {

vidView.stopPlayback();

}
}

@Override
public void onFinish() {

mViewFlipper.removeView(vidView);
mViewFlipper.addView(vidView);
mViewFlipper.stopFlipping();
mViewFlipper.setAutoStart(true);
mViewFlipper.setInAnimation(AnimationUtils.loadAnimation(mContext, R.anim.slide_in_from_right));
mViewFlipper.startFlipping();

}
}.start();
} else {
//TODO afficher les image ici
}
}
});


} else {
Toast.makeText(mContext, "Veuillez mettre à jour les pub svp...", Toast.LENGTH_LONG).show();
}
}


} else {
// Toast.makeText(getApplication(),"Erreur de recuperation",Toast.LENGTH_LONG).show();
}

} catch (RuntimeException e) {
Log.e(LOG, LOG + ".initVideoImageSlider RuntimeException :" + e != null ? e.getMessage() : "");
//Toast.makeText(MainActivity.this, "erreur innatendue : \n" + e != null && e.getMessage() != null && !"".equals(e.getMessage().trim()) ? e.getMessage() + "" : "", Toast.LENGTH_LONG).show();
} catch (Exception e) {
Log.e(LOG, LOG + ".initVideoImageSlider :\n" + e != null && e.getMessage() != null && !"".equals(e.getMessage().trim()) ? e.getMessage() + "" : "");
// Toast.makeText(MainActivity.this, "erreur : \n" + e != null && e.getMessage() != null && !"".equals(e.getMessage().trim()) ? e.getMessage() + "" : "", Toast.LENGTH_LONG).show();
} catch (Throwable e) {

Log.e(LOG, LOG + ".initVideoImageSlider Throwable EXCEPTION :" + e != null && e.getMessage() != null && !"".equals(e.getMessage().trim()) ? e.getMessage() + "" : "");
//Toast.makeText(MainActivity.this, "erreur innatendue : \n" + e != null && e.getMessage() != null && !"".equals(e.getMessage().trim()) ? e.getMessage() + "" : "", Toast.LENGTH_LONG).show();

}

}
});

} catch (Exception e) {
Toast.makeText(mContext, "erreur innatendue : \n" + e != null && e.getMessage() != null && !"".equals(e.getMessage().trim()) ? e.getMessage() + "" : "", Toast.LENGTH_LONG).show();
}


}`


Thanx for all response

Answer

I resolve it when i add this code on top of the function
when the timer is executed they refresh the ViewFlipper mViewFlipper.removeAllViews();