1

I'll do my best to explain what I want to accomplish; I feel if I could explain it better I wouldn't need to ask.

I want to select item from spinner, click one of the 4 buttons to send the spinner value (array value "Medications") to the next activity.

The values in "Medications" will have a string array of their own.

For EXAMPLE.

In "Medications" there is a value ENALAPRIL.

There is a string array "ENALAPRIL" that has 4 values,

HowString[0]

WhatString1

WhenString[2]

NotString[3]

In Activity2 there are only two TextView boxes.

The top TextView is for displaying the medication name selected from spinner

I CAN do this with no problem

The bottom TextView is for displaying a how, what, when, or not string - depending on which button was pressed to get to Activity2.

I need the bottom TextView to use a StringArray value dynamically that was passed with Intent:

"Medications" value(TextView1) selected needs to dynamically equal name of Array value used in (TextView2).

I am struggling with the context and how to phrase my needs so Apologies.

I hope that I am over-complicating something that is very, very simple.

Activity1 layout:

<Spinner
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/rxBox"
    android:prompt="@string/rxprompt"
    android:entries="@array/Medications"
    android:focusable="true"
    android:clickable="true"
    android:visibility="visible"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:textAlignment="center"
    android:spinnerMode="dialog"
    android:layout_centerHorizontal="true"
    android:background="@drawable/abc_search_dropdown_dark"
    android:popupBackground="@drawable/abc_ab_solid_light_holo"
    android:layout_below="@+id/helprx" />

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="@string/rxstudyinfo"
    android:id="@+id/helprx"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"

    android:textStyle="bold"
    android:paddingStart="10dp"
    android:visibility="visible"
    android:textSize="20dp"
    android:lines="5" />

<LinearLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentEnd="true"
    android:paddingTop="50dp"
    android:divider="@drawable/abc_ab_share_pack_holo_dark"
    android:visibility="visible"
    android:layout_below="@+id/rxBox">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/dosing"
        android:id="@+id/doseButton"
        android:layout_gravity="center_horizontal"
        android:clickable="true"
        android:enabled="true"
        android:onClick="howRx" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/what"
        android:id="@+id/whatButton"
        android:clickable="true"
        android:enabled="true"
        android:onClick="whatRX" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/whenButton"
        android:layout_gravity="center_horizontal"
        android:text="@string/when"
        android:clickable="true"
        android:enabled="true"
        android:onClick="whenRX" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/not"
        android:id="@+id/notButton"
        android:clickable="true"
        android:enabled="true"
        android:onClick="notRX" />

</LinearLayout>

Activity2 layout:

    <TextView
        android:text="MEDICATION NAME"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAlignment="center"
        android:textSize="35dp"
        android:id="@+id/rxName"
        android:paddingBottom="35dp" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="MEDICATION INFO"
        android:id="@+id/infoRx" />
 </LinearLayout>


CODE: Activity1

public class studyRX extends ActionBarActivity {




public void howRx(View view){


    Spinner medChoice = (Spinner) findViewById(R.id.rxBox);
    Intent howRxIntent = new Intent(getApplicationContext(),rxViewer.class);
    Bundle b = new Bundle();
    b.putString("medname",medChoice.getSelectedItem().toString());



    howRxIntent.putExtras(b);

    startActivity(howRxIntent);

    //array index 0
}

CODE: Activity2

public class rxViewer extends ActionBarActivity {



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

    Bundle b = this.getIntent().getExtras();
    Resources r = getResources();
    String medname = b.getString("medname");


    ((TextView)findViewById(R.id.rxName)).setText(medname);

    // myString = getResources().getIdentifier(medname,"string",this.getPackageName());
    //((TextView)findViewById(R.id.infoRx)).setText(bang);
    //TextView t = (TextView)findViewById(R.id.infoRx);
    //int maybe = getResources().getIdentifier(bang,"arrays",this.getPackageName());

    Intent intentObject = getIntent();

}

XML:Medications

<string-array name="Medications">
    <item>Enalapril</item>
    <item>Captopril</item>
    <item>Lisinopril</item>
    <item>Ramipril</item>
    <item>Adenosine</item>
    <item>Amiodarone</item>
</string-array>

XML:Enalapril

<string-array name="Enalapril">
    <item> String from HOW Button  </item>
    <item> String from WHAT Button </item>
    <item> String from WHEN Button </item>
    <item> String from NOT  Button </item>
 </string-array>

XML:Captopril

<string-array name="Captopril">
    <item> String from HOW Button  </item>
    <item> String from WHAT Button </item>
    <item> String from WHEN Button </item>
    <item> String from NOT  Button </item>
</string-array>

All medications are formatted the same so the Array index will relate to each button

THANK YOU!

Swaylay
  • 19
  • 1
  • 8
  • possible duplicate of [Dynamic Resource Loading Android](http://stackoverflow.com/questions/3648942/dynamic-resource-loading-android) – Soana Jul 10 '14 at 07:06
  • Thanks for the suggestion, Soana! I will try this within a day or two on my program and provide feedback. – Swaylay Jul 11 '14 at 17:42
  • Unfortunately I haven't been able to solve my problem. Please help – Swaylay Jul 14 '14 at 05:40

1 Answers1

0

From what I read and understood, your problem is to click a button and display an answer on the next activity accordingly with the button which was pressed and the selection of the spinner on the other activity.

So, first, from my point of view it would be better for you to have a class like this:

public class Medication { 
      String name; //name of you medication
      ArrayList<String> answers; //answers which you initialize by the order you want  

      public Medication (String name, ArrayList<String> answers) { 
            this.name = name;
            this.answers = answers;
      }

      public boolean isThisMedication(String name){
            if (this.name.equals(name))
               return true;
            return false;
      }

      public String getAnswer(int position) {
            return answers.get(position);
      }
}

You should initialize a list of objects of this class like ArrayList<Medication> on your Activity 2.

Then, whenever you click a button you have also to send in the bundle the String in the text of that specific button or a number from 0 to 3 (0 is you first button and three is you last number). On the OnClickListener of your button you should be able to do this with no problem.

This is it! It's all set!

Also, you should see this link Get StringArray Item Name on how to use a StringArray if you really don't want to use a class like the one I mentioned.

Hope this helps.

Community
  • 1
  • 1
luiscosta
  • 855
  • 1
  • 10
  • 16
  • Using your suggestion and modifying some of my resources I was able to get it to work as I wanted. THANK YOU – Swaylay Jul 15 '14 at 22:53