0

I'm working on the layout as shown below.

enter image description here

So far I've had success with manually defining each View in xml (RelativeLayout within a ScrollView) and manually populating each spinner in java. But this is tedious. I wish I could somehow use "I have this array of items. For each item I want a row on the screen with a spinner and an EditText." I must then be able to grab the user input to put into a database. Eventually, I'll want to be able to come back and edit this screen, which means I'll have to repopulate these fields from a database query.

I tried working some with a ListView, but had trouble populating multiple rows and then read that RelativeLayout is supposed to perform better. What would you suggest? If I have to declare every field in xml and java manually, then so be it.

This is what my XML looks like...

<ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="match_parent"
    android:layout_height="180dp" >

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="match_parent" >

        <Spinner
            android:id="@+id/category_spinner1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
             />

         <EditText
             android:id="@+id/editWeight1"
             android:layout_width="70dp"
             android:layout_height="wrap_content"
             android:layout_toLeftOf="@+id/percentSign1"
             android:layout_alignBaseline="@id/category_spinner1"
             android:inputType="numberDecimal"   >

        </EditText>

        <TextView
                android:id="@id/percentSign1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_alignBaseline="@id/category_spinner1"
                android:text="@string/percentSign"
                android:textSize="20sp" >
        </TextView>


        <Spinner
            android:id="@+id/category_spinner2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@id/category_spinner1"
             />

         <EditText
             android:id="@+id/editWeight2"
             android:layout_width="70dp"
             android:layout_height="wrap_content"
             android:layout_toLeftOf="@+id/percentSign2"
             android:layout_alignBaseline="@id/category_spinner2"
             android:inputType="numberDecimal"   >

        </EditText>

        <TextView
                android:id="@id/percentSign2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_alignBaseline="@id/category_spinner2"
                android:text="@string/percentSign"
                android:textSize="20sp" >
        </TextView> 
...

And my Java...

Spinner catSpin1 = (Spinner) rootView.findViewById(R.id.category_spinner1);
         ArrayAdapter<String> catSpinAdapter1 = new ArrayAdapter<String>(this.getActivity(),android.R.layout.simple_spinner_item, DBAdapter.gradeTypes);
         catSpinAdapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
         catSpin1.setAdapter(catSpinAdapter1);
         catSpin1.setSelection(0);

         Spinner catSpin2 = (Spinner) rootView.findViewById(R.id.category_spinner2);
         ArrayAdapter<String> catSpinAdapter2 = new ArrayAdapter<String>(this.getActivity(),android.R.layout.simple_spinner_item, DBAdapter.gradeTypes);
         catSpinAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
         catSpin2.setAdapter(catSpinAdapter2);
         catSpin2.setSelection(1);

         Spinner catSpin3 = (Spinner) rootView.findViewById(R.id.category_spinner3);
         ArrayAdapter<String> catSpinAdapter3 = new ArrayAdapter<String>(this.getActivity(),android.R.layout.simple_spinner_item, DBAdapter.gradeTypes);
         catSpinAdapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
         catSpin3.setAdapter(catSpinAdapter3);
         catSpin3.setSelection(2);
...
NSouth
  • 5,067
  • 7
  • 48
  • 83

2 Answers2

1

Sure, create a String array, for example:

<string-array name="my_array">
<item>One</item>
<item>Two</item>
</string-array>

Then add it to te spinner on the xml this way:

<Spinner 
        android:id="@+id/spinner"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:drawSelectorOnTop="true"
        android:entries="@array/my_array"/>
JasJar
  • 336
  • 3
  • 14
  • that's a way to fill the Spinner (and I'll probably implement it), but I'm more looking for a way to avoid having to create and declare the spinners (and other screen elements) one by one. What I'm looking for may or may not exist. – NSouth Apr 08 '14 at 19:11
  • The only way to do that is programatically. Create a loop in which you create the spinners, set them (content, params, etc.) and then add them to the corresponding viewgroup. – JasJar Apr 08 '14 at 19:44
  • that sounds good. But if I add them programmatically, then how will I reference them to retrieve user input? How would I know their id? – NSouth Apr 08 '14 at 20:24
  • http://stackoverflow.com/questions/1714297/android-view-setidint-id-programmatically-how-to-avoid-id-conflicts ;D – JasJar Apr 08 '14 at 20:34
1

My first intention would also have been a ListView, you could create a simple datastructure like a class with two string values that will get updated/displayed by this spinner/edittext combination and you could use this class also to persist it in your database.

Some side note: You should have a look at re-using layouts: http://developer.android.com/training/improving-layouts/reusing-layouts.html That would have made your approach much easier. (define one spinner/edittext layout and re-use it inside your main xml)

yekretsaM
  • 421
  • 1
  • 4
  • 10
  • Thanks for the reply. I read the "reusing layouts" article. How would I reference a field in order to retrieve and store a value if they are nested like that? – NSouth Apr 08 '14 at 19:59
  • You would give every one of that views an id just like you did with your spinner and edittext like: and then you could access your layout like: ViewGroup my_first_layout = (ViewGroup) findViewById(R.id.my_first_layout) and access your spinner/edittext there with: my_first_layout.findViewById(R.id.category_spinner) – yekretsaM Apr 09 '14 at 16:37