Reputation: 91
I stored datas from the database to an arrayList called nameList.
Within the function 'get_spinner_info', the values are successfully stored within the nameList.
However, there is no value for nameList outside of this function.
The error code is " java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 ".
I really need your help.
public class my_Item {
private Context context;
private FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance();
DatabaseReference datebaseReference = firebaseDatabase.getReference();
ArrayList<String> nameList = new ArrayList<String>();
// Get the value from the database and put them in the 'nameList'.
//In this code, I can successfully check the value within the 'nameList'.
public void get_spinner_info(String brand, String item, String prod_key){
datebaseReference.child(brand).child(item).child(prod_key).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot ds : dataSnapshot.child("myValue").getChildren()) {
String prod_name = ds.getValue().toString();
nameList.add(prod_name);
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
// But the nameList is empty in this part.
public void callFunction(final String brand, final String item, final String product_name, final String product_key, final int product_Num) {
get_spinner_info(brand, item, product_key);
Log.d("Spinner namelist : ", nameList.get(0));
}
}
Upvotes: 0
Views: 153
Reputation: 803
Synchronization problem cause this kind of error
if you already have data do this
create interface class
public interface DataCallback {
public void reciveData(ArrayList<String> nameList ,boolean isOk);
}
In your class my_Item call the interface like this
public void get_spinner_info(String brand,String item,String prod_key,DataCallback callback){
datebaseReference.child(brand).child(item).child(prod_key).addListenerForSingleValueEvent(new ValueEventListener(){
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot){
for(DataSnapshot ds:dataSnapshot.child("myValue").getChildren()){
String prod_name=ds.getValue().toString();
nameList.add(prod_name);
}
// here you should check if the result available or NOT to prevent null exceptions
if(nameList.size()>0){
callback.reciveData(nameList,true);// true and false used to check data if available or null
}
else{
callback.reciveData(nameList,false);
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError){
}
});
}
Now when you call get_spinner_info you need the callback Like this
get_spinner_info(brand, item, product_key,new DataCallback() {
@Override
public void callback(ArrayList<String> nameList, boolean isOk) {
if (isOk){
/// you have all data recived
Log.d("Spinner namelist : ", nameList.get(0));
}else {
// no data available
Log.i("TAG", "callback: No Data Available");
}
}
}););
Upvotes: 0
Reputation: 19223
get_spinner_info
method starts and ASYNCHRONOUS data loading (registers lsitener), thus onDataChange
will get called after Log.d("Spinner nameList : ", nameList.get(0));
your data will be available only after onDataChange
call, which may take some time. get_spinner_info
just starts loading your data, doesn't means that all data will be available just after method call ends
put this on the end of onDataChange
(after current for
loop) to check your items available/present in array
for (String name : nameList) Log.d("Spinner", "name: " + name);
Upvotes: 1