Android Firebase数据库正在获取数据?
我是Firebase的新手,我的问题是如何从Firebase获取数据下图显示了我的数据库的详细信息 我想从Android Firebase数据库正在获取数据?,android,firebase,Android,Firebase,我是Firebase的新手,我的问题是如何从Firebase获取数据下图显示了我的数据库的详细信息 我想从user\u time\u table中提取数据,因为每个Uid中都有Uid,每天都有保存的天数,有时间和槽,我想在listView中提取这两个数据,并显示如下图所示 我怎么才能从Firebase那里抓到时间 object class public String uid; public String username; public String addres
user\u time\u table
中提取数据,因为每个Uid中都有Uid,每天都有保存的天数,有时间和槽,我想在listView中提取这两个数据,并显示如下图所示
我怎么才能从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;
}
}
对象类
公共字符串uid;
公共字符串用户名;
公共字符串地址;
公众弦乐日;
公共字符串槽;
公众参与时间长;
公共int starCount=0;
publicmap stars=newhashmap();
公共邮政({
//调用DataSnapshot.getValue(Post.class)所需的默认构造函数
}
公共drPost(字符串uid、字符串用户名、字符串地址、字符串日期、字符串槽、长时间){
this.uid=uid;
this.username=用户名;
this.address=地址;
this.day=天;
this.slot=slot;
这个时间=时间;
}
//[开始张贴到地图]
@排除
公共地图toMap(){
HashMap结果=新建HashMap();
结果。输入(“uid”,uid);
结果。放入(“地址”,地址);
结果。投入(“日”,日);
结果。放置(“槽”,槽);
结果。放置(“时间”,时间);
结果。输入(“starCount”,starCount);
结果。放置(“星星”,星星);
返回结果;
}
//[结束后至地图]
公共长getTime(){
返回时间;
}
公共字符串getSlot(){
返回槽;
}
公共字符串getDay(){
回归日;
}
}
//[课程结束]
///取景///
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;
}
}
在此处输入代码
公课时间表延长活动时间{
ListView ListView,listView2;
List drpostList=new ArrayList();
//数据库参考数据库时间和时隙;
数据库参考数据库参考;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(右布局、活动和时间表);
databaseReference=FirebaseDatabase.getInstance().getReference().child(“用户时间表”).child(“用户ID”);
//从xml获取ListView对象
listView=(listView)findViewById(R.id.list1);
listView2=(ListView)findViewById(R.id.list1);
drpostList=newarraylist();
//要在ListView中显示的已定义数组值
字符串[]值=新字符串[]{
“星期一”,
“星期二”,
“星期三”,
“星期四”,
“星期五”,
“星期六”,
“星期日”
};
//定义一个新适配器
//第一个参数-上下文
//第二个参数-行的布局
//第三个参数-写入数据的TextView的ID
//第四,数据数组
ArrayAdapter=新的ArrayAdapter(此,
R.布局、时间表(项目、R.id.类型、值);
//将适配器分配给ListView
setAdapter(适配器);
//ListView项目单击侦听器
setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父级、视图、,
内部位置,长id){
//ListView单击的项目索引
int itemPosition=位置;
//ListView单击的项目值
String itemValue=(String)listView.getItemAtPosition(position);
意向意向=新意向(timeline.this,editTimeline.class);
intent.putExtra(“键”,itemValue);
星触觉(意向);
//显示警惕
Toast.makeText(getApplicationContext(),“”+itemValue,Toast.LENGTH_LONG).show();
}
});
}
@凌驾
受保护的void onStart(){
super.onStart();
databaseReference.addValueEventListener(新的ValueEventListener(){
@凌驾
公共void onDataChange(DataSnapshot DataSnapshot){
//清除上一个艺术家列表
drpostList.clear();
//遍历所有节点
对于(DataSnapshot postSnapshot:DataSnapshot.getChildren()){
//获得艺术家
drPost drPost=postSnapshot.getValue(drPost.class);
//将艺术家添加到列表中
drpostList.add(drpost);
}
//创建适配器
timeandslot artistAdapter=新的时间段(timeline.this,drpostList);
//将适配器附加到listview
listView2.setAdapter(artistAdapter);
}
@凌驾
已取消的公共void(DatabaseError DatabaseError){
}
});
}
}
在这里输入代码
公共类时隙扩展ArrayAdapter{
私人活动语境;
列出drpost;
公共时间和时段(活动上下文,列表drpost){
超级(上下文、右布局、时间表、项目、drpost);
this.context=上下文;
this.drpost=drpost;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
LayoutInflater充气器=上下文。getLayoutInflater();
View listViewItem=充气机。充气(R.layout.timeline\u items,null,true);
//TextView版本\时间=(TextView)listViewItem.findViewById(R.id.version\时间);
TextView版本\槽=(TextView)listViewItem.findViewById(R.id.version\槽);
TextView Day=(TextView)listViewItem.findViewById(R.id.type);
drPost drposts=drPost.get(位置);
DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference().
child("user_timetable");
databaseReference.child("uid").addChildEventListner(new ChildEventListener()
enter code here
{
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
if (dataSnapshot != null && dataSnapshot.getValue() != null)
ObjectModel chat = dataSnapshot.getValue(ObjectModel.class);
}
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});