1

I have data taken from an API, I have added a button that when pressed will move (title, image, and some data from FRAGMENT to other FRAGMENT), this data is taken from RECYCLERLVIEW and will be shown in RECYCLERVIEW. But when the button is pressed, nothing happens and the data is not transmitted.

The project link if you want to take a deeper look "Mox Project"


Here's the first adapter that I send data from

--- CNNAdapter ---

private List<Item> items = null;

@SuppressLint("NotifyDataSetChanged")
public void setCNNList(List<Item> items) {
    this.items = items;
    notifyDataSetChanged();
}

@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    return new NewsViewHolder(
            LayoutInflater.from(parent.getContext()).inflate(
                    R.layout.list_news,
                    parent,
                    false
            )
    );
}

@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
    Item News = items.get(position);
    ((NewsViewHolder) holder).setData(News);
    ((NewsViewHolder) holder).sendData(News);
}

@Override
public int getItemCount() {
    if (items == null) return 0;
    return items.size();
}

static class NewsViewHolder extends RecyclerView.ViewHolder {
    private final ImageView image;
    private final TextView title;
    private final TextView description;
    private final TextView source;
    private final TextView author;
    private final Button sendDataButton;
    private final Activity activity = new Activity();

    NewsViewHolder(@NonNull View itemView) {
        super(itemView);
        image = itemView.findViewById(R.id.image_news);
        title = itemView.findViewById(R.id.title_news);
        description = itemView.findViewById(R.id.description_news);
        source = itemView.findViewById(R.id.source_news);
        author = itemView.findViewById(R.id.author_name_news);
        sendDataButton = itemView.findViewById(R.id.button_save);
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    void setData(Item news) {
        Glide.get(itemView.getContext()).clearMemory();
        // load images in MainThread
        activity.runOnUiThread(() -> {
            Glide.with(itemView.getContext())
                    .load((Objects.requireNonNull(news.getEnclosure())).getLink())
                    .diskCacheStrategy(DiskCacheStrategy.NONE)
                    .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
                    .skipMemoryCache(true)
                    .transition(DrawableTransitionOptions.withCrossFade())
                    .into(image);

            Glide.get(itemView.getContext()).clearMemory();
        });

        title.setText(news.getTitle());
        description.setText(Html.fromHtml(Objects.requireNonNull(news.getDescription()).replaceAll("(<(/)img>)|(<img.+?>)", ""), Html.FROM_HTML_MODE_COMPACT));
        source.setText(R.string.cnn);
        author.setText(R.string.author);
    }

    void sendData(Item news) {
        sendDataButton.setOnClickListener(v -> {
            Bundle bundle = new Bundle();
            bundle.putString("title", news.getTitle());
            bundle.putString("image", Objects.requireNonNull(news.getEnclosure()).getLink());
            bundle.putString("source", "CNN");
            bundle.putString("author", "CNN editor's");
            bundle.putString("link", news.getLink());
            FavouriteFragment favouriteFragment = new FavouriteFragment();
            favouriteFragment.setArguments(bundle);
        });
    }
}

Here's the second adapter that I get the data and set it

--- FavoriteAdapter ---

public class FavoriteAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

private ArrayList<Item> items = new ArrayList<>();;
private Context context;

@SuppressLint("NotifyDataSetChanged")
public void setFavoriteList(ArrayList<Item> items, Context context) {
    this.items = items;
    this.context = context;
}

@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    return new FavoriteViewHolder(
            LayoutInflater.from(parent.getContext()).inflate(
                    R.layout.list_save,
                    parent,
                    false
            )
    );
}

@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
    ((FavoriteViewHolder) holder).setData();
}

@Override
public int getItemCount() {
    if (items == null) return 0;
    return items.size();
}

static class FavoriteViewHolder extends RecyclerView.ViewHolder {
    private final ImageView image;
    private final TextView title;
    private final TextView source;
    private final TextView author;
    private final Activity activity = new Activity();

    FavoriteViewHolder(@NonNull View itemView) {
        super(itemView);
        image = itemView.findViewById(R.id.image_article_save);
        title = itemView.findViewById(R.id.title_article_save);
        source = itemView.findViewById(R.id.source_article_save);
        author = itemView.findViewById(R.id.author_name_article_save);
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    void setData() {
        FavouriteFragment favouriteFragment = new FavouriteFragment();
        Bundle bundle = favouriteFragment.getArguments();
        String getTitle = bundle.getString("title");
        String getImage = bundle.getString("image");
        String getSource = bundle.getString("source");
        String getAuthor = bundle.getString("author");
        String getLink = bundle.getString("link");

        Glide.get(itemView.getContext()).clearMemory();
        // load images in MainThread
        activity.runOnUiThread (() -> {
            Glide.with(itemView.getContext())
                    .load((getImage))
                    .diskCacheStrategy(DiskCacheStrategy.NONE)
                    .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
                    .skipMemoryCache(true)
                    .transition(DrawableTransitionOptions.withCrossFade())
                    .into(image);

            Glide.get(itemView.getContext()).clearMemory();
        });

        title.setText(getTitle);
        source.setText(getSource);
        author.setText(getAuthor);
    }
}


My fragment that I display the data using recyclerview

--- FavouriteFragment ---

public class FavouriteFragment extends Fragment  {

private FragmentFavouriteBinding binding;
ArrayList<Item> arrayList = new ArrayList<>();;

@Override
public View onCreateView(@NotNull LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    binding = FragmentFavouriteBinding.inflate(getLayoutInflater());
    View view = binding.getRoot();
    requireActivity().setTitle("");
    initializeViews();
    return view;
}

private void initializeViews() {
    FavoriteAdapter adapter = new FavoriteAdapter();
    binding.recyclerViewFavorites.setLayoutManager(new LinearLayoutManager(requireContext()));
    binding.recyclerViewFavorites.setHasFixedSize(true);
    binding.recyclerViewFavorites.setAdapter(adapter);
    adapter.setFavoriteList(arrayList,getContext());
}


The project link if you want to take a deeper look "Mox Project"

Thank you for your help.

Moataz
  • 495
  • 3
  • 20

2 Answers2

0

The displaying of Fragments is managed by FragmentManager. You must pass an instance of FragmentManager to CNNAdapter as follows:

public class CNNAdapter extends RecyclerView.Adapter<CNNAdapter.NewsViewHolder> {

    private final FragmentManager mFragmentManager;

    public CNNAdapter(FragmentManager mFragmentManager) {
        this.mFragmentManager = mFragmentManager;
    }

    public void addData(List<> items) {
        list.addAll(items);
        notifyDataSetChanged();
    }
}

And then you display the fragment as follows:

FavouriteFragment favouriteFragment = new FavouriteFragment();
favouriteFragment.setArguments(bundle);
mFragmentManager.beginTransaction().replace(R.id.fragment_container,favouriteFragment).commit();

R.id.fragment_container is the view ID of the container in the layout xml of the activity that hosts the fragment. The container is usually a FrameLayout.

And then you instantiate CNNAdapter in the Fragment like this:

CNNAdapter cnnAdapter = new CNNAdapter(getFragmentManager());
codeman
  • 144
  • 7
  • Thank you for your help. If you see my code you will find that I made all of that before. I tried your FragmentManger and It is not working. – Moataz Aug 11 '21 at 13:34
  • Here's my project link, If you want to take a deeper look https://github.com/MoatazBadawy/MOX – Moataz Aug 11 '21 at 14:17
  • There was a small bug in the code. I forgot to call the commit() method to commit the fragment transaction. It should be like this `mFragmentManager.beginTransaction().replace(R.id.fragment_container,favouriteFragment).commit();` – codeman Aug 13 '21 at 15:27
0

Possible problems or issues that may cause the button not to show the passed data. remember a view will only display when it gets data. Here are some resources that might help:

Dr Mido
  • 2,414
  • 4
  • 32
  • 72
Mary Jones
  • 99
  • 3