There is a fragment in which I load the data from the firebase. The parent directory is called "News". He has a "child" news1, news2, newsn .. They contain: imageURL (News picture), news_header (News headline), newsdate (Date of news) and also child news_main (Main news) In It contains imageURL and news_text. I need to, when I click on the news, open a new activation and load info about this news in it. That is news_main. I do not use a unique key. Headline news, etc. loaded in recyclerview. Can anyone come across this. How to be?

public class NewsAdapter extends RecyclerView.Adapter<NewsAdapter.MyViewHolder> { Context context; ArrayList<News> news; public NewsAdapter(Context c, ArrayList<News> n) { context = c; news = n; } @NonNull @Override public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) { return new MyViewHolder(LayoutInflater.from(context).inflate(R.layout.news_view, viewGroup, false)); } @Override public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, int i) { myViewHolder.news_date.setText(news.get(i).getDatenews()); myViewHolder.news_header.setText(news.get(i).getHeader_news()); Picasso.get().load(news.get(i).getImageURL()).into(myViewHolder.news_picture); } @Override public int getItemCount() { return news.size(); } class MyViewHolder extends RecyclerView.ViewHolder { TextView news_date, news_header; 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); intent = getIntent(); idnews = intent.getStringExtra("idnews"); reference = FirebaseDatabase.getInstance().getReference("News").child(idnews); reference.addValueEventListener(new ValueEventListener() { @Override public void onDataChange(@NonNull DataSnapshot dataSnapshot) { // for (DataSnapshot snapshot : dataSnapshot.getChildren()){ News news = dataSnapshot.getValue(News.class); // news_date.setText(news.getDatenews()); 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(); } }); } 

}

enter image description here

Id transfer code:

 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); } }); 
  • attach a piece of code from your adapter - Rasul Ave
  • Adapter code attached. - Domex 3:49 pm

1 answer 1

in the viewholder constructor add itemView.setOnClickListener(this) . In OnClick(View view) open a new activity . If you do not use identifiers, and the data does not change in you, you can pass the position of the news as a key by taking it through the getAdapterPosition() method. But it is better, of course, to use unique identifiers. Or send the news directly to a new activity

  • In general, I wrote the code: From the adapter, I transfer the key value, and in the activity I process it. I'm just wondering if I can set a unique identifier using RANDOM-KEY? - Domex pm
  • Because something tells me that just id as in sql db does not roll - Domex
  • Just lay out the code. - Domex 8:01 pm
  • If there is not a lot of news, set as random key - Alexandr Motorin
  • In general, collected, launched. When I click on the news, I get an error. The error code refers to this line: main_news.setText (news.getNews_main ()); - Domex