我的ListView使用适配器时出现问题,并且填充了Firebase内容。但是在我开启活动并返回活动之前,它不会出现在活动的启动时。 这是我的FirebaseHelper代码
public class FirebaseHelper {
DatabaseReference db;
QuestionAdapter adapter;
View v;
ArrayList<Questions> questionEntries =new ArrayList<>();
public FirebaseHelper(DatabaseReference db) {
this.db=db;
}
/*private void fetchData (DataSnapshot dataSnapshot){
questionEntries.clear();
Questions questions = dataSnapshot.getValue(Questions.class);
questionEntries.add(questions);}*/
public ArrayList<Questions> retreive (){
db.orderByChild("question_date").addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
Questions questions =dataSnapshot.getValue(Questions.class);
questionEntries.add(questions);
}
@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) {
}
});
return questionEntries;
}
}
这是活动。 我尝试使用Visibility仅在有内容时显示,但它并不总是有效 这是我的活动:
public class Actu extends Fragment {
private DatabaseReference db;
FirebaseHelper helper;
QuestionAdapter adapter;
ListView lv;
View v;
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
v = inflater.inflate(R.layout.activity_actu, container, false);
lv = (ListView) v.findViewById(R.id.actu_rv);
progressBar = (ProgressBar) v.findViewById(R.id.progress_bar);
no_connection =(TextView) v.findViewById(R.id.no_connection);
db = FirebaseDatabase.getInstance().getReference("Questions");
db.limitToLast(100).orderByKey();
helper = new FirebaseHelper(db);
adapter = new QuestionAdapter(getContext(),helper.retreive());
lv.setAdapter(adapter);
return v;
}
请帮助我。
更新
public QuestionAdapter(Context c, ArrayList<Questions> questionEntries) {
this.c = c;
this.questionEntries = questionEntries;
}
@Override
public int getCount() {
return questionEntries.size();
}
@Override
public Object getItem(int position) {
return questionEntries.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View contentView, ViewGroup parent) {
if (contentView==null){
contentView= LayoutInflater.from(c).inflate(R.layout.actu_list_item,parent,false);
}
TextView asker = (TextView) contentView.findViewById(R.id.asker_name);
TextView date = (TextView) contentView.findViewById(R.id.date);
TextView title = (TextView) contentView.findViewById(R.id.question_title);
TextView content = (TextView) contentView.findViewById(R.id.question_content);
Button answer = (Button) contentView.findViewById(R.id.answer);
final EditText answerET= (EditText) contentView.findViewById(R.id.answer_edittext);
posit = this.getItem(position);
answer.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
answer_text = answerET.getText().toString();
if(answer_text.length()<20){
Toast.makeText(c,"Vous devez entrer au moins 20 cgharactères",Toast.LENGTH_LONG).show();
}else
{
Toast.makeText(c,"Post en cours...",Toast.LENGTH_LONG).show();
Answer answerClass = new Answer();
FirebaseDatabase database = FirebaseDatabase.getInstance();
FirebaseAuth firebaseAuth1 = FirebaseAuth.getInstance();
FirebaseUser firebaseUser= firebaseAuth1.getCurrentUser();
qe= (Questions) posit;
SimpleDateFormat dateFormat=new SimpleDateFormat("HH:mm dd-MM-yyyy");
String date= dateFormat.format(new Date(System.currentTimeMillis()));
assert firebaseUser !=null;
DatabaseReference Asker = database.getReference(firebaseUser.getUid()+"/name");
Asker.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
name = dataSnapshot.getValue(String.class);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
answerClass.setAnswer(answer_text);
answerClass.setUsername(name);
answerClass.setDate(date);
DatabaseReference AnswerRef =database.getReference("Questions/"+qe.getTag_id()+"/");
AnswerRef.push().setValue(answerClass).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
Toast.makeText(c,"Reponse postée",Toast.LENGTH_LONG).show();
}
});
}
}
});
qe= (Questions) this.getItem(position);
//有错误 //看起来qe是空的 asker.setText(qe.getQuestion_username()); date.setText(qe.getQuestion_date()); title.setText(String.format(“Q:%s”,qe.getQuestion_title())); content.setText(qe.getQuestion_content());
contentView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(c,qe.getQuestion_username(),Toast.LENGTH_LONG).show();
}
});
this.notifyDataSetChanged();
return contentView;
}
答案 0 :(得分:1)
您不能以您的方式返回questionEntries
ArrayList。当你使用这行代码时:
return questionEntries;
Firebase没有从您的数据库中获取数据。这是因为onChildAdded()
方法的异步行为而发生的。因此,要解决此问题,请使用以下代码:
db.orderByChild("question_date").addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
ArrayList<Questions> questionEntries = new ArrayList<>();
Questions questions = dataSnapshot.getValue(Questions.class);
questionEntries.add(questions);
Log.d("TAG", questionEntries);
}
@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) {}
});
编辑1:另一种方法是使用ValueEventListener
,如下所示:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference db = rootRef.child("Question");
ValueEventListener eventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
ArrayList<Questions> questionEntries = new ArrayList<>();
for(DataSnapshot ds : dataSnapshot.getChildren()) {
Questions questions = ds.getValue(Questions.class);
questionEntries.add(questions);
}
Log.d("TAG", questionEntries);
}
@Override
public void onCancelled(DatabaseError databaseError) {}
};
db.addListenerForSingleValueEvent(eventListener);
Edit2:假设Question
节点是Firebase数据库根目录的直接子节点,另一种方法是:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference db = rootRef.child("Question");
ValueEventListener eventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
ArrayList<String> questionEntries = new ArrayList<>();
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String question_title = ds.child("question_title").getValue(String.class);
questionEntries.add(question_title);
}
Log.d("TAG", questionEntries);
}
@Override
public void onCancelled(DatabaseError databaseError) {}
};
db.addListenerForSingleValueEvent(eventListener);
答案 1 :(得分:0)
问题似乎与 questionEntries 列表有关。