skip to Main Content

i have a problem here in using spinner, i want to add value to each array item in my string.xml

this is my code:

    <string-array name="hubungan">
            <item>Choice</item>
            <item>CHILD</item>
            <item>PARENT</item>
            <item>HUSBAND</item>
            <item>WIFE</item>
   </string-array>

I mean is:

<string-array name="hubungan">
                <item>Choice value="1"</item>
                <item>CHILD value="2"</item>
                <item>PARENT value="3"</item>
                <item>HUSBAND value="4"</item>
                <item>WIFE value="5"</item>
       </string-array>

2

Answers


  1. try this

     <Spinner
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:id="@+id/spinner"
         android:entries="@array/hubungan"
       />
    
    Login or Signup to reply.
  2. Well if you insist doing in this way try this example. The example will be as minimal as possible since you don’t provide enough code.

    Your string array values as you defined but I modified the way of storing by convenince.

    <string-array name="hubungan">
        <item>Choice,1</item>
        <item>CHILD,2</item>
        <item>PARENT,3</item>
        <item>HUSBAND,4</item>
        <item>WIFE,5</item>
    </string-array>
    

    I will assume that you have a spinner and a textview in your activity / fragment. You can set this data and process it as needed like following:
    Spinner definition in xml

    <Spinner
        android:id="@+id/spinner"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    
    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    

    This code must be located in your onCreate method if you use an activity, or onViewCreated method if you use a fragment.

    // I assume that you've already instantiated the spinner and textview
    
    // Setup for spinner
    String[] hubungans = getResources().getStringArray(R.array.hubungan);
    if(hubungans != null && hubungans.length > 0) {
        String[] names = new String[hubungans.length];
        String[] values = new String[hubungans.length];
        // Now we will parse the records and split them into name and value
        for(int i = 0; i < hubungans.length; i++) {
            String hubungan = hubungans[i];
            if(hubungan == null || hubungan.isEmpty()) {
                Log.d(TAG, "onViewCreated: couldn't get record for index "+i);
                continue;
            }
            // Split the record by "," seperator for example for choice "Choice,1"
            String[] nameValue = hubungan.split(",");
            if(nameValue.length < 2) {
                Log.d(TAG, "onViewCreated: couldn't get split record for index "+i);
                continue;
            }
            names[i] = nameValue[0]; // first index will have the names
            values[i] = nameValue[1]; // second will have its value
        }
        Log.d(TAG, "onViewCreated: names and values: "+ Arrays.toString(names)+" - "+Arrays.toString(values));
        ArrayAdapter<CharSequence> adapter = new ArrayAdapter<>(requireContext(), android.R.layout.simple_spinner_item, names);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                int val = 0;
                try {
                    val = Integer.parseInt(values[position]); // Here you have value as numeric type
                } catch (NumberFormatException nfe) {
                    nfe.printStackTrace();
                }
    
                textView.setText(String.format("Value for %s is %d", names[position], val));
            }
    
            @Override
            public void onNothingSelected(AdapterView<?> parent) {
    
            }
        });
    } else {
        Log.d(TAG, "onViewCreated: Hubungans cannot be read!");
    }
    

    There you go! Hope this helps you with your unique problem.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search