-1

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();
        }
    };
Level_Up
  • 789
  • 5
  • 19
  • Integer.parseInt(myStringValue); – Stultuske Jun 02 '20 at 07:35
  • Im sorry my bad. I forgot to put a question. Im new to stackoverflow. Is there any possibility to somehow change tha path to int withou changing the code entirely? parsing the int as mentioned in one of the comments does not work. I tried. – Tomáš Várady Jun 02 '20 at 07:40
  • what is the value of your String? – Stultuske Jun 02 '20 at 07:43
  • 1
    You can update your description adding your question. This way it will be easier to read and no need to scroll down to see the question in the comments. – Level_Up Jun 02 '20 at 07:46
  • Change the constructor of your `SliderBN` class to receive a `String` and inside the constructor, extract the number of the image from that `String`. Having a class just to save an `int` doesn't make any sense. With this change, having a class would make a little more sense. – SuperG280 Jun 02 '20 at 07:55
  • well actually it does make sense because Ive used it once with it returning int and it worked flawlessly – Tomáš Várady Jun 03 '20 at 09:20

1 Answers1

1

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!

Level_Up
  • 789
  • 5
  • 19
  • thank you for the answer but the problem persists because the class ImageView that Im using is asking for method getImage which returns int so I cannot use method getPath which returns String. Can you please help me to sort it? I can post the whole code here – Tomáš Várady Jun 03 '20 at 08:54
  • Ok I see what you mean.I didn't know about the API that require int (resource id). I check out now the documentation and found this method: setImageURI(Uri uri) -> https://developer.android.com/reference/android/widget/ImageView#setImageURI(android.net.Uri) It requires `Uri` so please test it. I also update my code on the point where you used this method. If you have some problems check how to find file (in your case image) with Uri in Android or write again here. Hope it helps. – Level_Up Jun 03 '20 at 10:17
  • I also add File you can see in the example above how to use it. – Level_Up Jun 03 '20 at 10:36
  • Also possible solution can be this two methods: https://developer.android.com/reference/android/widget/ImageView#setImageDrawable(android.graphics.drawable.Drawable) and also https://developer.android.com/reference/android/widget/ImageView#setImageBitmap(android.graphics.Bitmap) Just search an example how to use them to show local image – Level_Up Jun 03 '20 at 10:40
  • thank you, worked out flawlessly but I just needed to delete the `for loop` out of the sliderBN list and its working flawlessly right now – Tomáš Várady Jun 03 '20 at 11:50
  • Ok, happy to help. – Level_Up Jun 03 '20 at 12:19