Search code examples
javastringandroid-studiointandroid-viewpager2

How can I solve this String and int problem?


You see I have a problem: Is there any possibility to somehow change that path to int without changing the code entirely?

Im using android studio and currently developing app but Im strugling with one thing.

I have a String path of image which needs to be int for it to work.

String path1 = Environment.getDataDirectory().getAbsolutePath() + "/data/sk.lotario.bg/app_imgBuns/0.png";
String path2 = Environment.getDataDirectory().getAbsolutePath() + "/data/sk.lotario.bg/app_imgBuns/1.png";


    List<SliderBN> sliderBNS = new ArrayList<>();
    for(int i = 0; i < Recipe.bnPos; i++){
        sliderBNS.add(new SliderBN(path1));
        sliderBNS.add(new SliderBN(path2));

In class SliderBN I have this:

public class SliderBN {

    private int image;

    SliderBN(int image){
        this.image = image;
    }

    public int getImage() {
        return image;
    }

Problem is I dont want to change it here to String cause then I would have to change a lot of code.

Here is adapter to that class:


    private List<SliderBN> sliderBNS;
    private ViewPager2 vpBN;
    private static final String TAG = "SliderBNAdapter";
    public static int vpBNPosition = 0;

    public SliderBNAdapter(List<SliderBN> sliderBNS, ViewPager2 vpBN) {
        this.sliderBNS = sliderBNS;
        this.vpBN = vpBN;
    }

    public SliderBNAdapter() {

    }

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

    @Override
    public void onBindViewHolder(@NonNull SliderViewHolder holder, int position) {
        holder.setImage(sliderBNS.get(position));
        if (position == sliderBNS.size()) {
            vpBN.post(runnable);
        }
    }



    @Override
    public int getItemCount() {
        vpBNPosition = vpBN.getCurrentItem();
        Log.i(TAG,"position "+vpBNPosition);
        return sliderBNS.size();
    }

    class SliderViewHolder extends RecyclerView.ViewHolder {

        private ImageView imageView;

        SliderViewHolder(@NonNull View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.imageSlide);
        }
        void setImage(SliderBN sliderBN){
            imageView.setImageResource(sliderBun.getImage());
        }
    }

    private Runnable runnable = new Runnable() {
        @Override
        public void run() {
            sliderBNS.addAll(sliderBNS);
            notifyDataSetChanged();
        }
    };

Solution

  • You can add to the SliderBN two properties. One is String path and the other is int index. When you create SliderBN you can just set the values that you want. Then inside your code you can use the property which you need.

    public class SliderBN {
    
        private int index;
        private String path;
    
        SliderBN(String path, int index){
            this.index = index;
            this.path = path;
        }
    
        public int getIndex() {
            return index;
        }
    
        public String getPath() {
            return path;
        }
    }
    

    In the beginning when you create image you can do:

    String pathToImages = Environment.getDataDirectory().getAbsolutePath() + "/data/sk.lotario.bg/app_imgBuns/";
    
    List<SliderBN> sliderBNS = new ArrayList<>();
    // Here you need to be sure that you always have the images else you will 
    // receive exceptions! If this images will be dynamically added/removed then 
    // you need another way for retrieving the names and loading them in the list.
    for(int i = 0; i < Recipe.bnPos; i++){
        sliderBNS.add(new SliderBN(pathToImages + i + ".png", i));
    }
    

    For example where you need to set the image path you can just call getPath method:

    class SliderViewHolder extends RecyclerView.ViewHolder {
    
        private ImageView imageView;
    
        SliderViewHolder(@NonNull View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.imageSlide);
        }
        void setImage(SliderBN sliderBN){
            // Here you can set the path to the image
            // I saw that you setImageResource(int resId)
            // But for this to work you need the Image ids
            // In the current case we can use setImageURI(Uri uri)
            // With Uri you need to provide the path to the image not the id
    
             File imgFile = new File(sliderBun.getPath());
             if(imgFile.exists())
             {
                 Uri uri = Uri.fromFile(imgFile);
                 imageView.setImageURI(uri);
             }
             else
             {
                 // Log here message that the file does not exist
                 // and also log the path so you can see what is going wrong
             }
        }
    }
    

    NOTE: As your code is right now you do not need index (int) in class SliderBN because you have this information in the list where you add all images in the beginning. Still I don't know all of your use-cases that why I left it. See example of using index of the list to get the Object on that position:

    @Override
    public void onBindViewHolder(@NonNull SliderViewHolder holder, int position) 
    {
        // Here we get SliderBN object from the list by list position and not by 
        // internal properties
        holder.setImage(sliderBNS.get(position));
        if (position == sliderBNS.size()) {
            vpBN.post(runnable);
        }
    }
    

    One place where you will need index is if you have List with unsorted SliderBN objects and you need easily to sort them. Then you can use this property index to sort the objects in that List. But in your case you create the images from growing index inside of for loop so your list actually is sorted.

    Good luck to all!