Data not loading into my Spinner - android

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?

Related

Spinner, string-array onclick performance

I am making a self service form with a set of questions during user registration.
Here is the string.xml file
<resources>
sources>
<string name="question1">What was your childhood nickname?</string>
<string name="question2">What is the name of your favorite childhood friend?</string>
<string name="question3">In what city or town did your mother and father meet?</string>
<string name="question4">What is your favorite team?</string>
<string name="question5">What was your favorite sport in high school?</string>
<string name="question6">What was your favorite food as a child?</string>
<string name="question7">What was the make and model of your first car?</string>
<string name="question8">What is you mother\'s maiden name?</string>
<string name="question9">In which city/town were you born?</string>
<string name="question10">In what town/city was your first job?</string>
<string-array name="questions">
<item>Select a question</item>
<item>#string/question1</item>
<item>#string/question2</item>
<item>#string/question3</item>
<item>#string/question4</item>
<item>#string/question5</item>
<item>#string/question6</item>
<item>#string/question7</item>
<item>#string/question8</item>
<item>#string/question9</item>
<item>#string/question10</item>
</string-array>
I have pulled these questions into 3 different spinners in the activity xml file.
activity_self_service.xml
Spinner spinner1 = (Spinner) findViewById(R.id.spinner_q1);
Spinner spinner2 = (Spinner) findViewById(R.id.spinner_q2);
Spinner spinner3 = (Spinner) findViewById(R.id.spinner_q3);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter1 = ArrayAdapter.createFromResource(this, R.array.questions, android.R.layout.simple_spinner_item);
ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(this, R.array.questions, android.R.layout.simple_spinner_item);
ArrayAdapter<CharSequence> adapter3 = ArrayAdapter.createFromResource(this, R.array.questions, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
adapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setPrompt("Select a security question");
spinner2.setPrompt("Select a security question");
spinner3.setPrompt("Select a security question");
// Apply the adapter to the spinner
spinner1.setAdapter(adapter1);
spinner2.setAdapter(adapter2);
spinner3.setAdapter(adapter3);
Say the user selected question 3, question 5 and question 6 in 3 different spinners, I want to store the indexes of these questions and the answers in the database.
So, I want something like (3,answer 1) ,(5, answer 2), (6, answer 3) in the DB.
I tried to get the indexes but couldnt find a way.
Please help.
Set "OnItemSelectedListener" on all your spinners, it will give you the index when an item is selected.

Spinner ArrayAdapter using Resource from R.array

final String[] sortText = new String[]{"Date Uploaded", "File Name", "Up-loader", "File Size"};
Spinner sort = (Spinner) v.findViewById(R.id.sort);
sort.setAdapter(new ArrayAdapter<>(context, R.layout.sort_row_layout, R.id.sortTV, sortText));
The issue I'm having is that its unable to create constructor when I try to setup the adapter when pulling the array from resources so I can implement internationalization and have the text shown in different languages as so..
<resources>
<array name="sort">
<item>Date Uploaded</item>
<item>File Name</item>
<item>Up-loader</item>
<item>File Size</item>
</array>
</resources>
sort.setAdapter(new ArrayAdapter<>(context, R.layout.sort_row_layout, R.id.sortTV, R.array.sort));
What could the issue be here?
ArrayAdapter doesn't have a constructor you can pass a reference to an array resource to like that. See the docs
You want to use createFromResource() and setDropDownViewResource() like this:
ArrayAdapter<CharSequence> sortAdapter = ArrayAdapter.createFromResource(context, R.array.sort, R.layout.sortTV);
sortAdapter.setDropDownViewResource(R.layout.sort_row_layout);
sort.setAdapter(sortAdapter);
EDIT
For a standard, system default styled Spinner just use the built in layouts:
ArrayAdapter<CharSequence> sortAdapter = ArrayAdapter.createFromResource(context, R.array.sort, android.R.layout.simple_spinner_item);
sortAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sort.setAdapter(sortAdapter);

Spinner Android adapter in main Activity

Trying to simply get a Spinner going in my Application but the lines (commented out, the app works fine without these 2 lines) give me errors every time I try to start the Activity. I setup an Array in Strings.XML to be used in conjunction with the Spinner to view the data.
My XML contains a Spinner like so:
<Spinner
android:id="#+id/spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
In Strings.XML I have my Array:
<string-array name="spinner_array">
<item>Mercury</item>
<item>Venus</item>
<item>Earth</item>
<item>Mars</item>
<item>Jupiter</item>
<item>Saturn</item>
<item>Uranus</item>
<item>Neptune</item>
</string-array>
My Main Activity, 2 lines that are commented cause error.
public class BusPurchase extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.spinner_array, android.R.layout.simple_spinner_item);
super.onCreate(savedInstanceState);
//adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//spinner.setAdapter(adapter);
setContentView(R.layout.activity_bus_purchase);
}
Log Cat Displays this:
http://chopapp.com/#cbz5r823
call setContentView before accessing views from xml layout as:
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bus_purchase); // set layout here
Spinner spinner = (Spinner) findViewById(R.id.spinner);
//..your code here
}
because you are trying to access views before setting layout for Activity
You are getting a Null Pointer on your references to spinner (R.id.spinner) because you are trying to reference items in your layout before you inflate the layout. Try calling setContentView(R.layout.activity_bus_purchase); first thing in your onCreate() method and see if that doesn't fix it.

where to declare array of items for the spinner

I have been going through spinners for my application but I don't know where to declare the items that are to be shown in the spinner i.e either I declare them in the string.xml or in my main_activity like this ..
String[] data = { "Hello", "There", "I", "Am", "Taking", "Values" };
Which method is best to use and why?
After declaring values in resources tag in strings.xml like below:
<string-array name="spinner_values">
<item>a</item>
<item>b</item>
</string-array>
you can use entries attribute in spinner tag in your xml layout, instead of use it pro-grammatically in java file. Like below:
<Spinner
android:id="#+id/my_spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="#array/spinner_values" />
Declaring the array in string.xml is better because:
If you want to translate your app into another language, you just
need this one file which has all the arrays and strings that your app uses.
If you have to make any changes to any string/array, you know by default where to look for(All at one place).
Re-use is possible using this way. In any other activity you can just use from there, instead of declaring again.
you can try in this way ....
public void addItemsOnSpinner1() {
spinner1 = (Spinner) findViewById(R.id.spinner1);
List<String> list = new ArrayList<String>();
list.add("Small");
list.add("Medium");
list.add("Large");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(dataAdapter);
}
public void addListenerOnSpinnerItemSelection() {
spinner1 = (Spinner) findViewById(R.id.spinner1);
apply = (Button) findViewById(R.id.apply);
spinner1.setOnItemSelectedListener(new CustomOnItemSelectedListener());
apply.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(String.valueOf(spinner1.getSelectedItem()).equals("Small"))
{
// your code
}
if(String.valueOf(spinner1.getSelectedItem()).equals("Medium"))
{
// your code
}
else if(String.valueOf(spinner1.getSelectedItem()).equals("Large"))
{
// your code
}
}
});
If content of your spinner is fixed then declare it in String.xml . its best practice
You should declare array in strings.xml
<string-array name="data">
<item>Hello</item>
<item>There</item>
<item>I am</item>
<item>Taking</item>
</string-array>
and obtain values in activity
String [] array =getResources().getStringArray(R.array.data);

Why is my dynamic spinner not populating

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);

Categories

Resources