0

I am trying to make an Android app where you can check a CheckBox and save your selection. The save works but when I want to retrieve a random number out of this saved data I get a -1. Can somebody tell me what I'm doing wrong?

When I use the Toast to display the message it gives me the numbers I selected between [] so it is still an array right?

.java file

public class MainActivity extends AppCompatActivity {

Button save, load;
TextView message;
String Message;
int data_block = 100;

static ArrayList<Integer> selection = new ArrayList<Integer>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    save = (Button) findViewById(R.id.save_button);
    load = (Button) findViewById(R.id.load);
    message = (TextView) findViewById(R.id.textView);
    save.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Message = selection.toString();
            try {
                FileOutputStream fou = openFileOutput("doubleArrayNumbers.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.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            try {
                FileInputStream fis = openFileInput("doubleArrayNumbers.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];
                    }

                    if(final_data.length() == 0){
                        Toast.makeText(MainActivity.this, "You need to pick a selection first", Toast.LENGTH_SHORT).show();
                    } else{
                       // Toast.makeText(getBaseContext(), "Message : " + final_data, Toast.LENGTH_SHORT).show();
                        Random doubleNumberRandom = new Random();
                        int number = final_data.indexOf(doubleNumberRandom.nextInt(final_data.length()));
                        TextView myText = (TextView) findViewById(R.id.textView);
                        String myString = String.valueOf(number);
                        myText.setText(myString);
                    }

                } catch (IOException e) {
                    e.printStackTrace();
                }

            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }
    });

}

public void selectNumber(View view) {
    boolean numberChecked = ((CheckBox) view).isChecked();
    switch (view.getId()) {
        case R.id.number_D1:
            if (numberChecked) {
                selection.add(new Integer(1));
            }else{
                selection.remove(new Integer(1));

            }
            break;
        case R.id.number_D2:
            if (numberChecked) {
                selection.add(new Integer(2));
            } else {
                selection.remove(new Integer(2));
            }
            break;

        case R.id.number_D3:
            if (numberChecked) {
                selection.add(new Integer(3));
            } else {
                selection.remove(new Integer(3));
            }
            break;

        case R.id.number_D4:
            if (numberChecked) {
                selection.add(new Integer(4));
            } else {
                selection.remove(new Integer(4));
            }
            break;

        case R.id.number_D5:
            if (numberChecked) {
                selection.add(new Integer(5));
            } else {
                selection.remove(new Integer(5));
            }
            break;

        case R.id.number_D6:
            if (numberChecked) {
                selection.add(new Integer(6));
            } else {
                selection.remove(new Integer(6));
            }
            break;

        case R.id.number_D7:
            if (numberChecked) {
                selection.add(new Integer(7));
            } else {
                selection.remove(new Integer(7));
            }
            break;

        case R.id.number_D8:
            if (numberChecked) {
                selection.add(new Integer(8));
            } else {
                selection.remove(new Integer(8));
            }
            break;

        case R.id.number_D9:
            if (numberChecked) {
                selection.add(new Integer(9));
            } else {
                selection.remove(new Integer(9));
            }
            break;

        case R.id.number_D10:
            if (numberChecked) {
                selection.add(new Integer(10));
            } else {
                selection.remove(new Integer(10));
            }
            break;


    }
}

}

.xml file

 <?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.developer.sven.seceltiedartworkout.MainActivity">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:textAppearanceLarge"
    android:text="Dart Workout"
    android:id="@+id/textView"
    android:layout_gravity="center_horizontal"
    android:layout_marginBottom="83dp"
    android:layout_alignParentBottom="true"
    android:layout_alignEnd="@+id/load" />
<Button
    android:layout_width="125dp"
    android:layout_height="125dp"
    android:text="D"
    android:textSize="50dp"
    android:id="@+id/load"
    android:onClick="DoubleButton"
    android:background="#0a07c5"
    android:layout_alignParentTop="true"
    android:layout_alignParentEnd="true"
    android:layout_marginTop="37dp" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Save"
    android:id="@+id/save_button"
    android:layout_centerVertical="true"
    android:layout_alignEnd="@+id/load" />

<CheckBox
    android:layout_width="wrap_content"
    android:layout_height="21dp"
    android:id="@+id/number_D1"
    android:text="D1"
    android:onClick="selectNumber"
    android:layout_alignParentTop="true"
    android:layout_alignParentStart="true"
    android:checked="false" />

<CheckBox
    android:layout_width="wrap_content"
    android:layout_height="21dp"
    android:id="@+id/number_D2"
    android:text="D2"
    android:layout_alignParentStart="true"
    android:onClick="selectNumber"
    android:layout_below="@+id/number_D1"
    android:layout_alignParentLeft="true"
    android:checked="false" />

<CheckBox
    android:layout_width="wrap_content"
    android:layout_height="21dp"
    android:id="@+id/number_D3"
    android:text="D3"
    android:layout_alignParentStart="true"
    android:onClick="selectNumber"
    android:layout_below="@+id/number_D2"
    android:layout_alignParentLeft="true"
    android:checked="false" />

<CheckBox
    android:layout_width="wrap_content"
    android:layout_height="21dp"
    android:id="@+id/number_D4"
    android:text="D4"
    android:layout_alignParentStart="true"
    android:onClick="selectNumber"
    android:layout_below="@+id/number_D3"
    android:layout_alignParentLeft="true"
    android:checked="false" />

<CheckBox
    android:layout_width="wrap_content"
    android:layout_height="21dp"
    android:id="@+id/number_D5"
    android:text="D5"
    android:layout_alignParentStart="true"
    android:onClick="selectNumber"
    android:layout_below="@+id/number_D4"
    android:layout_alignParentLeft="true"
    android:checked="false" />

<CheckBox
    android:layout_width="wrap_content"
    android:layout_height="21dp"
    android:text="D6"
    android:id="@+id/number_D6"
    android:layout_below="@+id/number_D5"
    android:layout_alignParentStart="true"
    android:checked="false"
    android:onClick="selectNumber"/>

<CheckBox
    android:layout_width="wrap_content"
    android:layout_height="21dp"
    android:text="D7"
    android:id="@+id/number_D7"
    android:layout_below="@+id/number_D6"
    android:layout_alignParentStart="true"
    android:onClick="selectNumber"
    android:checked="false" />

<CheckBox
    android:layout_width="wrap_content"
    android:layout_height="21dp"
    android:text="D8"
    android:id="@+id/number_D8"
    android:layout_below="@+id/number_D7"
    android:layout_alignParentStart="true"
    android:onClick="selectNumber"
    android:checked="false" />

<CheckBox
    android:layout_width="wrap_content"
    android:layout_height="21dp"
    android:text="D9"
    android:id="@+id/number_D9"
    android:layout_below="@+id/number_D8"
    android:layout_alignEnd="@+id/number_D8"
    android:onClick="selectNumber"
    android:checked="false" />

<CheckBox
    android:layout_width="wrap_content"
    android:layout_height="21dp"
    android:text="D10"
    android:id="@+id/number_D10"
    android:layout_below="@+id/number_D9"
    android:onClick="selectNumber"
    android:checked="false" />

</RelativeLayout>
Tyler Benzing
  • 1,116
  • 11
  • 25
SvenH
  • 5
  • 3

1 Answers1

0

Changed my whole answer!

You should be able to see where to replace code. The "[" and "]" chars can be srtiped, then split on "," to get you an array of strings (now they are indexed, closer to your original intent I think).

String csv = final_data.replaceAll("\\[|\\]|s", "");
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.textView);
myText.setText(number);

You can now generate a random index in to numbers, and set that resultant number string directly into your textview.

Miao Liu
  • 440
  • 4
  • 9
  • Hello Maio Liu, it does almost work only not the way i want it to. if i'm using the (int number) i get strange outcomes of for example D32 - D91 -D53 etc. and not the selection i gave it. if i'm using the (char number) the numbers are correct only the "," and "[ ]" are also shown in the textview. is there a way to fix this – SvenH Mar 24 '16 at 21:33
  • I updated my answer. I ran your code to figure out what you were doing, since there's no sample pics or anything, and I'm not really that good at interpreting xml in my head quickly. Anyway, this is one way to do this. Sort of "least lines of code changed to unbreak things" sort of solution. This whole method is sort of re-inventing the wheel though. You can use the `serializable` property of `List`s to make this work. Check out [this](http://stackoverflow.com/questions/16111496/java-how-can-i-write-my-arraylist-to-a-file-and-read-load-that-file-to-the), and ping me if you need examples. – Miao Liu Mar 25 '16 at 01:53