MaintActivity
package com.finn.aroundu;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
private RecyclerView mPostList;
private DatabaseReference mDatabase;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDatabase= FirebaseDatabase.getInstance().getReference();
Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
mPostList = (RecyclerView) findViewById(R.id.post_list);
mPostList.setHasFixedSize(true);
if(recyclerView != null) {
mPostList.setLayoutManager(new LinearLayoutManager(this));
}
}
@Override
protected void onStart() {
super.onStart();
FirebaseRecyclerAdapter<Location_data,PostViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Location_data, PostViewHolder>(
Location_data.class,
R.layout.recycler_adpater,
PostViewHolder.class,
mDatabase
) {
@Override
protected void populateViewHolder(PostViewHolder viewHolder, Location_data model, int position) {
viewHolder.setName(model.getName());
viewHolder.setCaption(model.getCaption());
viewHolder.setLocation(model.getLocation());
}
};
mPostList.setAdapter(firebaseRecyclerAdapter);
}
public static class PostViewHolder extends RecyclerView.ViewHolder {
View mView;
public PostViewHolder(View itemView) {
super(itemView);
itemView= mView;
}
public void setName(String name){
TextView info_text = (TextView) mView.findViewById(R.id.info_text);
info_text.setText(name);
}
public void setCaption(String caption){
TextView info_text2=(TextView) mView.findViewById(R.id.info_text2);
info_text2.setText(caption);
}
public void setLocation(String location){
TextView info_text3 = (TextView)mView.findViewById(R.id.info_text3);
info_text3.setText(location);
}
}
public boolean onCreateOptionsMenu(final Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId()==R.id.add){
startActivity(new Intent(MainActivity.this,post.class));
}
return super.onOptionsItemSelected(item);
}
}
Location_Activity(方法)
package com.finn.aroundu;
/**
* Created by Sagar on 1/31/2017.
*/
public class Location_data {
private String Name;
private String Location;
private String Caption;
public Location_data(){
}
public Location_data(String Location, String Name, String Caption) {
this.Location = Location;
this.Name = Name;
this.Caption = Caption;
}
public String getCaption() {
return Caption;
}
public void setCaption(String Caption) {
this.Caption = Caption;
}
public String getLocation() {
return Location;
}
public void setLocation(String Location) {
this.Location = Location;
}
public String getName() {
return Name;
}
public void setName(String Name) {
this.Name = Name;
}
}
我正在创建一个简单的应用程序来从我的firebase数据库中获取数据并将其显示在RecyclerView上,但数据根本没有提取。输出只是一个空白活动。我是应用程序开发的新手,我认为我犯了一些愚蠢的错误,请指出我的错误。
CRASH LOG:
com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type com.finn.aroundu.Location_data
得到了修复: 我的代码中有很多重大问题,我无意中忘记了从我所参考的博客编写活动代码。 不管怎样,谢谢你!
答案 0 :(得分:0)
就像异常所说的那样,你会收到字符串json响应。您需要转换为对象
https://www.learnhowtoprogram.com/android/data-persistence/firebase-recycleradapter
答案 1 :(得分:0)
在行中检查对数据库的引用:
mDatabase= FirebaseDatabase.getInstance().getReference();