私はFirebaseで新しいです。私の質問は、Firebaseからデータを取得する方法です。下の図は、私のデータベースの詳細を示しています。Firebaseデータベースのデータを取得していますか?
私は、UIDがそこに各UIDがあるということでデータを取得したいと思っています。そこには時間とスロットがある日々が表示されます。リストビューでこれらのデータを取得したいのですが画像私はFirebaseから時刻を取得するために持っているか
以下?フェッチおよび表示する///
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からデータを取得する方法については、/firebase.google.com/docs/database/android/lists-of-data)ガイドをご覧ください。 – Jay