Why is my dynamic spinner not populating - android

I have created a spinner dynamically. It shows up but is empty. It is meant to contain the following string array.
<string-array name="task_array">
<item>task 1</item>
<item>task 2</item>
<item>task 3</item>
<item>task 4</item>
</string-array>
The following is my code from a class that extends fragmentactivity
public void addNewView() {
final TableLayout table = (TableLayout) findViewById(R.id.spLayout);
Spinner sp = new Spinner(this);
ArrayAdapter<CharSequence> adapter_tasks = ArrayAdapter.
createFromResource(this, R.array.task_array,
android.R.layout.simple_spinner_item);
table.addView(sp);
for(int i = 0; i < 10; i++) {
sp.setId(i+10);
}}
Any ideas? ta

You forgot to add the adapter to the spinner.
Use sp.setAdapter(adapter_tasks);

Related

Selecting a specific string array

I have a question and i cant find a solution myself. I am using for example the following string arrays:
<string-array name="alphabet">
<item>ccc</item>
<item>bbb</item>
<item>aaa</item>
</string-array>
<string-array name="ccc">
<item>1</item>
<item>2</item>
<item>3</item>
</string-array>
<string-array name="bbb">
<item>4</item>
<item>5</item>
<item>6</item>
</string-array>
<string-array name="aaa">
<item>7</item>
<item>8</item>
<item>9</item>
</string-array>
So, The first string array alphabet is placed inside a spinner. (dropdown menu) Lets say i select CCC, Then i want only the items in between the array CCC to be worked with and stored in an array These information in this array should then be randomized and formatted into groups later on. But i only need a selection of the string selected.
So is there a way to select just one array based on the choice made out of the first array?
Kind regards,
Here is the code, the way you needed.
Step 1] Set, setOnItemSelectedListener on first spinner and get the Arrayname for target spinner
spinner_alphabates.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l)
{
String arrayName = spinner_alphabates.getSelectedItem().toString();
int resId = getResources().getIdentifier(arrayName,"array",getPackageName());
setResultArray(resId);
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
Step 2] set the result array on target spinner as
public void setResultArray(int resID)
{
String [] result_array = getResources().getStringArray(resID);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, result_array);
spinner_result.setAdapter(adapter);
}
No need to put 'if' 'else' conditions to match array names.
Hope this help you.
Try this, I am not sure whether it is going to work, just have try
<string-array name="ccc">
<item>1</item>
<item>2</item>
<item>3</item>
</string-array>
<integer-array name="id">
<item> #array/ccc </item>
</integer-array>
You need to get array of string like this. and you will access it with index of string array.
String.xml
<string-array name="my_books">
<item>Item 1</item>
<item>Item 2</item>
<item>Item 3</item>
<item>Item 4</item>
</string-array>
Get List of string in Java file like :
Resources res = getResources();
String[] myBooks = res.getStringArray(R.array.my_books);
Try this method and put this method into onCreate in activity method..
change this array add one more item..
<string-array name="alphabet">
<item>Select</item>
<item>ccc</item>
<item>bbb</item>
<item>aaa</item>
</string-array>
private void initView() {
spinner = findViewById(R.id.spinner);
final List<String> asList = Arrays.asList(getResources().getStringArray(R.array.alphabet));
final ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, asList);
final ArrayAdapter<String> adapter2 = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
if (spinner.getSelectedItem().equals("ccc")) {
adapter2.addAll(Arrays.asList(getResources().getStringArray(R.array.ccc)));
spinner.setAdapter(adapter2);
adapter2.notifyDataSetChanged();
} else if (spinner.getSelectedItem().equals("aaa")) {
adapter2.addAll(Arrays.asList(getResources().getStringArray(R.array.aaa)));
spinner.setAdapter(adapter2);
adapter2.notifyDataSetChanged();
} else if (spinner.getSelectedItem().equals("bbb")) {
adapter2.addAll(Arrays.asList(getResources().getStringArray(R.array.bbb)));
spinner.setAdapter(adapter2);
adapter2.notifyDataSetChanged();
}
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
}

How to delete item from spinner added via entries in layout xml?

I have this array in my strings.xml :
<string-array name="lst_addressTypes">
<item>FISCAL</item>
<item>NAP</item>
<item>SUCURSAL</item>
<item>ALMACEN</item>
<item>OFICINA</item>
<item>OTRO</item>
</string-array>
And I have this Spinner in my layout XML:
<Spinner
android:id="#+id/cboTipoDireccion"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="#array/lst_addressTypes" />
And I want to delete some items programmatically.
I tried to do this like:
spinnerObject.removeViewAt(0)
but this threw an `InvalidOperationException
You can add String[] or ArrayList<String> for entries in your activity.
Adding:
List<String> entriesList = new ArrayList<>();
// add items into spinner dynamically
public void addItemsOnSpinner() {
String[] entries = getResources().getStringArray(R.array.lst_addressTypes);
entriesList = new ArrayList<String>(Arrays.asList(entries));
Spinner spinner = (Spinner) findViewById(R.id.cboTipoDireccion);
ArrayAdapter spinnerAdapter = new ArrayAdapter(this,android.R.layout.simple_spinner_item, entriesList);
spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(spinnerAdapter);
}
Removing:
entriesList.remove(0);
spinnerArrayAdapter.notifyDataSetChanged();

how to read items from string-array on android

I got a problem with reading items from string-array one by one. For example, i got string-array with 10 items:
<string-array name="arr">
<item>First</item>
<item>Second</item>
<item>...</item>
<item>Tenth</item>
</string-array>
So i know how to display items randomly, im using this code
Resources res = getResources();
myString = res.getStringArray(R.array.arr);
int length=myString.length;
int index=rgenerator.nextInt(length);
String q = myString[index];
tv = (TextView) findViewById(R.id.text);
tv.setText(q);
And in TextView on every button click it displays random item from array.
Problem is, how to make display item from string-array not randomly. Like, it starts from displaying First, then on click it displays Second, and so on untill end of array.
Please help!
You can't initialize your testArray field this way, because the application resources still aren't ready.
Change the code to:
package com.xtensivearts.episode.seven;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
public class Episode7 extends ListActivity {
String[] mTestArray;
/** Called when the activity is first created. */
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Create an ArrayAdapter that will contain all list items
ArrayAdapter<String> adapter;
mTestArray = = getResources().getStringArray(R.array.testArray);
/* Assign the name array to that adapter and
also choose a simple layout for the list items */
adapter = new ArrayAdapter<String>(
this,
android.R.layout.simple_list_item_1,
mTestArray);
// Assign the adapter to this ListActivity
setListAdapter(adapter);
}
}
Declare a variable
int currentIndex=0;
outside this onClick method.
In the
onClick(View v)
{
//Verify if only that btn is clicked
{
tv.setText(myString[(currentIndex++)%(myString.length)]);
}
}
Hope it works.
try
int i=0;
String q = myString[i];
i++;
strings.xml
<string-array name="month">
<item>Jan</item>
<item>Feb</item>
<item>Mar</item>
<item>Apr</item>
<item>May</item>
<item>Jun</item>
<item>Jul</item>
<item>Aug</item>
<item>Sep</item>
<item>Oct</item>
<item>Nov</item>
<item>Dec</item>
</string-array>
java
for (int i = 0; i < getResources().getStringArray(R.array.month).length; i++) {
Log.e("Array",""+getResources().getStringArray(R.array.month)[i]);
}

spinner adding string array on item selection how can get item related value in android

i am developing one spinner this spinner i am string array
spinner = (Spinner)this.findViewById(R.id.mlg);
final CharSequence[] itemArray =getResources().getTextArray(R.array.RectBeam);
final List<CharSequence> itemList =new ArrayList<CharSequence>(Arrays.asList(itemArray));
adapter = new ArrayAdapter(this,android.R.layout.simple_spinner_item,itemList);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
Toast.makeText(parent.getContext(), "The planet is " +
parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
}
............................
<string-array name="RectBeam">
<item value="3000000" > Steel</item></string-array>
this is the spinner related string array i am get the spinner item i am using parent.getItemAtPosition(pos).toString(),done my problem is particular item value how can get
example : steel----------->3000000
I am not sure either Spinner allow that attribute value in XML String or not but your problem can be solved like this.
Create two arrays in your array.xml file like:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="items">
<item>Nokia 1200</item>
<item>Nokia 1600</item>
<item>Nokia 5130</item>
</string-array>
<string-array name="values">
<item>1000</item>
<item>2000</item>
<item>5000</item>
</string-array>
</resources>
Now load first array in your adapter and store the second one in other Array to hold values of items as:
String items [] = getResources().getStringArray(R.array.items);
String values [] = getResources().getStringArray(R.array.values);
And you can simply get the respective item name and value in your onItemSelected() method like this:
String item = parent.getItemAtPosition(pos).toString();
String value = values [pos];

Data not loading into my Spinner

I'm trying to load an array of data into a Spinner component and it's throwing a NullPointerException. The code I'm using is below and it all seems okay.
Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<?> spin_adapter = ArrayAdapter.createFromResource(
this, R.array.letters_array, android.R.layout.simple_spinner_item);
spin_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(spin_adapter);
The string-array looks like this
<string-array name="letters_array">
<item>A</item>
<item>B</item>
<item>C</item>
<item>D</item>
<item>E</item>
<item>F</item>
<item>G</item>
<item>H</item>
<item>I</item>
<item>J</item>
<item>K</item>
<item>L</item>
<item>M</item>
<item>N</item>
<item>O</item>
<item>P</item>
<item>Q</item>
<item>R</item>
<item>S</item>
<item>T</item>
<item>U</item>
<item>V</item>
<item>W</item>
<item>X</item>
<item>Y</item>
<item>Z</item>
</string-array>
Is there a limit to the number of items in a Spinner or am I doing something else wrong?
Try this:
ArrayAdapter<CharSequence> spin_adapter = ...
In which xml-File is your Array stored and where is it located?

Categories

Resources