如何从保存的文件中检索随机数? (Java)的

时间:2016-03-21 20:40:49

标签: java android android-studio arraylist save

我正在尝试创建一个Android应用,您可以在其中检查CheckBox并保存您的选择。保存有效但当我想从这个保存的数据中检索一个随机数时,我得到-1。有人可以告诉我我做错了吗?

当我使用Toast显示消息时,它会给出我在[]之间选择的数字,所以它仍然是一个数组吗?

.java档案

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档案

 <?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>

1 个答案:

答案 0 :(得分:0)

改变了我的整个答案!

您应该能够看到替换代码的位置。 &#34; [&#34;和&#34;]&#34; chars可以被srtiped,然后分成&#34;,&#34;为你提供一系列字符串(现在它们被编入索引,我认为更接近你原来的意图)。

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);

您现在可以在numbers中生成随机索引,并将结果数字字符串直接设置到textview中。