1

I am working on a project in which I need to dynamically add TextView and Spinner as well. I was able to add these two things dynamically from my program.

Now when I was trying to select some items in the Spinner, it was not selecting those items.

Does I need to do anything to make that item selected in Spinner?

for (Map.Entry<String, String> entry : mapColumns.entrySet()) {

    spinnerArray = new ArrayList<String>();

    final TextView rowTextView = new TextView(cont);
    final Spinner spinner = new Spinner(cont);

    rowTextView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    spinner.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

    for(String s: entry.getValue().split(",")) {
        System.out.println(s);
        s = s.replaceAll("[^a-zA-Z0-9]+","");

        spinnerArray.add(s);
    }

    ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(cont, android.R.layout.simple_spinner_dropdown_item, spinnerArray);

    rowTextView.setText(entry.getKey());
    rowTextView.setTypeface(null, Typeface.BOLD);
    spinner.setAdapter(spinnerArrayAdapter);

    layout.addView(rowTextView);
    layout.addView(spinner);
}

Here mapColumns will hev Key-Value pair. So in the Spinner all the items are getting shown from the Value of that map.

Problem Statement:-

Now I need to make sure if anybody is selecting any items in the Spinner, it should get selected and visible to naked eye.

How can I do that based on my code. Thanks for the help.

Below is the image-

enter image description here

arsenal
  • 23,366
  • 85
  • 225
  • 331
  • Do you mean visible in Spinner as selected? – AwadKab Mar 24 '13 at 22:17
  • Yes. As soon as I select items in Spinner, I cannot see anything what I selected and also everything looks very light in color. But when I had the same thing getting populated from string.xml file, I was able to see everything and also it was not that much light. I have attached the image as well in my question. – arsenal Mar 24 '13 at 22:18

2 Answers2

1

Try use this code :

ArrayAdapter<String> spinnerArrayAdapter  = new ArrayAdapter<String>(con,
        android.R.layout.simple_spinner_item, spinnerArray);
    spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
AwadKab
  • 3,054
  • 2
  • 17
  • 17
  • One quick question, I guess, I have found the problem. Here `con` is `getApplicationContext` so that's why it was not working before. But after making it to `getBaseContext` it started working. Then what's the difference between those two? – arsenal Mar 24 '13 at 23:24
0

Add an OnItemSelectedListener to your spinner:

spinner.setAdapter(spinnerArrayAdapter);

// add the listener
spinner.setOnItemSelectedListener(this);

Then, implement the listener in your activity:

public void onItemSelected(AdapterView<?> parent, View view, 
        int pos, long id) {
    // An item was selected. You can retrieve the selected item using
    // parent.getItemAtPosition(pos)
}

public void onNothingSelected(AdapterView<?> parent) {
    // Another interface callback
}
Entreco
  • 12,738
  • 8
  • 75
  • 95
  • Thanks Entreco for the suggestion. My question was after selecting the items from the spinner, that items doesn't get reflected to me as I cannot see by my naked eye. But Toast shows me that I did selected some item. – arsenal Mar 24 '13 at 22:03