如何修复代码以显示Firebase中的数据?

时间:2019-04-02 19:45:55

标签: java android firebase firebase-realtime-database android-recyclerview

我已经问过类似的问题,当我单击textview(header_news from firebase)时,我切换到另一个活动并从Firebase加载数据。在另一项活动中,我得到了相同的数据(id为1条新闻)。但我想显示main_news。我知道问题出在哪里,但我不知道如何解决。我认为bindviewholder中的问题是因为我没有添加myViewHolder.main_news.setText(Mnews.get(i).getMain_news());,但是我无法添加,因为它显示在标题新闻中。...P.S对不起,我的英文希望您能理解我。

适配器代码:

    public class NewsAdapter extends RecyclerView.Adapter<NewsAdapter.MyViewHolder> {

    Context Mcontext;
    ArrayList<News> Mnews;


    public NewsAdapter(Context Mcontext, ArrayList<News> Mnews)
    {
        this.Mnews = Mnews;
        this.Mcontext = Mcontext;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new MyViewHolder(LayoutInflater.from(Mcontext).inflate(R.layout.news_view, viewGroup, false));
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, int i) {
        final News news = Mnews.get(i);
        myViewHolder.news_date.setText(Mnews.get(i).getDatenews());
        myViewHolder.news_header.setText(Mnews.get(i).getHeader_news());

        Picasso.get().load(Mnews.get(i).getImageURL()).into(myViewHolder.news_picture);

        myViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent(Mcontext, NewsActivity.class);
                intent.putExtra("idnews", news.getIdnews());
                Mcontext.startActivity(intent);

            }
        });

    }

    @Override
    public int getItemCount() {
       return Mnews.size();
   }

    class MyViewHolder extends RecyclerView.ViewHolder {

        TextView news_date, news_header, main_news;
        ImageView news_picture;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);

            news_date = (TextView) itemView.findViewById(R.id.news_date);
            news_header = (TextView) itemView.findViewById(R.id.news_header);
            news_picture = (ImageView) itemView.findViewById(R.id.news_picture);
        }
    }
}

活动代码:

    public class NewsActivity extends AppCompatActivity {
    String idnews;
    TextView main_news;
    DatabaseReference reference;
    TextView news_date;
    RecyclerView recyclerView;
    Intent intent;
    ArrayList<News> Nlist;
    NewsAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_news);

        recyclerView = findViewById(R.id.recycler_view);
        recyclerView.setHasFixedSize(true);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getApplicationContext());
        linearLayoutManager.setStackFromEnd(true);
        recyclerView.setLayoutManager(linearLayoutManager);
        main_news = findViewById(R.id.main_news);
        intent = getIntent();
        idnews = intent.getStringExtra("idnews");

        Nlist = new ArrayList<News>();
        reference = FirebaseDatabase.getInstance().getReference("News").child(idnews);
        reference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                Nlist.clear();
                News news = dataSnapshot.getValue(News.class);
                main_news.setText(news.getNews_main());
                Nlist.add(news);

                adapter = new NewsAdapter(getApplicationContext(), Nlist);
                recyclerView.setAdapter(adapter);

            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
                Toast.makeText(NewsActivity.this, "Что то не так", Toast.LENGTH_SHORT).show();
            }
        });
    }
}

My database struct

希望你能帮助我。

0 个答案:

没有答案