There are 2 fragments. The first starts the service that connects to the Internet, reads the necessary data and sends it to another fragment by Broadcast. In the adapter I wrote a method that will add new elements to the list and notify that the data has changed. But from the fragment, for some reason I do not see it. How to be? And how good is a good idea to transmit data by Broadcast?

public class CardsWeatherAdapter extends RecyclerView.Adapter<CardsWeatherAdapter.ViewHolder> { private List<CardObject> mCards = new ArrayList<>(); // этот метод должен вызываться с фрагмента public void addCard(CardObject cardObject){ mCards.add(cardObject); notifyDataSetChanged(); } static class ViewHolder extends RecyclerView.ViewHolder { TextView celsius; TextView fahrenheit; TextView suggestion; TextView aqi; TextView cityName; ImageView cityPicture; ImageView aqiColor; public ViewHolder(View v) { super(v); celsius = (TextView) v.findViewById(R.id.celsius); fahrenheit = (TextView) v.findViewById(R.id.fahrenheit); suggestion = (TextView) v.findViewById(R.id.suggestion); aqi = (TextView) v.findViewById(R.id.aqi_value); cityName = (TextView) v.findViewById(R.id.city_name); cityPicture = (ImageView) v.findViewById(R.id.city_picture); aqiColor = (ImageView) v.findViewById(R.id.aqi_color); } } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_item, parent, false); return new ViewHolder(view); } @Override public void onBindViewHolder(ViewHolder holder, int position) { } @Override public int getItemCount() { return mCards.size(); } 

}

 public class CardsFragment extends Fragment { public static final String ACTION = "action"; private RecyclerView.Adapter mAdapter; private BroadcastReceiver mReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { mAdapter. // здесь хочу вызвать метод addCard(), но не получается. } }; @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); IntentFilter filter = new IntentFilter(ACTION); LocalBroadcastManager.getInstance(getContext()).registerReceiver(mReceiver, filter); } @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_cards, container, false); initView(view); return view; } 
  • Where is the code? Only you know how the method is declared, and why it is not visible from the fragment. Do not ask 2 questions in one, it is harmful for the structure of this forum. If you are interested in whether it is good to transmit data by Broadcast, ask a question separately. - Vladyslav Matviienko
  • Corrected the post by adding the code - Alexander Borisov

1 answer 1

Your adapter is declared as

  private RecyclerView.Adapter mAdapter; 

Obviously, the RecyclerView.Adapter does not have an addCard() method. He is at CardsWeatherAdapter .

If it is easier to explain, then the RecyclerView.Adapter is a Mammal, and the CardsWeatherAdapter is a Cat, which is inherited from a Mammal. And you declared a variable of type Mammal, and now you are trying to call the мурлыкать() method in it. But the mammal does not know how to purr! You have declared this method only on Cats. To call this method, either declare a field like this:

 private CardsWeatherAdapter mAdapter; 

Or, explicitly bring the adapter to the CardsWeatherAdapter :

 ((CardsWeatherAdapter)mAdapter).addCard(); 
  • Do you happen to be a teacher?) You are great at explaining)) Thank you! - Alexander Borisov
  • @AlexanderBorisov, no, but I once wanted to teach. I understood in time that I am not ready to sacrifice a good salary in the name of science ... - Vladyslav Matviienko