如何从Firebase数据库检索子值并将其显示在布局列表中?

时间:2018-11-16 20:28:01

标签: android listview arraylist firebase-realtime-database

我有一个Fragment类,我想在其中显示布局列表,并在其中使用Firebase数据值更新布局。

这是我的数据库的外观(user_dubs> user_id>照片ID):

enter image description here

以下是片段(已更新):

public class DubsFragment extends Fragment {

private static final String TAG = "DubsFragment";

//firebase
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
private FirebaseDatabase mFirebaseDatabase;
private DatabaseReference myRef;

//widgets
private EditText mthisdub;
private ListView mListView;

//vars
private ArrayList<String> mDubs;
private ArrayAdapter<String> adapter;
private Context mContext;
private PostDubs mpostdubs;
private String userID;


@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_mydubs, container, false);

    mListView = (ListView) view.findViewById(R.id.listViewz);
    mDubs = new ArrayList<>();
    mContext = getActivity();
    mpostdubs = new PostDubs();

    adapter = new ArrayAdapter<String>(mContext, R.layout.layout_dubs_listitem, R.id.getusername, mDubs);
    mListView.setAdapter(adapter);

        setupFirebaseAuth();
    return view;
}

private void setupFirebaseAuth(){
    Log.d(TAG, "setupFirebaseAuth: setting up firebase auth.");

    mAuth = FirebaseAuth.getInstance();
    mFirebaseDatabase = FirebaseDatabase.getInstance();
    myRef = mFirebaseDatabase.getReference();

    userID = mAuth.getCurrentUser().getUid();
    DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
    Query query = reference
            .child(getString(R.string.dbname_userdubs))
            .child(userID)
            .orderByChild(getString(R.string.field_photoid));
    query.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

                    for ( DataSnapshot singleSnapshot :  dataSnapshot.getChildren()) {
                        mpostdubs = singleSnapshot.getValue(PostDubs.class);
                        mDubs.add(mpostdubs.getEditdub().toString());
                        adapter.notifyDataSetChanged();
                    }
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });
}


@Override
public void onStart() {
    super.onStart();
    mAuth.addAuthStateListener(mAuthListener);
}

@Override
public void onStop() {
    super.onStop();
    if (mAuthListener != null) {
        mAuth.removeAuthStateListener(mAuthListener);
    }
}

}

后期配音班:

public class PostDubs {

    private String editdub;
    private String dubdate;
    private String user_id;
   private String photoid;
    private String username;
    private Context mContext;

    public PostDubs(String user_id, String editdub, String photoid, String dubdate, String username){
        this.user_id = user_id;
        this.editdub = editdub;
        this.dubdate = dubdate;
        this.photoid = photoid;
        this.username = username;
    }

    public PostDubs() {

    }


    public String getUser_id() {
        return user_id;
    }

    public void setUser_id(String user_id) {
        this.user_id = user_id;
    }

    public String getEditdub() {
        return editdub;
    }

    public void setEditdub(String editdub) {
        this.editdub = editdub;
    }

    public String getDubdate() {
        return dubdate;
    }

    public void setDubdate(String dubdate) {
        this.dubdate = dubdate;
    }

    public String getPhotoid() {
        return photoid;
    }

    public void setPhotoid(String photoid) {
        this.dubdate = photoid;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }


    @Override
    public String toString() {
        return "User{" +
                "user_id='" + editdub + '\'' +
                ", phone_number='" + dubdate + '\'' +
                '}';
    }

}

这是layout_dubs_listitem.xml:

<?xml version="1.0" encoding="utf-8" ?>
     <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" xmlns:app="http://schemas.android.com/apk/res-auto">
     <RelativeLayout android:layout_width="match_parent"
         android:layout_height="100dp">

    <de.hdodenhof.circleimageview.CircleImageView android:layout_width="75dp"
        android:layout_height="75dp"
        app:civ_border_color="@color/black"
         app:civ_border_width="0.5dp"
        android:id="@+id/profile_image"
        android:layout_marginLeft="20dp"
        android:layout_centerVertical="true" />

     <RelativeLayout android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:layout_toRightOf="@+id/profile_image"
         android:layout_centerVertical="true">

    <TextView android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:textSize="22sp"
        android:textColor="@color/black"
        android:id="@+id/editdub"
        android:text=""/>

    <TextView android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:textSize="15sp"
        android:layout_centerVertical="true"
        android:textColor="@color/black"
         android:id="@+id/getusername"
        android:text=""/>
</RelativeLayout>
</RelativeLayout>
</RelativeLayout>

对于测试,我只是试图在布局中获取编辑配音,但它根本无法正常工作。我确定我在做一些愚蠢的事情。

请指导我解决此问题,如果您可以帮助我使用其他子值(如用户名)更新布局,这将非常有帮助。谢谢。

1 个答案:

答案 0 :(得分:0)

我通过正确地命令孩子在firebase中进行修复。