1

OK, so I've been following a tutorial (http://javatechig.com/android/android-spinner-example) which is about creating Spinners and dynamically adding images (the locations of which are defined in the strings.xml file) to the view. Well that works fine but I'd like to extend the project so that I can include text arrays, again defined in the strings.xml file, to appear both before and after the image. My code follows and, as you can see I'd like to substitute the 2 setText lines with references to the spinner item that I've already selected. Any ideas? Thanks for your interest.

Thanks all for your input and I've now solved the problem and am showing some commented code below on how I resolved it...

public class MainActivity extends Activity {

private ImageView image;
private String[] states;
private Spinner spinner1;
private TypedArray imgs;
private String[] leaders;
private String[] descrip;

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

    states = getResources().getStringArray(R.array.countries);
    imgs = getResources().obtainTypedArray(R.array.leader_photos);

    // --- The following lines grab the problematic string arrays defined in
    // strings.xml
    leaders = getResources().getStringArray(R.array.leaders);
    descrip = getResources().getStringArray(R.array.descrip);

    image = (ImageView) findViewById(R.id.leaderPhoto);
    spinner1 = (Spinner) findViewById(R.id.spinner1);

    ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, states);
    dataAdapter
            .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner1.setAdapter(dataAdapter);

    spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> parent, View view,
                int position, long id) {

            // The variable index identifies what position the selected
            // spinner is. TextView text... locks to the TextView defined in
            // the activity_main.xml layout file. text.setText... loads the
            // appropriate value from the leaders string array
            int index = parent.getSelectedItemPosition();
            TextView text = (TextView) findViewById(R.id.tv1);
            text.setText(leaders[index]);

            image.setImageResource(imgs.getResourceId(
                    spinner1.getSelectedItemPosition(), -1));

            // See earlier comments above TextView tv1 for how this works
            TextView text2 = (TextView) findViewById(R.id.tv2);
            text2.setText(descrip[index]);

        }

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

        }

    });
}

}

2
  • 1
    String item = (String) parent.getItemAtPosition(pos); and then text.setText(item); Commented Dec 17, 2013 at 17:49
  • Combine both the answer and @Raghunandhan's comment and that would work like you expect. Commented Dec 17, 2013 at 18:07

2 Answers 2

1

Android - programatically adding text from strings.xml to TextView -

If I understood you right?

 Resources resources = context.getResources();

 String[] textString = resources.getStringArray(R.array.mytext);

Then you have the text in a string-vector - easy to put in a textView

Then in the xml-file

 <string-array name = "mytext"> 
    <item> ... text </item>
    <item> ... text</item>
    <item> ... text</item>
 </string-array>

And in the textView-object

text.setText(textString[i]); // at position i
Sign up to request clarification or add additional context in comments.

Comments

0

You can use the position you get into the OnClickListener of Spinner to get the particular Leader and Description of the that states as follows. text.setText(leader[position]); text2.setText(descrip[position]);

Comments

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.