Null pointer exception while dealing with spinners - android

I am getting a null pointer exception when i am running this peice of code
private void loadSpinnerData() {
// database handler
AttendAppDatasource db = new AttendAppDatasource(getApplicationContext());
// Spinner Drop down elements
List<String> clas = db.viewCol(AttendAppDbOpenHelper.COLUMN_CLASS);
Log.i("Select Class","List Assigned");
// Creating adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, clas);
// Drop down layout style - list view with radio button
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// attaching data adapter to spinner
spinner.setAdapter(dataAdapter);
}
this is the relevant log output
this is the log out put corresponding to the list

Your spinner is set to null. You need to get it from your resources
Spinner spinner = (Spinner) findViewById(R.id.'spinner_id_name');
Then you can set your adapter :)

Related

I have this json response.... i want to add it to my android spinner? How i add this

{"state":[{"state_id":"101","state_name":"Haryana"},{"state_id":"102","state_name":"Punjab"}]}
this is my json response.... i want to add it to my spinner in android.
Create a list and add all the values you need to show in your spinner
List<String> list = new ArrayList<String>();
List<String> listID = new ArrayList<String>();
list.add("state name");
list.add("state id");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Create an adapter and set the adapter to your spinner.
spinner.setAdapter(dataAdapter);
now get the spinner selected item position
int position= spinner.getSelectedItemPosition();
with this position value you can get state code in the arraylist

Set spinner title when activity is created

I have a spinner whose elements set by an arrayList.this arraylist gets data by calling an api. I have set title at 0th position of arrayList .the problem is i want to show this title as activity is created. but this will set only when api calling is successful .it takes some time to load data. can anyone give me better idea to set title to spinner.
If data comes from api then you can add dialog on screen and hide title until api call success.. when you get response true then show titleview and add title from api. and api response is false then show error.. so user will get better view..
Thanks
Do like this
ArrayAdapter stringArrayAdapter = new ArrayAdapter(this,
R.layout.spinner_item, CatagoryName);
// create a spinner
Spinner spinner = (Spinner) findViewById(R.id.spinner);
// add adapter to spinner
spinner.setAdapter(stringArrayAdapter);
// create listener and add to spinner
spinner.setOnItemSelectedListener(new
AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int
position, long id) {
// put code which recognize a selected element
}
Try this way
ArrayList<String> list = new ArrayList<>();
list.add(0,"Please Select");
ArrayAdapter stringArrayAdapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, CatagoryName);
Spinner spinner = (Spinner) findViewById(R.id.spinner);
spinner.setAdapter(stringArrayAdapter);
// now call the API for data
//After data received, you have to make temp list to store response
ArrayList<String> Templist = new ArrayList<>();
//Templist = Server response
//now add templist in main List
list.addAll(Templist);
// and call this
stringArrayAdapter.notifyDataSetChanged();

How to change multi Spinner's value

I use two Spinner and get data from asyncTask when I get first spinner and want get the 2th spinner but when setadapter .. two spanner's data are the same...
This is i setadapter first time.
List<String> none=new ArrayList<String>();
none.add("none");
ArrayAdapter<String> adapterchoseTime =
new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, none);
adapterchoseTime.setDropDownViewResource(
android.R.layout.simple_spinner_dropdown_item);
ArrayAdapter<String> adapterchoseProm =
new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, none);
adapterchoseProm.setDropDownViewResource(
android.R.layout.simple_spinner_dropdown_item);
choseTime.setAdapter(adapterchoseTime);
choseProm.setAdapter(adapterchoseProm);
I get Spinner 1's data
ArrayAdapter<String> adapterchoseTime=(ArrayAdapter<String>)choseTime.getAdapter();
adapterchoseTime.clear();
adapterchoseTime.addAll(time);
choseTime.refreshDrawableState();
and i get spinner2's data
ArrayAdapter<String> adapterchoseTime = (ArrayAdapter<String>)choseTime.getAdapter();
adapterchoseTime.clear();
adapterchoseTime.addAll(time);
ArrayAdapter<String> adapterchoseProm = (ArrayAdapter<String>)choseProm.getAdapter();
adapterchoseProm.clear();
adapterchoseProm.addAll(prom);
choseTime.setAdapter(adapterchoseTime);
choseProm.setAdapter(adapterchoseProm);
And then I get two same spinner...
It looks like you're referencing the same spinner from your setDropDownViewResource.
adapterchoseTime.setDropDownViewResource(
android.R.layout.simple_spinner_dropdown_item);
One of these needs to reference the second spinner.

Array Adapter Issue

I'm trying to input an array into a ListView. I've gotten it to work for a spin box with this code:
Spinner spinner = (Spinner) findViewById(R.id.location_spinner);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, model.getLocationsArray());
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
When I run the next block of code the program closes unexpectedly. Strangely if I remove model.getLocationsArray() it runs but the view won't update.
ListView listView = (ListView) findViewById(R.id.available_locations_list);
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, model.getLocationsArray());
listView.setAdapter(adapter);
Thanks in advance!
model.getLocationsArray() instead this... you can use directly Arraylist object. If you have.Nullpointer Exc. because of from your EditText value cant be added to your ArrayList.

Get and Set values form spinner

I'm looking for to get and set values form spinner loades from database but i don't find it.
private void loadSpinnerData() {
// database handler
DatabaseHandler db = new DatabaseHandler(getApplicationContext());
// Spinner Drop down elements
List<String> lables = db.getAllCadenesAsList();
// Creating adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, lables);
// Drop down layout style - list view with radio button
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// attaching data adapter to spinner
spinner.setAdapter(dataAdapter);
}
The cursor returns _id and Name and the spinner load correctly (it seems..)
Now when I try to get de selected valued in onItemSelected, I the id and the position is the same, and is not a id of cursor, it is the posicion in de spinner.
How can I get de ID of selected value?
How can I set the value of spinner.
I need and I know the id of database, not the id/position of spinner.
Thanks in advance.
You're not getting a real database _id on onItemSelected of the spinner because you hooked the spinner up to an ArrayAdapter. This sort of adapter hookup is not in any way connected to your database but only the List<String> or other collection you passed into it. To get what you want, you need to set the spinner's adapter to a CursorAdapter instead.

Categories

Resources