我的firebase数据结构
我的主要活动代码......
public class MainActivity extends AppCompatActivity {
ListView reviewListView;
DatabaseReference reviewDatabaseReference;
//FirebaseReview1 fr;
CustomReviewAdapter adapter;
ArrayList<UserReview> userReviews = new ArrayList<>();
TextView text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
reviewListView = (ListView) findViewById(R.id.reviewList);
//reviewDatabaseReference = FirebaseDatabase.getInstance().getReference();
//Query q = reviewDatabaseReference.child("Reviews").child("Mobile").child("Apple").child("iPhone 8 Plus").orderByChild("time");
//fr = new FirebaseReview1(q);
adapter = new CustomReviewAdapter(this,retrieve());
reviewListView.setAdapter(adapter);
}catch (Exception e){
Toast.makeText(getApplicationContext(),e.toString(),Toast.LENGTH_SHORT).show();
}
}
private void fetchReviewData(DataSnapshot dataSnapshot){
try{
userReviews.clear();
for(DataSnapshot ds: dataSnapshot.getChildren()){
UserReview userReview1 = ds.getValue(UserReview.class);
userReviews.add(userReview1);
}}catch (Exception e){
text = (TextView)findViewById(R.id.text);
text.setText(e.toString());
Toast.makeText(getApplicationContext(),e.toString(),Toast.LENGTH_SHORT).show();
}
}
public ArrayList<UserReview> retrieve()
{
try {
reviewDatabaseReference = FirebaseDatabase.getInstance().getReference();
Query q = reviewDatabaseReference.child("Reviews").child("Mobile").child("Apple").child("iPhone 8 Plus").orderByChild("time");
q.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
fetchReviewData(dataSnapshot);
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
fetchReviewData(dataSnapshot);
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}catch (Exception e){
Toast.makeText(getApplicationContext(),e.toString(), Toast.LENGTH_SHORT).show();
}
return userReviews;
}
我的UserReview类
public class UserReview {
String phone;
String name;
String rate;
String title;
String review;
String time;
public UserReview(){}
public UserReview(String phone,String name,String rate,String title,String review,String time){
this.phone = phone;
this.name = name;
this.rate = rate;
this.title = title;
this.review = review;
this.time = time;
}
public String getPhone() { return phone; }
public String getName() {
return name;
}
public String getRate() {return rate;}
public String getTitle() {
return title;
}
public String getReview() {
return review;
}
public String getTime() {return time;}
}
我的CustomeAdapter
public class CustomReviewAdapter extends BaseAdapter{
Context c;
ArrayList<UserReview> userReviews;
public CustomReviewAdapter(Context c, ArrayList<UserReview> userReviews){
this.c = c;
this.userReviews = userReviews;
}
@Override
public int getCount() {
return userReviews.size();
}
@Override
public Object getItem(int position) {
return userReviews.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null){
convertView = LayoutInflater.from(c).inflate(R.layout.review_list,parent,false);
}
TextView starTextView = (TextView)convertView.findViewById(R.id.starTextView);
TextView titleTextView = (TextView)convertView.findViewById(R.id.titleTextView);
TextView nameTextView = (TextView)convertView.findViewById(R.id.nameTextView);
TextView reviewTextView = (TextView)convertView.findViewById(R.id.reviewTextView);
UserReview ur = (UserReview)this.getItem(position);
starTextView.setText(ur.getRate());
titleTextView.setText(ur.getTitle());
nameTextView.setText(ur.getName());
reviewTextView.setText(ur.getReview());
return convertView;
}
}
我正在尝试检索数据,但却出错
DatabaseException:无法将java.lang.String类型的对象转换为类型java.lang.String以键入
答案 0 :(得分:0)
由于您正在收听onChildAdded
,因此快照中只包含一个孩子。因此,fetchData
并不需要遍历快照的孩子。
private void fetchReviewData(DataSnapshot dataSnapshot){
UserReview userReview = dataSnapshot.getValue(UserReview.class);
userReviews.add(userReview);
}