I've been searching for this error for three days and can not fix it. I'm putting together a photo gallery for my app and I need an Array with the photos coming from Firebase to show them in the gallery. This error occurs when clicking on the gallery, so I already researched the error happens when I put items in the array and their size changes, I would have to notify the Adapter using notifyDataSetChanged (), however I could not get a way to do this or I am notifying in Wrong place, I have used this method in several places and nothing ... Could anyone help me? I am adding the code
public class Galeria extends AppCompatActivity {
private GaleriaImagensAdapter adapter;
private List<String> valores;
private List<String> temporaria;
private ViewPager galeria;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_galeria);
getIntentAtual();
galeria = (ViewPager) findViewById(R.id.vp_galeria);
}
@Override
protected void onResume() {
super.onResume();
if(valores == null){
valores = new ArrayList<>();
}
valores = ConfiguracaoFirebase.galeriaFire();
if (adapter == null){
adapter = new GaleriaImagensAdapter(this, valores);
galeria.setAdapter(adapter);
}else {
adapter.notifyDataSetChanged();
}
}
private void getIntentAtual(){
if(getIntent().hasExtra("nome_sessao") && getIntent().hasExtra("sessao_bd")){
String tituloSessao = getIntent().getStringExtra("nome_sessao");
String nomeSessaoBD = getIntent().getStringExtra("sessao_bd");
}else{
Log.i("falha", "sem nada");
}
}
}
Here the gallery's Adapter
public class GaleriaImagensAdapter extends PagerAdapter {
private Context context;
private List<String> valores;
private boolean doNotifyDataSetChangedOnce = false;
private int[] imagens = new int[] { R.drawable.cadastrar_usuario_branco_extraido, R.drawable.cadastrar_usuario_branco, R.drawable.cadastrar_usuario};
public GaleriaImagensAdapter(Context contextm, List<String> valor){
this.valores = valor;
this.context = context;
}
@Override
public int getCount() {
if (doNotifyDataSetChangedOnce){
doNotifyDataSetChangedOnce = false;
notifyDataSetChanged();
return valores.size();
}
@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
return view.equals(object);
}
@Override
public void destroyItem(ViewGroup pager, int position, Object object){
((ViewPager) pager).removeView((ImageView) object);
}
@Override
public Object instantiateItem(ViewGroup pager, int position){
ImageView imagem = new ImageView(context);
imagem.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
Glide.with(context).load(valores.get(position)).into(imagem);
((ViewPager) pager).addView(imagem, 0);
return imagem;
}
}
Here is the code for the firebase:
public static List galeriaFire(){
final List<String> imagensGaleria = new ArrayList<>();
firebase = ConfiguracaoFirebase.getFirebase().child("sessoes").child("galeria").child("2018").child("agosto");
post = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot child : dataSnapshot.getChildren()){
imagensGaleria.add(child.getValue().toString());
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
};
firebase.addValueEventListener(post);
return imagensGaleria;
}
Below the error:
09-14 12:36:06.651 3166-3166/doeamor.acaosocial.com.doeamor E/AndroidRuntime: FATAL EXCEPTION: main
Process: doeamor.acaosocial.com.doeamor, PID: 3166
java.lang.IllegalStateException: The application's PagerAdapter changed the adapter's contents without calling PagerAdapter#notifyDataSetChanged! Expected adapter item count: 0, found: 3 Pager id: doeamor.acaosocial.com.doeamor:id/vp_galeria Pager class: class android.support.v4.view.ViewPager Problematic adapter: class doeamor.acaosocial.com.doeamor.Adapter.GaleriaImagensAdapter
at android.support.v4.view.ViewPager.populate(ViewPager.java:1137)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1086)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1616)
at android.view.View.measure(View.java:17547)
at android.support.constraint.ConstraintLayout.internalMeasureChildren(ConstraintLayout.java:1212)
at android.support.constraint.ConstraintLayout.onMeasure(ConstraintLayout.java:1552)
at android.view.View.measure(View.java:17547)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:141)
at android.view.View.measure(View.java:17547)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
at android.view.View.measure(View.java:17547)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
at android.view.View.measure(View.java:17547)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
at android.view.View.measure(View.java:17547)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2615)
at android.view.View.measure(View.java:17547)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2015)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1173)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1379)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1061)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5885)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
at android.view.Choreographer.doCallbacks(Choreographer.java:580)
at android.view.Choreographer.doFrame(Choreographer.java:550)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)