我遇到来自firebase数据库的值和一个简单的textview显示它们的问题。
public class FindReportActivity extends Activity implements View.OnClickListener{
EditText editTextFindReport;
TextView textViewCommento;
TextView textViewGravita;
ImageView imageViewFindReport;
String cdt;
Report report;
ReportDAO reportDAO = new ReportDAO();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_find_report);
imageViewFindReport = (ImageView) findViewById(R.id.imageViewFindReport);
editTextFindReport = (EditText) findViewById(R.id.editTextCDT);
textViewCommento= (TextView) findViewById(R.id.textViewcommento);
textViewGravita= (TextView) findViewById(R.id.textViewgravita);
imageViewFindReport.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if( v == imageViewFindReport){
cdt = editTextFindReport.getText().toString().trim();
textViewCommento.setText(reportDAO.findReport(cdt));
}
}
}
这里是ReportDAO类
public class ReportDAO {
private FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance();
private DatabaseReference databaseReference = firebaseDatabase.getReference();
String commento = "non";
public ReportDAO(){ }
public String findReport(String cdt){
DatabaseReference refReport = databaseReference.child("Report").child(cdt).child("commento");
refReport.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
commento = dataSnapshot.getValue(String.class);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
return commento;
}
当我点击imageview时,第一次是空白,第二次是正确值...为什么?
答案 0 :(得分:0)
您需要实现一个等待评论可用的侦听器,因为findReport方法是异步的。