Mohammed Riyadh
Mohammed Riyadh

Reputation: 1019

How to structure my Object to retrieve nested FireBase data

i want my app (IOS & Android) to display some news feeds, and this news feed could be including text & 1 image (to be display in image view) or text and multi images a not specified NO. (to be display in gridview) according to object type property.

i have tried to implement this function like below but it didn't work and i hope that some one goes through this issue i had check a lot of questions but i think non is matching my case.

her is my firebase data structure and my object for android :

public class object {

    String text;
    String type;
    List<images> images;
    public object(){

    }
    public String gettext(){return text;}
    public  String gettype(){return type;}
    public List<images> getimages(){return images;}
}

class images{

    String images;
    public images(){

    }
public String getimages(){return images;}

}

and retrieving activity :

public class MainActivity extends AppCompatActivity {
    DatabaseReference database;
    List<object> objectsList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        database = FirebaseDatabase.getInstance().getReference();
        loaddata();
    }

    private void loaddata(){
        Query query = database.child("news");
        query.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                if (objectsList!=null){
                    objectsList.clear();
                }
                for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()){
                    object object = dataSnapshot1.getValue(object.class);
                    Log.i("&&&&&&& Images &&&&&",object.gettext());
                    objectsList.add(object);

                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
                Log.e("%%%%%%%% ERROR ",databaseError.getMessage());
            }
        });


  }
}

any suggestions would be much appreciated

enter image description here

Upvotes: 0

Views: 370

Answers (1)

John O&#39;Reilly
John O&#39;Reilly

Reputation: 10330

Try changing images as shown below.

public class object { 

    String text;
    String type;
    Map<String, String> images;
    public object(){ 

    } 
    public String gettext(){return text;}
    public  String gettype(){return type;}
    public Map<String,String> getimages(){return images;}
} 

Upvotes: 1

Related Questions