我正在尝试制作一个新闻应用程序,Firebase数据库和Recyclerview来向用户显示网站的新闻提要,以及一个webview
来查看该网站。我已经将每个细节添加到数据库中,但是我的问题是它没有显示在我的home
活动中。
这是home
类:
public class home extends AppCompatActivity implements PopupMenu.OnMenuItemClickListener {
RecyclerView.LayoutManager layoutManager;
private RecyclerView mPeopleRV;
SpotsDialog dialog;
DatabaseReference mDatabase;
SwipeRefreshLayout swipeLayout;
private FirebaseRecyclerAdapter<News, home.NewsViewHolder> mPeopleRVAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
mDatabase = FirebaseDatabase.getInstance().getReference().child("News");
mDatabase.keepSynced(true);
mPeopleRV = (RecyclerView) findViewById(R.id.news_home);
DatabaseReference personsRef = FirebaseDatabase.getInstance().getReference().child("News");
Query personsQuery = personsRef.orderByKey();
mPeopleRV.hasFixedSize();
mPeopleRV.setLayoutManager(new LinearLayoutManager(this));
FirebaseRecyclerOptions personsOptions = new FirebaseRecyclerOptions.Builder<News>().setQuery(personsQuery, News.class).build();
mPeopleRVAdapter = new FirebaseRecyclerAdapter<News, home.NewsViewHolder>(personsOptions) {
@Override
protected void onBindViewHolder(home.NewsViewHolder holder, final int position, final News model) {
holder.setNewsimage(getBaseContext(), model.getNewsimage());
holder.setTitle(model.getTitle());
holder.setNewssource(model.getNewssource());
holder.setDescription(model.getDescription());
holder.mView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String url = model.getUrl();
Intent intent = new Intent(getApplicationContext(), newswebview.class);
intent.putExtra("id", url);
startActivity(intent);
}
});
}
@Override
public home.NewsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.news, parent, false);
return new home.NewsViewHolder(view);
}
};
mPeopleRV.setAdapter(mPeopleRVAdapter);
}
@Override
public void onStart() {
super.onStart();
mPeopleRVAdapter.startListening();
}
@Override
public void onStop() {
super.onStop();
mPeopleRVAdapter.stopListening();
}
public static class NewsViewHolder extends RecyclerView.ViewHolder{
View mView;
public NewsViewHolder(View itemView){
super(itemView);
mView = itemView;
}
public void setNewsimage(Context ctx, String image){
ImageView post_image = mView.findViewById(R.id.newsimage);
Picasso.with(ctx).load(image).into(post_image);
}
public void setTitle(String title){
TextView post_title = mView.findViewById(R.id.newsTitle);
post_title.setText(title);
}
public void setNewssource(String source){
TextView post_desc = mView.findViewById(R.id.newsSource);
post_desc.setText(source);
}
public void setDescription(String desc){
TextView post_desc = mView.findViewById(R.id.newsDesc);
post_desc.setText(desc);
}
}
这是xml
文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/swipeRefresh"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".home">
<ImageView
android:id="@+id/home_menu"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_marginEnd="10dp"
android:layout_marginTop="10dp"
android:baselineAligned="false"
android:onClick="showPopup"
android:src="@drawable/ic_more_vert_black_24dp" />
<android.support.v7.widget.RecyclerView
android:id="@+id/news_home"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:layout_marginTop="5dp"
android:padding="10dp" />
</LinearLayout>
@alexmamo这是新的错误
E/AndroidRuntime: FATAL EXCEPTION: main
Process: app.ayodkay.com.ajudeme, PID: 27305
com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type app.ayodkay.com.ajudeme.classes.News
at com.google.firebase.database.obfuscated.zzbw.zzb(com.google.firebase:firebase-database@@16.0.2:2423)
at com.google.firebase.database.obfuscated.zzbw.zza(com.google.firebase:firebase-database@@16.0.2:79)
at com.google.firebase.database.DataSnapshot.getValue(com.google.firebase:firebase-database@@16.0.2:214)
at com.firebase.ui.database.ClassSnapshotParser.parseSnapshot(ClassSnapshotParser.java:29)
at com.firebase.ui.database.ClassSnapshotParser.parseSnapshot(ClassSnapshotParser.java:15)
at com.firebase.ui.common.BaseCachingSnapshotParser.parseSnapshot(BaseCachingSnapshotParser.java:35)
at com.firebase.ui.common.BaseObservableSnapshotArray.get(BaseObservableSnapshotArray.java:52)
at com.firebase.ui.database.FirebaseRecyclerAdapter.getItem(FirebaseRecyclerAdapter.java:106)
at com.firebase.ui.database.FirebaseRecyclerAdapter.onBindViewHolder(FirebaseRecyclerAdapter.java:122)
at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6673)
at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6714)
at android.support.v7.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5647)
at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5913)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5752)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5748)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2232)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1559)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1519)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:614)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3812)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3529)
at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:4082)
at android.view.View.layout(View.java:16000)
at android.view.ViewGroup.layout(ViewGroup.java:5228)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
at android.view.View.layout(View.java:16000)
at android.view.ViewGroup.layout(ViewGroup.java:5228)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
at android.view.View.layout(View.java:16000)
at android.view.ViewGroup.layout(ViewGroup.java:5228)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
at android.view.View.layout(View.java:16000)
at android.view.ViewGroup.layout(ViewGroup.java:5228)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
at android.view.View.layout(View.java:16000)
at android.view.ViewGroup.layout(ViewGroup.java:5228)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
at android.view.View.layout(View.java:16000)
at android.view.ViewGroup.layout(ViewGroup.java:5228)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
at android.view.View.layout(View.java:16000)
at android.view.ViewGroup.layout(ViewGroup.java:5228)
at android.view.ViewRootImpl.perf
答案 0 :(得分:2)
问题是您想念一个孩子。要解决这个问题,在向数据库中添加数据时应使用push()
方法,如下所示:
mDatabase = FirebaseDatabase.getInstance().getReference().child("News").push();
mDatabase.setValue(/* yourObject */);
那么您下面的查询将起作用:
DatabaseReference personsRef = FirebaseDatabase.getInstance().getReference().child("News");
Query personsQuery = personsRef.orderByKey();