user1397859 user1397859 - 3 years ago 145
Android Question

Android: Custom Gridview data are not binding at first time

I make a gridview to display icons list(image & text) by reading data from Firebase DB, at the first time calling that activity the gridview display blank. Only after I change the orientation then data are loaded.

Please advise how can I make it display at first time.(this is my first time developing Android app and never code Java before)
Thanks

Below is my code:

package com.abc.expensestracker;



import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;

import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

import java.util.ArrayList;

public class NewCategoryIconListActivity extends AppCompatActivity {

private GridView mIconListGridView;
private IconsListGridViewAdapter mIconListGridViewAdapter;
public ArrayList<IconsImageGridItemData> mIconsImageArrData = null;

private DatabaseReference mIconImagesRef;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_category_icon_list);


mIconListGridView = (GridView) findViewById(R.id.icons_list_gridview);
mIconImagesRef = FirebaseDatabase.getInstance().getReference().child("Iconimages");


mIconsImageArrData = new ArrayList<IconsImageGridItemData>();
setListData();

mIconListGridViewAdapter = new IconsListGridViewAdapter(this,R.layout.icons_single_item_layout,mIconsImageArrData);//(this, R.layout.icons_single_item_layout, mGridData);
mIconListGridView.setAdapter(mIconListGridViewAdapter);


mIconListGridViewAdapter.setGridData(mIconsImageArrData);



mIconListGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

//Get item at position
IconsImageGridItemData item = (IconsImageGridItemData) parent.getItemAtPosition(position);
String selectedIconURL = item.getIconURL();

//send back the selected icon to parent activity
Intent returnIntent = getIntent();
returnIntent.putExtra("icon_URL",selectedIconURL);
setResult(Activity.RESULT_OK,returnIntent);
finish();
}
});
}


/****** Function to set data in ArrayList *************/
public void setListData()
{

mIconImagesRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {

for(DataSnapshot categorySnapshot: dataSnapshot.getChildren()){
String iconURL = categorySnapshot.child("iconURL").getValue(String.class);

final IconsImageGridItemData iconsImageData = new IconsImageGridItemData();
/******* Firstly take data in model object ******/
iconsImageData.setIconURL(iconURL);



/******** Take Model Object in ArrayList **********/
mIconsImageArrData.add(iconsImageData);
}
}

@Override
public void onCancelled(DatabaseError databaseError) {

}
});
}

}


Answer Source

Form your above code what I can understand is, task inside setListData(); are completing after the execution of

mIconListGridViewAdapter.setGridData(mIconsImageArrData);

One simple change should work. in side your setListData() function after finishing of for inside eventlistener's onDataChange() function

 /******** Take Model Object in ArrayList **********/
                                  mIconsImageArrData.add(iconsImageData);
                              }// For finish 
                          mIconListGridViewAdapter.setGridData(mIconsImageArrData);
                     }// end of onDataChange 

this will make sure your adapter is getting data just after finishing data copying in mIconsImageArrData

Note:- I am assuming that you are certainly calling notifyDataSetChanged(); in setGridData function copying data .

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download