我想制作Recycler View,但我也想从数据库向构造函数添加不同的值
这是我的DaftarTugas.java
package com.example.yehezkiel.eclassapp;
/**
* Created by Yehezkiel on 7/12/2018.
*/
public class DaftarTugas{
private String judul_tugas;
private String deskripsi_tugas;
private String tanggal_kumpul;
private String tanggal_tugas;
private String nama_matkul;
public DaftarTugas(String nama_matkul,String judul_tugas, String deskripsi_tugas, String tanggal_kumpul, String tanggal_tugas) {
this.nama_matkul = nama_matkul;
this.judul_tugas = judul_tugas;
this.deskripsi_tugas = deskripsi_tugas;
this.tanggal_kumpul = tanggal_kumpul;
this.tanggal_tugas = tanggal_tugas;
}
public DaftarTugas() {
}
public String getNama_matkul() {
return nama_matkul;
}
public void setNama_matkul(String nama_matkul) {
this.nama_matkul = nama_matkul;
}
public String getJudul_tugas() {
return judul_tugas;
}
public void setJudul_tugas(String judul_tugas) {
this.judul_tugas = judul_tugas;
}
public String getDeskripsi_tugas() {
return deskripsi_tugas;
}
public void setDeskripsi_tugas(String deskripsi_tugas) {
this.deskripsi_tugas = deskripsi_tugas;
}
public String getTanggal_kumpul() {
return tanggal_kumpul;
}
public void setTanggal_kumpul(String tanggal_kumpul) {
this.tanggal_kumpul = tanggal_kumpul;
}
public String getTanggal_tugas() {
return tanggal_tugas;
}
public void setTanggal_tugas(String tanggal_tugas) {
this.tanggal_tugas = tanggal_tugas;
}
}
这是我来自TwoFragment.Java
的代码
package com.example.yehezkiel.eclassapp;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
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 java.util.List;
public class TwoFragment extends Fragment {
View v;
private ArrayList<String> key2 = new ArrayList<>();
private DaftarTugas tugas = new DaftarTugas();
private ProgressBar mProgressBar4;
private List<DaftarTugas> listTugas = new ArrayList<>();
private RecyclerView mRecycleView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener authListener;
private ArrayList<String> obj2 = new ArrayList<>();
DatabaseReference userRef = FirebaseDatabase.getInstance().getReference("users");
FirebaseUser users = FirebaseAuth.getInstance().getCurrentUser();
DatabaseReference mataKuliahRef = FirebaseDatabase.getInstance().getReference("courses");
DatabaseReference daftarTugasRef = FirebaseDatabase.getInstance().getReference("tugas");
public TwoFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
v = inflater.inflate(R.layout.fragment_two, container, false);
mAuth = FirebaseAuth.getInstance();
//Recycler View
mRecycleView = (RecyclerView) v.findViewById(R.id.TugasRView);
mRecycleView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(getActivity());
mRecycleView.setLayoutManager(mLayoutManager);
mRecycleView.addItemDecoration(new DividerItemDecoration(getActivity(), LinearLayoutManager.VERTICAL));
mAdapter = new myAdapterTugas(listTugas);
mRecycleView.setAdapter(mAdapter);
Bundle bundle = this.getArguments();
if(getArguments()!=null)
{
obj2 = bundle.getStringArrayList("keys");
Log.e("nba",obj2.toString());
}
for(int i = 0;i<obj2.size();i++){
mataKuliahRef.child(obj2.get(i)).child("name").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot1) {
tugas.setName(dataSnapshot1.getValue().toString());
listTugas.add(tugas);
mAdapter.notifyDataSetChanged();
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
mataKuliahRef.child(obj2.get(i)).child("tugas").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
listTugas.clear();
if (dataSnapshot.exists()) {
for (DataSnapshot dataHasil : dataSnapshot.getChildren()) {
final String idTugas = dataHasil.getKey();
Log.e("Yhz2", idTugas);
daftarTugasRef.child(idTugas).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot2) {
tugas = dataSnapshot2.getValue(DaftarTugas.class);
listTugas.add(tugas);
mAdapter.notifyDataSetChanged();
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
return v;
}
}
我只想为DaftarTugas添加名称,daftar tugas的名称为deskripsi_tugas,judul_tugas,tanggal_kumpul,tanggal_tugas。已经有deskripsi_tugas,judul_tugas,tanggal_kumpul,tanggal_tugas做得很好。因此,我想向该java类再添加1个,这是另一个数据库中的名称
mataKuliahRef.child(obj2.get(i)).child("name").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot1) {
tugas.setName(dataSnapshot1.getValue().toString());
listTugas.add(tugas);
mAdapter.notifyDataSetChanged();
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
注意: 1. obj2包含另一个片段的密钥,该片段是我发送给TwoFragment
的