Android Development-XML and Java Files, Insert Ran

2019-09-02 16:58发布

I am creating an app for android, using Eclipse. I am uncertain how to have a new random string every time the user enters the next screen. Currently, I have the first and second screens up and running smoothly. However, the third screen (where the random string will be shown) is still not working. I have tried everything that I could think of, and then I also tried other answers to questions from this website; and yet nothing is working. Please help me with both the java and xml files and direct me on how to solve this problem!
Below are my codes:

My string array (located in my values xml section)

   <!-- Restaurant Array Jazz -->
    <string-array name="restArray">
        <item>Olive Garden</item>
        <item>Red Robin</item>
        <item>Cracker Barrel</item>
        <item>Bertuccis</item>
        <item>Outback</item>
        <item>Applebees</item>
        <item>Bahama Breeze</item>
    </string-array>

This java file displays the information in the xml file, but does not give a random output:

package com.momsmealplanner;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;


public class OutcomeActivity extends Activity 
{

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

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

I also tried this for my java file:

package com.momsmealplanner;

import java.util.Random;

import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TableRow;


public class OutcomeActivity extends Activity 
{
    private String[] list; 
    private static final Random rgenerator = new Random();
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;)
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.layout1);

        Resources res = getResources();
            list = res.getStringArray(R.array.restArray);
            String q = list[rgenerator.nextInt(list.length)];
        int toal = 7;

            for (int current = 0; current < total; current++)
            {
                // Create a TableRow and give it an ID
                TableRow tr = new TableRow(this);
                tr.setId(100 + current);
                tr.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
                        LayoutParams.WRAP_CONTENT));

                //Create a TextView to house the name of the province
                TextVuew labelTV = new TextView(this);
                labelTV.setId(200 + current);
                labelTV.setText(q);
                labelTV.setTextSize(14);
                labelTV.setGravity(Gravity.CENTER);
                labelTV.setTextColor(Color.WHITE);
                labelTV.setLayoutParams(newLayoutParams(LayoutParams.FILL_PARENT,
                        LayoutParams.WRAP_CONTENT));

                tr.addView(labelTV);
                tablelayout.addView(tr, newTableLayoutParams(LayoutParams.FILL_PARENT,
                        LayoutParams.WRAP_CONTENT));
            }


    }









}

I tried this as well for my java file:

package com.momsmealplanner;

import java.util.Random;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;


public class OutcomeActivity extends Activity 
{

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

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

    String[]strs=new String[]{"first", "second", "third", "fourth", "fifth"};
    int randomIndex=new Random().nextInt(5);
    int resId=getResources().getIdentifier(strs[randomIndex],"string", momsmealplanner);
    String randomString = getString(resId);

}

And also this for my java file:

package com.momsmealplanner;

import java.util.*;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;



public class OutcomeActivity  
{
    public static void main(String[]args)
    {
        Random rnd = new Random();
        Scanner scan = new Scanner (System.in);
        System.out.println("Enter how many string the array");
        int x = scan.nextInt();
        ArrayList<String> arraystr = new ArrayList<string>();

        for (int i=0; i < x ; i++)
        {
            System.out.println("Eneter elements of the array");
            arraystr.add(scan.next());
        }
        System.out.println("\nI picked: ");

        for ( int t = 0; t < 3; t++) //3 is how many elements you want to pick
        {
            int random = rnd.nextInt(x);
            System.out.println(arraystr.get(random));
            arraystr.remove(random);
        }
    }

}

My xml file:

   <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=".OutcomeActivity" >

      <!-- <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/hello_world" />
      -->  

      <TextView
            android:id="@+id/textView4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/outcome" 
            android:layout_centerHorizontal="true" 
        />





    </RelativeLayout>

Note: the xml file works fine!

I just need help with fixing one of my java codes and then also with what I need to place into the xml file to have it register what was created in the java part! Thank-you in advance!

V/r, Amelia

...I even just tried this code in my java

package com.momsmealplanner;

import java.util.Random;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;


public class OutcomeActivity extends Activity 
{
    public void restChoiceButton_Click(View view)
    {
        TextView outcomeHolder;
        outcomeHolder = (TextView) findViewById(R.id.textViewOutcome);
        Random genOutcome = new Random();
        Integer curOutcome = outcome.lastIndexOf(outcomeHolder.getText());
        Integer randOutcome = genOutcome.nextInt(outcome.size());

        while(randOutcome==curOutcome)
        {
            randOutcome = genOutcome.nextInt(outcome.size());
        }

        outcomeHolder.setText(outcome.elementAt(randOutcome));
    }

}

but on all the 'outcome's it says that "outcome cannot be resolved." It suggest ten quick fixes, but after trying them, it only makes it more complicated and still does not work. Please help!!!

0条回答
登录 后发表回答