Android 如何使用listview片段从firebase检索数据?
//在检索数据时,我混淆了这部分代码。我希望你能帮我解决这个难题Android 如何使用listview片段从firebase检索数据?,android,listview,firebase,navigation-drawer,Android,Listview,Firebase,Navigation Drawer,//在检索数据时,我混淆了这部分代码。我希望你能帮我解决这个难题 package com.resurreccion.tophe.sma; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.v4.app.Fragment; import android.util.Log; import android.view.LayoutInflater; import
package com.resurreccion.tophe.sma;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import java.util.ArrayList;
import static com.resurreccion.tophe.sma.R.id.lv;
/**
* A simple {@link Fragment} subclass.
* Use the {@link Fragment_Profile#newInstance} factory method to
* create an instance of this fragment.
*/
public class Fragment_Profile extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
private static final String TAG = "Fragment_Profile";
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
public Fragment_Profile() {
// Required empty public constructor
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment Fragment_Profile.
*/
// TODO: Rename and change types and number of parameters
public static Fragment_Profile newInstance(String param1, String param2) {
Fragment_Profile fragment = new Fragment_Profile();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
private FirebaseDatabase fd;
private FirebaseAuth fb_view;
private DatabaseReference dr;
private FirebaseAuth.AuthStateListener fbl;
private String userID;
private ListView mlist;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@覆盖
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
视图=充气机。充气(R.layout.activity\u配置文件,容器,错误);
fb_view=FirebaseAuth.getInstance();
fd=FirebaseDatabase.getInstance();
dr=fd.getReference().child(“stdb”);
mlist=(ListView)getView().findViewById(R.id.lv);
FirebaseUser=fb_view.getCurrentUser();
userID=user.getUid();
fbl=新的FirebaseAuth.AuthStateListener(){
@凌驾
AuthStateChanged上的公共void(@NonNull FirebaseAuth FirebaseAuth){
FirebaseUser=firebaseAuth.getCurrentUser();
如果(用户!=null){
//用户已登录
Log.d(标记为“+user.getUid()”);
//Toast.makeText(Fragment_Profile.this,“已成功登录:”+user.getEmail(),Toast.LENGTH_SHORT)。
//show();
}否则{
//用户已注销
Log.d(标记“onAuthStateChanged:signed_out”);
//Toast.makeText(Fragment_Profile.this,“成功注销”,Toast.LENGTH_SHORT.show();
}
}
};
dr.addValueEventListener(新的ValueEventListener(){
@凌驾
公共void onDataChange(DataSnapshot DataSnapshot){
showData(dataSnapshot);
}
@凌驾
已取消的公共void(DatabaseError DatabaseError){
}
});
返回视图;
}
私有void showData(DataSnapshot DataSnapshot){
对于(DataSnapshot ds:DataSnapshot.getChildren()){
UserInfo uInfo=新UserInfo();
uInfo.setStudent_num(ds.child(userID).getValue(UserInfo.class.getStudent_num());//设置学生的名称
uInfo.setPassword(ds.child(userID).getValue(UserInfo.class.getPassword());//设置密码
uInfo.setLast_name(ds.child(userID).getValue(UserInfo.class).getLast_name());//设置姓氏
uInfo.setFirst_name(ds.child(userID).getValue(UserInfo.class).getFirst_name());//设置第一个_名称
uInfo.setMiddle_name(ds.child(userID).getValue(UserInfo.class).getLast_name());//设置中间名称
//显示所有信息
Log.d(标记“showData:student_name:+uInfo.getStudent_num());
Log.d(标记“showData:password:+uInfo.getPassword());
Log.d(标记“showData:last_name:+uInfo.getLast_name());
Log.d(标记“showData:first_name:+uInfo.getFirst_name());
Log.d(标记“showData:middle_name:+uInfo.getMiddle_name());
ArrayList数组=新的ArrayList();
add(uInfo.getStudent_num());
add(uInfo.getPassword());
add(uInfo.getLast_name());
add(uInfo.getFirst_name());
add(uInfo.getMiddle_name());
ArrayAdapter=新的ArrayAdapter(Fragment_Profile.this.getActivity(),android.R.layout)。
简单列表项目1,
阵列);
mlist.setAdapter(适配器);
}
}
}
此片段用于在片段上显示listview,但listview无法从firebase获取数据。有谁能帮我摆脱这一切。提前感谢很难在运行时在listview中管理行列表。因此,Listview不是显示Firebase数据的好选项。试试Recylerview。在此处编码->您可以查看这个github项目,我在活动中使用了listview,它保存了firebase数据库的数据
但要在fragment中使用,它与活动类似,只需在fragment中的onViewCreated()方法中添加代码。移动应用程序正在工作,但当我尝试检索数据时,它没有显示从FIrebaseUI写入的特定数据。您所做的一切都有点遗留,请尝试使用FIrebaseUI和FirebaseRecycleView。除非你有更高级的用例。看到了吗?它对导航抽屉中的片段有效吗?@ChristopherResurReccion-是的,你可以用Recyclerview替换任何ListVIew
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_profile, container, false);
fb_view = FirebaseAuth.getInstance();
fd = FirebaseDatabase.getInstance();
dr = fd.getReference().child("stdb");
mlist = (ListView) getView().findViewById(R.id.lv);
FirebaseUser user = fb_view.getCurrentUser();
userID = user.getUid();
fbl = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user != null) {
// User is signed in
Log.d(TAG, "onAuthStateChanged:signed_in:" + user.getUid());
//Toast.makeText(Fragment_Profile.this,"Successfully signed in with: " + user.getEmail(),Toast.LENGTH_SHORT).
//show();
} else {
// User is signed out
Log.d(TAG, "onAuthStateChanged:signed_out");
//Toast.makeText(Fragment_Profile.this,"Successfully signed out.",Toast.LENGTH_SHORT).show();
}
}
};
dr.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
showData(dataSnapshot);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
return view;
}
private void showData(DataSnapshot dataSnapshot) {
for (DataSnapshot ds : dataSnapshot.getChildren()) {
UserInfo uInfo = new UserInfo();
uInfo.setStudent_num(ds.child(userID).getValue(UserInfo.class).getStudent_num()); //set the student_name
uInfo.setPassword(ds.child(userID).getValue(UserInfo.class).getPassword()); //set the password
uInfo.setLast_name(ds.child(userID).getValue(UserInfo.class).getLast_name()); //set the last_name
uInfo.setFirst_name(ds.child(userID).getValue(UserInfo.class).getFirst_name()); //set the first_name
uInfo.setMiddle_name(ds.child(userID).getValue(UserInfo.class).getLast_name()); //set the middle_name
//display all the information
Log.d(TAG, "showData: student_name: " + uInfo.getStudent_num());
Log.d(TAG, "showData: password: " + uInfo.getPassword());
Log.d(TAG, "showData: last_name: " + uInfo.getLast_name());
Log.d(TAG, "showData: first_name: " + uInfo.getFirst_name());
Log.d(TAG, "showData: middle_name: " + uInfo.getMiddle_name());
ArrayList<String> array = new ArrayList<>();
array.add(uInfo.getStudent_num());
array.add(uInfo.getPassword());
array.add(uInfo.getLast_name());
array.add(uInfo.getFirst_name());
array.add(uInfo.getMiddle_name());
ArrayAdapter adapter = new ArrayAdapter(Fragment_Profile.this.getActivity(), android.R.layout.
simple_list_item_1,
array);
mlist.setAdapter(adapter);
}
}
}