vendredi 23 octobre 2015

android random numbers in an array

I want to make an app of generating 7 random numbers. Click the button one time will generate one number, but I wonder why it cannot run. Here is my code MainActivity.java public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
public void generate(View v){
    Random myRandom = new Random();

    int[] arr = new int[2];
    int i = 0;
    TextView tv_number_one = (TextView)findViewById(R.id.tv_number_one);
    TextView tv_number_two = (TextView)findViewById(R.id.tv_number_two);
    TextView tv_number_three = (TextView)findViewById(R.id.tv_number_three);
    TextView tv_number_four = (TextView)findViewById(R.id.tv_number_four);
    TextView tv_number_five = (TextView)findViewById(R.id.tv_number_five);
    TextView tv_number_six = (TextView)findViewById(R.id.tv_number_six);
    TextView tv_number_seven = (TextView)findViewById(R.id.tv_number_seven);
    i++;
    arr[i]  = myRandom.nextInt(48)+1;

    tv_number_one.setText("" + String.valueOf(arr[1]));
    tv_number_two.setText("" + String.valueOf(arr[2]));
    tv_number_three.setText("" + String.valueOf(arr[3]));
    tv_number_four.setText("" + String.valueOf(arr[4]));
    tv_number_five.setText("" + String.valueOf(arr[5]));
    tv_number_six.setText("" + String.valueOf(arr[6]));
    tv_number_seven.setText("" + String.valueOf(arr[7]));

}}

activity_main

<TextView android:text="\?" android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/tv_number_one"
    android:textSize="30sp"
    android:textIsSelectable="true"
    android:layout_marginLeft="10dp"
    android:textColor="#67ceff"
    android:layout_marginStart="20dp"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="\?"
    android:id="@+id/tv_number_two"
    android:layout_alignBottom="@+id/tv_number_one"
    android:layout_toRightOf="@+id/tv_number_one"
    android:layout_toEndOf="@+id/tv_number_one"
    android:textColor="#67ceff"
    android:textSize="30sp"
    android:layout_marginLeft="20dp"
    android:layout_marginStart="20dp"
    />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="\?"
    android:id="@+id/tv_number_three"
    android:layout_alignParentTop="true"
    android:layout_toRightOf="@+id/tv_number_two"
    android:layout_toEndOf="@+id/tv_number_two"
    android:layout_marginLeft="20dp"
    android:textColor="#67ceff"
    android:textSize="30sp"
    android:layout_marginStart="20dp"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="\?"
    android:id="@+id/tv_number_four"
    android:layout_alignParentTop="true"
    android:layout_toRightOf="@+id/tv_number_three"
    android:layout_toEndOf="@+id/tv_number_three"
    android:textColor="#67ceff"
    android:textSize="30sp"
    android:textIsSelectable="true"
    android:layout_marginLeft="20dp"
    android:layout_marginStart="20dp"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="\?"
    android:id="@+id/tv_number_five"
    android:layout_alignParentTop="true"
    android:layout_toRightOf="@+id/tv_number_four"
    android:layout_toEndOf="@+id/tv_number_four"
    android:layout_marginLeft="20dp"
    android:textColor="#67ceff"
    android:textSize="30sp"
    android:layout_marginStart="20dp"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="\?"
    android:id="@+id/tv_number_six"
    android:layout_alignBottom="@+id/tv_number_five"
    android:layout_toRightOf="@+id/tv_number_five"
    android:layout_toEndOf="@+id/tv_number_five"
    android:textColor="#67ceff"
    android:textSize="30sp"
    android:layout_marginLeft="20dp"
    android:layout_marginStart="20dp"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="\?"
    android:id="@+id/tv_number_seven"
    android:layout_alignParentTop="true"
    android:layout_toRightOf="@+id/tv_number_six"
    android:layout_toEndOf="@+id/tv_number_six"
    android:layout_marginLeft="20dp"
    android:textColor="#6198ff"
    android:textSize="30sp"
    android:layout_marginStart="20dp"/>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="NEXT NUMBER"
    android:id="@+id/button_next_num"
    android:layout_below="@+id/tv_number_one"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="41dp"
    android:onClick="generate"/>

Can anyone help me???




Aucun commentaire:

Enregistrer un commentaire