1

I'm doing sliding image using SliderLayout. By default change the image every 10 seconds.If user wants to change time interval, creating interval.txt file in device local memory and getting interval time from that file. Now i'm getting problem is that if I change interval time in interval.txt its taking old time interval. Can you please guide me ,how to resolve this problem.

protected void onCreate(Bundle savedInstanceState) {
 writeFile();
 ImageSlider();
}
public void writeFile(){
    try {
        BufferedWriter bw = new BufferedWriter(new FileWriter(txtFile));
        bw.write("10000");
        bw.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
private void ImageSlider() {

    sliderLayout = (SliderLayout)findViewById(R.id.slider);
    File file=new File("Images/");
    File[] listFile = file.listFiles();
    for (File f: listFile) {
       .....
       .....
    }
    for(String name : Hash_file_maps.keySet()){
        TextSliderView textSliderView = new TextSliderView(MainActivity.this);
        ......
        ......
    }

        try {
            if ( new FileReader(txtFile) != null ) {
                BufferedReader bufferedReader = new BufferedReader(new FileReader(txtFile));
                String receiveString = "";
                StringBuilder stringBuilder = new StringBuilder();

                while ( (receiveString = bufferedReader.readLine()) != null ) {
                    stringBuilder.append(receiveString);
                }                   
                String ret = stringBuilder.toString();                 

sliderLayout.setDuration(Integer.parseInt(stringBuilder.toString()));
            }
        }
        } catch (IOException e) {
        }
    sliderLayout.addOnPageChangeListener(this);
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
GNK
  • 1,036
  • 2
  • 10
  • 29

2 Answers2

1

I have resolved this problem.

try {

        BufferedReader reader = new BufferedReader(new FileReader(txtFile));

        String line = "N/A";

        while ((line = reader.readLine()) != null) {
            text.append(line);
            duration = text.toString();

        }
        reader.close();
    } catch (IOException e) {

    }

sliderLayout.setDuration(Integer.valueOf(duration));

GNK
  • 1,036
  • 2
  • 10
  • 29
0

You should try to used Shared preferences instead of storing the value in a file.

private final static String PREFS = "MY_SHARED_PREFERENCES";


private void storeDuration(Context context, int value) {
    SharedPreferences settings = context.getApplicationContext().getSharedPreferences(PREFS, 0);
    SharedPreferences.Editor editor = settings.edit();
    editor.putInt("DURATION", value);
    editor.apply();
}



private int getDuration(Context context) {
    SharedPreferences settings = context.getApplicationContext().getSharedPreferences(PREFS, 0);
    return settings.getInt("DURATION", 0);
}
AlexMok
  • 724
  • 5
  • 18
  • if we ll do like this ,How ll user could change the interval time. – GNK Jan 15 '18 at 11:18
  • You could make a small dialog with a slider so that the user could change the duration. That would be more user friendly – AlexMok Jan 15 '18 at 11:21