0

I am adding data into an addToSepetims arraylist and I want to use it in another fragment but I cant get this arraylist


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

    ArrayList<product_bilgileri> product_bilgileris = new ArrayList<product_bilgileri>();

    LayoutInflater layoutInflater;
    Context context;
    ArrayList<AddToSepetim> addToSepetims = new ArrayList<AddToSepetim>();


    public productAdapter(ArrayList<product_bilgileri> product_bilgileris, Context context) {
        this.product_bilgileris = product_bilgileris;
        this.context = context;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        holder.productName.setText(product_bilgileris.get(position).getProductname());
        holder.description.setText(product_bilgileris.get(position).getDescription());
        holder.imageView.setImageDrawable(context.getResources().getDrawable(product_bilgileris.get(position).getImage()));
        holder.ratingBar.setRating((product_bilgileris.get(position).getRating()));
        holder.button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String a =holder.productName.getText().toString();
                String b =holder.description.getText().toString();
                int c = R.drawable.iphone5;
                //Toast.makeText(context, ""+a, Toast.LENGTH_SHORT).show();
                addToSepetims.add(new AddToSepetim(a,b,c));


            }
        });
    }

This is Where I want to use addToSepetims arraylist :



public class SepetimFragment extends Fragment {


    RecyclerView recyclerView;
    ArrayList<AddToSepetim> addToSepetims = new ArrayList<AddToSepetim>();
    public SepetimFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_sepetim, container, false);
        recyclerView = view.findViewById(R.id.products_recylerview);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setReverseLayout(true);
        layoutManager.setStackFromEnd(true);
        recyclerView.setLayoutManager(layoutManager);
        SepetimAdapter sepetimAdapter = new SepetimAdapter(addToSepetims,getActivity());
        recyclerView.setAdapter(sepetimAdapter);
        return view;
    }
}

I have also SepetimAdapter Class

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

    ArrayList<AddToSepetim> addToSepetims = new ArrayList<AddToSepetim>();
    LayoutInflater layoutInflater;

    public SepetimAdapter(ArrayList<AddToSepetim> addToSepetims, Context context) {
        this.addToSepetims = addToSepetims;
        this.context = context;
    }

    Context context;

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        layoutInflater = LayoutInflater.from(context);
        View view = layoutInflater.inflate(R.layout.row_product_sepetim,parent,false);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        holder.productName.setText(addToSepetims.get(position).getProductname());
        holder.description.setText(addToSepetims.get(position).getDescription());
        holder.imageView.setImageDrawable(context.getResources().getDrawable(addToSepetims.get(position).getImage()));
        holder.button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                addToSepetims.remove(addToSepetims.get(position));
            }
        });
    }

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


    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView productName,description;
        ImageView imageView;
        Button button;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            productName = itemView.findViewById(R.id.product_Name);
            description = itemView.findViewById(R.id.descripton);
            imageView = itemView.findViewById(R.id.product_pic);
            button  = itemView.findViewById(R.id.deleteFromCard);
        }
    }
}


Second QUESTION :

Product adapter class in onBindViewHolder class :

int c = R.drawable.iphone5;

in this line I want to get holder.imageView instead of "R.drawable.iphone5" How can I do that ?

fatihmrcn
  • 13
  • 1
  • 7

1 Answers1

0

You can share data between fragments through a ViewModel:

  1. Add dependency in build.gradle (Module:app) level.
    implementation "androidx.lifecycle:lifecycle-viewmodel:2.2.0"
    implementation "android.arch.lifecycle:runtime:2.2.0"
  1. Create a custom ViewModel class that extends from AndroidViewModel

Here added the list you want to be shared between fragments.

public class MainViewModel extends AndroidViewModel {

    private ArrayList<product_bilgileri> products;

    public TempViewModel(@NonNull Application application) {
        super(application);
    }

    public ArrayList<product_bilgileri> getProducts() {
        return products;
    }

    public void setProducts(ArrayList<product_bilgileri> products) {
        this.products = products;
    }

}
  1. Instantiate the ViewModel instance in activity/fragment
// In activity 
MainViewModel mViewModel = new ViewModelProvider(this).get(MainViewModel.class);

// In fragment
MainViewModel mViewModel = new ViewModelProvider(requireActivity()).get(MainViewModel.class);

Then set the list of data with mViewModel.setProducts(myList) and retrieve it with mViewModel.getProducts()

For more info, check the documentation. Also check this article.

Zain
  • 37,492
  • 7
  • 60
  • 84
  • Can I use products.add() inside of a fragment ? – fatihmrcn Jan 08 '21 at 22:50
  • @fatihmrcn you can make it like `mViewModel.getProducts().add()` – Zain Jan 08 '21 at 22:51
  • I am trying to use it in adapter class like this : ```MainViewModel mViewModel = new ViewModelProvider((ViewModelStoreOwner) context).get(MainViewModel.class);``` and I get null pointerexception – fatihmrcn Jan 08 '21 at 23:25
  • You can't use it in adapter .. only lifecycle owners (activities and fragments) .. you can use a listener instead between the adapter and the fragment – Zain Jan 08 '21 at 23:26
  • If you check my productAdapter class you can find the ``` addToSepetims.add(new AddToSepetim(a,b,c)); ``` and I want to usee that addToSepetims arraylist in another fragment but I am filling addToSepetim in a adapter thats why I cant reach the data (addToSepetim filled data) from another fragment or activtiy but I want to reach it to use because I have another recylerview and its recyling addToSepetim arraylist – fatihmrcn Jan 08 '21 at 23:35
  • @fatihmrcn I recommend you to send the updated list using a listener interface from the adapter back to the activity/fragment that instantiated it and update the `ViewModel` list from this activity/fragment... Using `ViewModel` in adapters is not recommended .. please check [here](https://stackoverflow.com/questions/61364874/view-models-for-recyclerview-items) – Zain Jan 08 '21 at 23:44
  • Thank you for your help but I am afraid there is no way to do that :(a – fatihmrcn Jan 09 '21 at 00:02