无法退回键入
W/System.err:
at com.firebase.client.DataSnapshot.getValue(DataSnapshot.java:183)
caused by : com.fasterxml.jackson.databind.JsonMappingException:
Can not instantiate value of type [simple type, class hi.example.com.hi.DataModel] from String value; no single-String constructor/factory method.
即使我在我的DataModel类中使用单个字符串构造函数,我在构造函数中一次得到一个字符串值,为什么会发生这种情况,请有人帮助我..
here is my json file
{
"messages": {
"matt__walk": {
"Username": "matt__walk",
"imageurl": "https://firebasestorage.googleapis.com/v0",
"text": "hii"
}
}
}
DataModel类:
@JsonIgnoreProperties(ignoreUnknown=true)
public class DataModel {
private String Username;
private String imageurl;
private String text;
public DataModel(){
}
public String getUsername() {
return Username;
}
public String getText() {
return text;
}
public String getImageUrl() {
return imageurl;
}
}
MainActivity类:
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
Menu menu;
private Toolbar toolbar;
private LinearLayoutManager layoutManager;
private static RecyclerView recyclerView;
private Firebase ref;
private String res;
private DatabaseReference mDatabase;
private FirebaseRecyclerAdapter<DataModel, MessageViewHolder> mFirebaseAdapter;
public static class MessageViewHolder extends RecyclerView.ViewHolder {
View mViews;
CircleImageView img;
TextView name,data;
public MessageViewHolder(View views) {
super(views);
mViews = views;
}
public void setText(String Name) {
TextView name = (TextView) mViews.findViewById(R.id.Name);
name.setText(Name);
}
public void setData(String Data) {
TextView data = (TextView) mViews.findViewById(R.id.datas);
data.setText(Data);
}
public void setImage(Context cnt, String imageUrl) {
CircleImageView img = (CircleImageView) mViews.findViewById(R.id.image);
Picasso.with(cnt).load(imageUrl).into(img);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainactivity);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Firebase.setAndroidContext(this);
recyclerView = (RecyclerView) findViewById(R.id.recycler);
recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(this);
ref = new Firebase("https://hi-34.firebaseio.com/messages/matt__walk");
mFirebaseAdapter = new FirebaseRecyclerAdapter<DataModel, MessageViewHolder>(
DataModel.class,
R.layout.rowplaces,
MessageViewHolder.class,
ref
) {
@Override
protected void populateViewHolder(MessageViewHolder messageViewHolder, DataModel dataModel, int i) {
messageViewHolder.setText(dataModel.getUsername());
messageViewHolder.setData(dataModel.getText());
messageViewHolder.setImage(getApplicationContext(),dataModel.getImageUrl());
}
};
mFirebaseAdapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
@Override
public void onItemRangeInserted(int positionStart, int itemCount) {
super.onItemRangeInserted(positionStart, itemCount);
int friendlyMessageCount = mFirebaseAdapter.getItemCount();
int lastVisiblePosition = layoutManager.findLastCompletelyVisibleItemPosition();
if (lastVisiblePosition == -1 ||
(positionStart >= (friendlyMessageCount - 1) &&
lastVisiblePosition == (positionStart - 1))) {
recyclerView.scrollToPosition(positionStart);
}
}
});
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(mFirebaseAdapter);
}
答案 0 :(得分:0)
Recycler视图(和列表视图)显示项目列表。 FirebaseRecyclerAdapter
用于填充此类项目列表。
如果您在节点上添加FirebaseRecyclerAdapter
,它会尝试将该节点下的节点添加到Recycler视图中。因此,在您的情况下,这意味着它会尝试将Username
,imageurl
和text
添加到列表中。这似乎不是你想到的。要解决此问题,您需要使适配器在树中听一级:
ref = new Firebase("https://hi-34.firebaseio.com/messages");
mFirebaseAdapter = new FirebaseRecyclerAdapter<DataModel, MessageViewHolder>(
DataModel.class,
R.layout.rowplaces,
MessageViewHolder.class,
ref
) {
这样,它会为populateViewHolder
的每个孩子拨打messages
,其中第一个/唯一一个是matt__walk
。
第二个问题是,JSON中属性名称的大小写与Java类中相应的getter不匹配。
如果使用:
public class DataModel {
private String username;
private String imageUrl;
private String text;
public DataModel(){
}
public String getUsername() {
return Username;
}
public String getText() {
return text;
}
public String getImageUrl() {
return imageurl;
}
}
然后:
matt__walk": {
"username": "matt__walk",
"imageUrl": "https://firebasestorage.googleapis.com/v0",
"text": "hii"
}