我是Firebase新手,我的問題是如何從Firebase獲取數據,下圖顯示了我的數據庫的詳細信息。Firebase數據庫獲取數據?
我想從user_time_table
中獲取數據,那個UID在每個uid裏面都有saven天,每天都顯示有時間和槽在那裏我想要在listView中獲取這些拖拽數據,並像在下面的圖片
我怎麼也來從火力地堡的時間?
object class
public String uid;
public String username;
public String address;
public String day;
public String slot;
public long time;
public int starCount = 0;
public Map<String, Boolean> stars = new HashMap<>();
public drPost() {
// Default constructor required for calls to DataSnapshot.getValue(Post.class)
}
public drPost(String uid, String username, String address, String day, String slot, long time) {
this.uid = uid;
this.username = username;
this.address = address;
this.day = day;
this.slot = slot;
this.time = time;
}
// [START post_to_map]
@Exclude
public Map<String, Object> toMap() {
HashMap<String, Object> result = new HashMap<>();
result.put("uid", uid);
result.put("address", address);
result.put("day", day);
result.put("slot", slot);
result.put("time", time);
result.put("starCount", starCount);
result.put("stars", stars);
return result;
}
// [END post_to_map]
public long getTime() {
return time;
}
public String getSlot(){
return slot;
}
public String getDay(){
return day;
}
}
// [END post_class]
///獲取並查看///
enter code here
public class timetable extends Activity {
ListView listView, listView2;
List<drPost> drpostList = new ArrayList<>();
// DatabaseReference databasetimeandslot;
DatabaseReference databaseReference;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_timetable);
databaseReference = FirebaseDatabase.getInstance().getReference().child("user_time_table").child("userID");
// Get ListView object from xml
listView = (ListView) findViewById(R.id.list1);
listView2 = (ListView) findViewById(R.id.list1);
drpostList = new ArrayList<>();
// Defined Array values to show in ListView
String[] values = new String[]{
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
"Sunday"
};
// Define a new Adapter
// First parameter - Context
// Second parameter - Layout for the row
// Third parameter - ID of the TextView to which the data is written
// Forth - the Array of data
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.timetable_items, R.id.type, values);
// Assign adapter to ListView
listView.setAdapter(adapter);
// ListView Item Click Listener
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// ListView Clicked item index
int itemPosition = position;
// ListView Clicked item value
String itemValue = (String) listView.getItemAtPosition(position);
Intent intent = new Intent(timetable.this, EditTimeTable.class);
intent.putExtra("key", itemValue);
startActivity(intent);
// Show Alert
Toast.makeText(getApplicationContext(), "" + itemValue, Toast.LENGTH_LONG).show();
}
});
}
@Override
protected void onStart() {
super.onStart();
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
//clearing the previous artist list
drpostList.clear();
//iterating through all the nodes
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
//getting artist
drPost drpost = postSnapshot.getValue(drPost.class);
//adding artist to the list
drpostList.add(drpost);
}
//creating adapter
timeandslot artistAdapter = new timeandslot(timetable.this, drpostList);
//attaching adapter to the listview
listView2.setAdapter(artistAdapter);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
enter code here
public class timeandslot extends ArrayAdapter<drPost> {
private Activity context;
List<drPost> drpost;
public timeandslot(Activity context, List<drPost> drpost) {
super(context, R.layout.timetable_items, drpost);
this.context = context;
this.drpost = drpost;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View listViewItem = inflater.inflate(R.layout.timetable_items, null, true);
// TextView version_TIME = (TextView) listViewItem.findViewById(R.id.version_TIME);
TextView version_SLOT = (TextView) listViewItem.findViewById(R.id.version_SLOT);
TextView Day = (TextView) listViewItem.findViewById(R.id.type);
drPost drposts = drpost.get(position);
// version_TIME.setText((int) drposts.getTime());
version_SLOT.setText(drposts.getSlot());
Day.setText(drposts.getDay());
return listViewItem;
}
}
你也許應該通過[開始](HTTPS:/ /firebase.google.com/docs/database/android/lists-of-data)指南,因爲它介紹瞭如何從Firebase檢索數據。 – Jay