简体   繁体   中英

How do I retrieve this specific data from firebase into my android studio application listview?

I am trying to retrieve specific data from google firebase which is specific to each user that is logged in. Basically, the data that i want to retrieve is 'field' data listed under user ID's but in a separate class on google firebase realtime database. I want to be able to show the data of each field in a 'listview' in an activity on my application but whenever i try to run it, the application just closes.

I have already tried running the application under references to the 'users' class.But this didn't work.I also tried running under the 'fields' class but this also does not work.

Here is my FarmActivity.java where I want the 'listview' of the field data to show.

public class FarmActivity extends AppCompatActivity {


ListView listView;
FirebaseDatabase database;
DatabaseReference ref;
ArrayList<String> list;
ArrayAdapter<String> adapter;
FieldInformation fieldInformation;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_farm);

    listView = (ListView )findViewById(R.id.listView);
    database = FirebaseDatabase.getInstance();
    ref = database.getReference("Fields");
    list = new ArrayList<>();
    adapter = new ArrayAdapter<String>(this, R.layout.user_field_info, R.id.userInfo, list);
    fieldInformation = new FieldInformation();

    ref.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for(DataSnapshot ds:dataSnapshot.getChildren()){
                fieldInformation = ds.getValue(FieldInformation.class);
                list.add(fieldInformation.getFieldName().toString() + "  " + "\n"+ fieldInformation.getCrop().toString() + "  ");
            }
            listView.setAdapter(adapter);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

Here is my FieldInformation.java where i have specified the strings for the FarmActivity

public class FieldInformation {


public String crop;
public String fieldName;

public FieldInformation(String crop, String fieldName) {

    this.crop = crop;
    this.fieldName = fieldName;

}

public FieldInformation(){

}

public String getCrop() {
    return crop;
}

public void setCrop(String crop) {
    this.crop = crop;
}

public String getFieldName() {
    return fieldName;
}

public void setFieldName(String fieldName) {
    this.fieldName = fieldName;
}
}

Your adapter should have a list in it and your adapter should have a constructor that takes a list. You initialize your adapter in FarmActivity with the list in there and remove your set adapter then set it after you initialize adapter, lastly put adapter.notifydatasetchanged(); where you where setting your adapter. Feel free to post your adapter and ill help you fix it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM