Android ExpandableListView不与firebase一起使用
正在尝试使用ExpandableListView显示firebase数据,但在使用静态数据时,它没有显示任何内容,或者是否有其他方法列出数据,如ExpandableListView,但可以使用firebaseAndroid ExpandableListView不与firebase一起使用,android,firebase,firebase-realtime-database,Android,Firebase,Firebase Realtime Database,正在尝试使用ExpandableListView显示firebase数据,但在使用静态数据时,它没有显示任何内容,或者是否有其他方法列出数据,如ExpandableListView,但可以使用firebase @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // In
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
final View view = inflater.inflate(R.layout.fragment_appointment, container, false);
if (user != null) {
buttonAppoint = view.findViewById(R.id.bappoint);
buttonAppoint.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Fragment newFragment = new Register();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.flContent, newFragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
}
});
// get the listview
expListView = view.findViewById(R.id.explist);
// preparing list data
prepareListData();
listAdapter = new ExpandableListAdapter(this.getContext(), listDataHeader, listDataChild);
// setting list adapter
expListView.setAdapter(listAdapter);
// Listview Group click listener
expListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
// Toast.makeText(getApplicationContext(),
// "Group Clicked " + listDataHeader.get(groupPosition),
// Toast.LENGTH_SHORT).show();
return false;
}
});
// Listview Group expanded listener
expListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
@Override
public void onGroupExpand(int groupPosition) {
Toast.makeText(getActivity().getApplicationContext(),
listDataHeader.get(groupPosition) + " Expanded",
Toast.LENGTH_SHORT).show();
}
});
// Listview Group collasped listener
expListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {
@Override
public void onGroupCollapse(int groupPosition) {
Toast.makeText(getActivity().getApplicationContext(),
listDataHeader.get(groupPosition) + " Collapsed",
Toast.LENGTH_SHORT).show();
}
});
// Listview on child click listener
expListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
// TODO Auto-generated method stub
Toast.makeText(
getActivity().getApplicationContext(),
listDataHeader.get(groupPosition)
+ " : "
+ listDataChild.get(
listDataHeader.get(groupPosition)).get(
childPosition), Toast.LENGTH_SHORT)
.show();
return false;
}
});
Log.d(TAG, "onAuthStateChanged:signed_in:" + user.getUid());
} else {
Toast.makeText(getActivity(), "Please login before view/make appointment",
Toast.LENGTH_SHORT).show();
Fragment newFragment = new LoginFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.flContent, newFragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
Log.d(TAG, "onAuthStateChanged:signed_out");
}
return view;
}
private void prepareListData() {
listDataHeader = new ArrayList<>();
listDataChild = new HashMap<>();
myRef.orderByChild("userID").equalTo(currentUser).addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String prevChildKey) {
Appointment appointment = dataSnapshot.getValue(Appointment.class);
listDataHeader.add(appointment.date+" "+ appointment.time);
List<String> appoint = new ArrayList<>();
appoint.add("Hours of Service: "+ appointment.hours);
appoint.add("Person In Charge: "+ appointment.personInCharge);
appoint.add("Address: "+ appointment.address);
appoint.add("Contact: "+ appointment.contact);
listDataChild.put(listDataHeader.get(i), appoint); // Header, Child data
i++;
listAdapter.notifyDataSetChanged();
}
@Override
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) {
}
});
}
@覆盖
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
//为该碎片膨胀布局
最终视图=充气机。充气(R.layout.fragment_,容器,错误);
如果(用户!=null){
buttonAppoint=view.findViewById(R.id.bappoint);
ButtonPoint.setOnClickListener(新视图.OnClickListener(){
公共void onClick(视图){
Fragment newFragment=新寄存器();
FragmentTransaction=getFragmentManager().beginTransaction();
事务。替换(R.id.flContent,newFragment);
transaction.addToBackStack(空);
//提交事务
commit();
}
});
//获取列表视图
expListView=view.findViewById(R.id.explist);
//准备列表数据
prepareListData();
listAdapter=新的ExpandableListAdapter(this.getContext(),listDataHeader,listDataChild);
//设置列表适配器
expListView.setAdapter(listAdapter);
//Listview组单击侦听器
expListView.setOnGroupClickListener(新的ExpandableListView.OnGroupClickListener(){
@凌驾
公共布尔值onGroupClick(ExpandableListView父视图,视图v,
int groupPosition,长id){
//Toast.makeText(getApplicationContext(),
//“组单击”+listDataHeader.get(groupPosition),
//吐司。长度(短)。show();
返回false;
}
});
//Listview组扩展侦听器
expListView.setOnGroupExpandListener(新的ExpandableListView.OnGroupExpandListener(){
@凌驾
public void onGroupExpand(int groupPosition){
Toast.makeText(getActivity().getApplicationContext(),
listDataHeader.get(groupPosition)+“Expanded”,
吐司。长度(短)。show();
}
});
//Listview组压缩侦听器
expListView.setOnGroupCollapseListener(新的ExpandableListView.OnGroupCollapseListener(){
@凌驾
公共void-onGroupCollapse(int-groupPosition){
Toast.makeText(getActivity().getApplicationContext(),
listDataHeader.get(groupPosition)+“折叠”,
吐司。长度(短)。show();
}
});
//子单击侦听器上的Listview
expListView.setOnChildClickListener(新的ExpandableListView.OnChildClickListener(){
@凌驾
公共布尔onChildClick(ExpandableListView父视图,视图v,
int groupPosition、int childPosition、long id){
//TODO自动生成的方法存根
Toast.makeText(
getActivity().getApplicationContext(),
listDataHeader.get(groupPosition)
+ " : "
+listDataChild.get(
listDataHeader.get(groupPosition)).get(
儿童位置),吐司。长度(短)
.show();
返回false;
}
});
Log.d(标记为“+user.getUid()”);
}否则{
Toast.makeText(getActivity(),“请在查看/预约前登录”,
吐司。长度(短)。show();
Fragment newFragment=新登录片段();
FragmentTransaction=getFragmentManager().beginTransaction();
事务。替换(R.id.flContent,newFragment);
transaction.addToBackStack(空);
//提交事务
commit();
Log.d(标记“onAuthStateChanged:signed_out”);
}
返回视图;
}
私有void prepareListData(){
listDataHeader=新的ArrayList();
listDataChild=newHashMap();
myRef.orderByChild(“userID”).equalTo(currentUser).addChildEventListener(新的ChildEventListener(){
@凌驾
公共void onChildAdded(DataSnapshot DataSnapshot,字符串prevChildKey){
约会=dataSnapshot.getValue(Appointment.class);
listDataHeader.add(约会日期+“”+约会时间);
列表指定=新的ArrayList();
添加(“服务时间:+预约时间”);
任命。添加(“负责人:+任命。负责人”);
任命。添加(“地址:”+任命。地址);
任命。添加(“联系人:+任命。联系人”);
listDataChild.put(listDataHeader.get(i),commission);//头,子数据
i++;
listAdapter.notifyDataSetChanged();
}
@凌驾
公共void onChildChanged(DataSnapshot DataSnapshot,字符串s){
}
@凌驾
ChildRemoved上的公共void(DataSnapshot DataSnapshot){
}
@凌驾
已移动ChildMoved上的公共void(DataSnapshot DataSnapshot,字符串s){
}
@凌驾
已取消的公共void(DatabaseError DatabaseError){
}
});
}
从Firebase获得结果后,您需要通知DataChange谢谢,它现在可以工作了,另一件事是如何在进入页面时显示它,它只在添加数据后才显示。用户addValueEventListener代替了AddChildEventListener OK Simuly me,只是在启动加载缓慢时…仍然显示原样。。childeventlistener还可以