lundi 28 mars 2016

save in one class load in another in java

currently i'm working on a app where i can save numbers in a database in one class and load in another class. i get the numbers by checking a checkbox and add them to an arrayList. now my problem. for example when i checked number 1 and number 2 and save it it works fine. only when i uncheck number 1 and number 2 and check number 3 and number 4 and then save it number 1 and 2 are still there. can somebody tell me what i am doing wrong.

if you need the code of the checkboxes please say it and i will add it.

save part

protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.preference_dart_throw_page);
        final Intent preferenceDartThrowPageOpened = getIntent();
        String previousActivity = preferenceDartThrowPageOpened.getExtras().getString("Pref");
        loadSingle = (Button) findViewById(R.id.savePreferenceTriple);

        save = (Button) findViewById(R.id.savePreferenceSingle);
        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Message = Practise.selectionSingle.toString();
                try {
                    FileOutputStream fou = openFileOutput("singleArrayNumbers.txt", MODE_WORLD_READABLE);
                    OutputStreamWriter osw = new OutputStreamWriter(fou);
                    try {

                        osw.write(Message);
                        osw.flush();
                        osw.close();
                        Toast.makeText(getBaseContext(), "Data Saved", Toast.LENGTH_SHORT).show();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                } catch (FileNotFoundException e) {

                    e.printStackTrace();
                }
            }
        }); 
}

load part

public class Practise extends AppCompatActivity{

Button save;
int data_block = 100;
static ArrayList<Integer> selectionSingle = new ArrayList<Integer>();
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.practise_page);

        Intent practisePageOpened = getIntent();
        String previousActivity = practisePageOpened.getExtras().getString("Practise");
            loadSingle = (Button) findViewById(R.id.buttonSingle);
       loadSingle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    FileInputStream fis = openFileInput("singleArrayNumbers.txt");
                    InputStreamReader isr = new InputStreamReader(fis);
                    char[] data = new char[data_block];
                    String final_data = "";
                    int size;
                    try {
                        while ((size = isr.read(data)) > 0) {
                            String read_data = String.copyValueOf(data, 0, size);
                            final_data += read_data;
                            data = new char[data_block];
                        }

                        String csv = final_data.replaceAll("\\[", "").replaceAll("\\]","").replaceAll(" ","");
                        String[] numbers = csv.split(",");
                        // Toast.makeText(getBaseContext(), "Message : " + final_data, Toast.LENGTH_SHORT).show();
                        Random doubleNumberRandom = new Random();
                        String number = numbers[doubleNumberRandom.nextInt(numbers.length)];
                        TextView myText = (TextView) findViewById(R.id.randomNumberDisplay);
                        if(number == ""){
                            Toast.makeText(Practise.this, "empty", Toast.LENGTH_SHORT).show();
                        }else {
                            Toast.makeText(Practise.this, "numbers :" + final_data, Toast.LENGTH_SHORT).show();
                            myText.setText("D" + number);
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
            }
        });
}




Aucun commentaire:

Enregistrer un commentaire