0

I have a custom view that uses a TextView and a Spinner. I am trying to build the new view in a way that allows me to set the TextView text, the Spinner entries and the Spinner prompt from the newly created object. See the code below for how it works.
When I add the entries parameter directly on the original spinner it works fine. However, when I add it in the custom view I only get null when I try to access the array from the SettingItemSpinner class. It is worth noting that I have no problems accessing the text parameter or the prompt parameter. So what am I doing wrong? I have tried to access the entries attribute as an int, a String a CharacterSequence[] I've called getText(int) and getTextArray(int).

Using logcat to check the results shows that element in the TypedArray is always null. How can I fix this?

My custom view, is defined in the following xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="10dp"
    android:orientation="horizontal"
    android:weightSum="1">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_weight="0.5"
        android:gravity="center_vertical"
        android:textColor="@color/ws_blue"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Settings Item: "
        android:id="@+id/itemLabel"
        />

    <Spinner
        android:id="@+id/si_spinner_value"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:layout_weight="0.5"
        />

</LinearLayout>

I use the custom view in another layout file for a fragment, the section concerned looks like this:

<com.mystuff.serviceapplication.SettingItemSpinner
    android:id="@+id/si_wlan_presets"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="WLAN Presets:"
    android:entries="@array/security_type_array"
    android:prompt="@string/security_type_prompt"
  />

Notice these three lines:

    android:text="WLAN Presets:"
    android:entries="@array/security_type_array"
    android:prompt="@string/security_type_prompt"

the text attribute belongs to the TextView element of my custom view, and the other two elements belong to the Spinner element of the custom view. I can access the text parameter and the prompt parameter without any problems.

My the relevant section of my SettingItemSpinner.class looks like this:

private void init(Context context, AttributeSet attributeSet) {
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    rootView = (SettingItemSpinner) inflater.inflate(R.layout.setting_item_spinner, this);
    tvItemLabel = (TextView) rootView.findViewById(R.id.itemLabel);
    spinner = (Spinner) rootView.findViewById(R.id.si_spinner_value);
    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {

            Log.i("SPINNER: ", "You selected item: " + spinner.getSelectedItem());
        }

        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {

        }
    });

    int[] attrs = {android.R.attr.text, android.R.attr.entries, android.R.attr.prompt};
    TypedArray a = context.obtainStyledAttributes(attributeSet, attrs);
    String text = a.getString(0);
    Log.i("SPINNER", "text string: " + text);
    String prompt = a.getString(2);
    Log.i("SPINNER", "prompt string: " + prompt);
/*
 *  I expect to get access to the security_type_array somewhere here where I
 *  will parse it into an array of Strings before continuing below
 */

    List<String> itemList = new ArrayList<String>();

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(context.getApplicationContext(),android.R.layout.simple_spinner_dropdown_item, itemList);
    spinner.setAdapter(adapter);
    spinner.setPrompt(prompt);
    tvItemLabel.setText(text);

    a.recycle();
}
3
  • See stackoverflow.com/questions/13747400/… post probably help Commented Jul 13, 2016 at 14:06
  • thanks, I'll check it out. Commented Jul 14, 2016 at 7:45
  • Nope, that doesn't help, I still can't access the array from the SettingItemSpinner class. I keep getting null Commented Jul 14, 2016 at 9:02

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.