androidNoob1980
androidNoob1980

Reputation: 5

getting data from firebase database using getkey

hi guys a bit of a noob question but I'm using firebase database and I'm using getkey to fill my listview with the keys. these key represent jobs in my app.

 dbref.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            if (dataSnapshot.exists()) {
                for(DataSnapshot ds : dataSnapshot.getChildren()) {
                    keyList.add(ds.getKey());

                    final String[] REGISTRATION_KEY = keyList.toArray(new String[keyList.size()]);


                   currentjobAdapter apkData = new currentjobAdapter(Current_Jobs.this, REGISTRATION_KEY);
                  lv.setAdapter(apkData);
                    apkData.notifyDataSetChanged();


                }
            }
        }

this all works great it populates great thanks to help from you guys. My next problem though is i need to also take the information from REGISTRATION_KEY and then use it to gain access to that child.

My database is as follows

GH67FGH{
  GH67FGH{ 
   Important_Job true:
   Date 15 04 2018:

} } What i want to do is not only fill the listview with the key which in this case would be GH67FGH but also i want to add the info from Important_job and Date.

i know i already have the keys here keyList.add(ds.getKey());

how can i turn the key list to a String for each item in my list and then use it to access the child().getValue()

Upvotes: 0

Views: 230

Answers (1)

markharrop
markharrop

Reputation: 876

So i think i may have the answer, you can access it from your base adapter by setting a value event listener to fire when populating your views. Try something like this

public class currentjobAdapter extends BaseAdapter {
String[] Reg;
Context C;
public currentjobAdapter() {
    Reg = null;
}

public currentjobAdapter(Context c, String[] reg){
 this.C = c;
    Reg = reg;
}
@Override
public int getCount() {
    return Reg.length;
}

@Override
public Object getItem(int i) {
    return null;
}

@Override
public long getItemId(int i) {
    return i;
}

@Override
public View getView(final int i, View convertView, ViewGroup viewGroup) {
    final View row;


    if (convertView == null) {

        row = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.YourCustomList, viewGroup, false);

    } else {
        row = convertView;
    }


   TextView tv1 =  row.findViewById(R.id.yourid);
    tv1.setText(Reg[i]);
    final String[] date = new String[1];
    String registration = Reg[i];
    FirebaseDatabase db = FirebaseDatabase.getInstance();
    DatabaseReference dbref = db.getReference().child(registration).child(registration);
    dbref.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            date[0] = dataSnapshot.child("date").getValue(String.class);

            TextView tv2 = (TextView) row.findViewById(R.id.yourid2);
            tv2.setText(date[0]);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
    return row;
}

}

Upvotes: 1

Related Questions