Spinner won't display string entries

Trush Patel :

My program should use a spinner to show a list of color names, and have the background color of it. For example in the image shown, the color red should have a black text displayed saying "Red" to specify it. https://i.stack.imgur.com/BT0dW.jpg

I have android:entries="@array/myStrings" that shows the first color's text (without color, plain white background) on design mode, but once I run the program, the text does not display for any of the colors.

strings.xml

<resources>
    <string name="app_name">Color Activity</string>
    <string-array name="myStrings">
        <item>Silver</item>
        <item>Pink</item>
        <item>Red</item>
        <item>Orange</item>
        <item>Yellow</item>
        <item>Green</item>
        <item>Blue</item>
        <item>Indigo</item>
        <item>Violet</item>
        <item>Brown</item>
    </string-array>
</resources>

colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#008577</color>
    <color name="colorPrimaryDark">#00574B</color>
    <color name="colorAccent">#D81B60</color>

    <color name="silver" type="color">#c0c0c0</color>
    <color name="pink" type="color">#ffc0cb</color>
    <color name="red" type="color">#ff0000</color>
    <color name="orange" type="color">#ffa500</color>
    <color name="yellow" type="color">#ffff00</color>
    <color name="green" type="color">#00ff00</color>
    <color name="blue" type="color">#0000ff</color>
    <color name="indigo" type="color">#4b0082</color>
    <color name="violet" type="color">#7f00ff</color>
    <color name="brown" type="color">#654321</color>
    <color name="white" type="color">#FFFFFF</color>
    <integer-array name="myColors">
        <item>@color/silver</item>
        <item>@color/pink</item>
        <item>@color/red</item>
        <item>@color/orange</item>
        <item>@color/yellow</item>
        <item>@color/green</item>
        <item>@color/blue</item>
        <item>@color/indigo</item>
        <item>@color/violet</item>
        <item>@color/brown</item>
    </integer-array>

</resources>

MainActivity

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.graphics.Color;
import android.media.session.PlaybackState;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

import java.util.ArrayList;


public class ColorActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        final View newBackground;

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final Spinner spinner = findViewById(R.id.spinner);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
                this, R.array.myColors, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(
                android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(new ColorAdapter(this));
        newBackground = this.getWindow().getDecorView();



        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                ArrayList<Integer> colors;
                colors = new ArrayList<Integer>();
                int retrieve []= getResources().getIntArray(R.array.myColors);
                for(int i:retrieve)
                {
                    colors.add(i);
                }

                if(position == 0){
                    view.setBackgroundResource(R.color.white);
                    newBackground.setBackgroundResource(R.color.silver);
                }else if(position == 1){
                    view.setBackgroundResource(R.color.white);
                    newBackground.setBackgroundResource(R.color.pink);
                }else if(position == 2){
                    view.setBackgroundResource(R.color.white);
                    newBackground.setBackgroundResource(R.color.red);

                }else if(position == 3){
                    view.setBackgroundResource(R.color.white);
                    newBackground.setBackgroundResource(R.color.orange);
                }else if(position == 4){
                    view.setBackgroundResource(R.color.white);
                    newBackground.setBackgroundResource(R.color.yellow);
                }else if(position == 5){
                    view.setBackgroundResource(R.color.white);
                    newBackground.setBackgroundResource(R.color.green);
                }else if(position == 6){
                    view.setBackgroundResource(R.color.white);
                    newBackground.setBackgroundResource(R.color.blue);
                }else if(position == 7){
                    view.setBackgroundResource(R.color.white);
                    newBackground.setBackgroundResource(R.color.indigo);
                }else if(position == 8){
                    view.setBackgroundResource(R.color.white);
                    newBackground.setBackgroundResource(R.color.violet);
                }else if(position == 9){
                    view.setBackgroundResource(R.color.white);
                    newBackground.setBackgroundResource(R.color.brown);
                }
            }

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

            }
        });


    }

}

Color Adapter:

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.SpinnerAdapter;
import android.widget.TextView;

import java.util.ArrayList;

public class ColorAdapter extends BaseAdapter {
    ArrayList<Integer> colors;
    Context context;

    public ColorAdapter(Context context){

        this.context=context;
        colors = new ArrayList<Integer>();
        int retrieve []=context.getResources().getIntArray(R.array.myColors);

        for(int i:retrieve)
        {
            colors.add(i);
        }
    }
    @Override
    public int getCount() {
        return colors.size();
    }

    @Override
    public Object getItem(int args) {
        return colors.get(args);
    }

    @Override
    public long getItemId(int args) {
        return args;
    }

    public String getElementFromColors(int position){
        String retrieve []=context.getResources().getStringArray(R.array.myColors);
        return retrieve[position];
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater=LayoutInflater.from(context);
        convertView=inflater.inflate(android.R.layout.simple_spinner_dropdown_item, null);
        TextView txv=(TextView)convertView.findViewById(android.R.id.text1);
        txv.setBackgroundColor(colors.get(position));
        txv.setTextSize(20f);
        txv.setText(getElementFromColors(position));
        return convertView;
    }

}
Zain :

You just have a miss-typing of R.array.myColors in your adapter instead of R.array.myStrings

To Solve this, replace below line of code within getElementFromColors() method of your ColorAdapter

String retrieve[] = context.getResources().getStringArray(R.array.myColors);

with:

String retrieve[] = context.getResources().getStringArray(R.array.myStrings);

enter image description here

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=11414&siteId=1