Display Data in Spinner,Android - android

I wanted to display my data into android spinner from database.
I used two spinner..
2nd spinner should reflect once 1st spinner item is selected, every thing is working fine..data is loaded into the second spinner, but not displaying into 2nd spinner when 2nd spinner item is selected.
Spinner1 = (Spinner)findViewById(R.id.createProfileCitySpinnerId);
Spinner2 = (Spinner)findViewById(R.id.createProfileStateSpinnerId);
//for 1st spinner.....(working)
final List<String> list1 = new ArrayList<String>();
ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item,list1);
Spinner1.setAdapter(adapter1);
//for second spinner...
final List<String> list2 = new ArrayList<String>();
Spinner1.setOnItemSelectedListener(new OnItemSelectedListener(){
#Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
{
if(//some condition using id)
{
list2.add(stateCursor.getString(1));
}
}
#Override
public void onNothingSelected(AdapterView<?> parent)
{
}
});
//everything is working data is loading, but not display once item is selected on 2nd spinner
ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,list2);
stateSpinner.setAdapter(adapter2);

You need to add the element directly to the adapter (not to list2), the ArrayAdapter keeps its own internal data. Try this code on your onItemSelected:
adapter2.add(stateCursor.getString(1));
adapter2.notifyDataSetChanged();

Call adapter2.notifyDataSetChanged();
such as
#Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
{
if(//some condition using id)
{
list2.add(stateCursor.getString(1));
adapter2.notifyDataSetChanged();
}
}
see http://developer.android.com/reference/android/widget/ArrayAdapter.html for more information.
public void notifyDataSetChanged () Since: API Level 1
Notifies the attached observers that the underlying data has been
changed and any View reflecting the data set should refresh itself.

Related

android: spinner not updating my view when notifyDataSetChanged() is called

I am trying to update the spinner view by calling notifyDataSetChanged(). But its not updating.
The below is my Activity:
public class DisplayArchive2 extends AppCompatActivity {
List<String> yearslist;
Spinner spinner;
ArrayAdapter<String> dataAdapter;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.archive2);
spinner = (Spinner) findViewById(R.id.year_spinner);
yearslist = new ArrayList<String>();
yearslist.add("2017");
yearslist.add("2016");
yearslist.add("2015");
yearslist.add("2014");
yearslist.add("2013");
dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,yearslist);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
yearslist.clear();
yearslist.add("2010");
yearslist.add("2009");
yearslist.add("2008");
yearslist.add("2007");
yearslist.add("2006");
dataAdapter.notifyDataSetChanged();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
}
The list of years is not changing on selecting any year.
It looks like below
Try this use dataAdapter2.setNotifyOnChange(true); its working fine i have tested it my device
void setNotifyOnChange (boolean notifyOnChange)
Control whether methods that change the list (add(T), addAll(Collection), addAll(Object[]), insert(T, int), remove(T), clear(), sort(Comparator)) automatically call notifyDataSetChanged(). If set to false, caller must manually call notifyDataSetChanged() to have the changes reflected in the attached view. The default is true, and calling notifyDataSetChanged() resets the flag to true.
CODE
List<String> yearslist;
Spinner spinner;
ArrayAdapter<String> dataAdapter2;
spinner = (Spinner) findViewById(R.id.year_spinner);
yearslist = new ArrayList<String>();
yearslist.add("2017");
yearslist.add("2016");
yearslist.add("2015");
yearslist.add("2014");
yearslist.add("2013");
dataAdapter2 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,yearslist);
dataAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter2);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (position == 0) {
} else {
yearslist.clear();
yearslist.add("2010");
yearslist.add("2009");
yearslist.add("2008");
yearslist.add("2007");
yearslist.add("2006");
dataAdapter2.setNotifyOnChange(true);
dataAdapter2.notifyDataSetChanged();
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
As per my understanding. You are notifying the adapter inside onItemSelected(). In case of Spinner adapter initialization onItemSelected() will get called once for first time . So the list you will get in Spinner is always the new one(i.e 2006-2010) never the first one(i.e 2013-2017).
So if you do notify adapter on some other action or maybe avoid the first call to onItemSelected() your code will work fine .

am trying that when I click on spinner item, list view shows further details of that item

When I click a spinner item,I would like list view to shows more details on that item.
i.e if I select Student from Spinner then list view should show student names.
spin.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(SpinnerActivity.this, parent.getItemAtPosition(position)+" ", Toast.LENGTH_LONG).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
What is the right way to do it ?
Create an ArrayList with Strings(Student names). Now create an ArrayAdapter in onCreate() which contains the Students ArrayList.
In the EventListener(onItemSelected) of the Spinner, check for the Spinner-item text. If it's "Students" set the ArrayAdapter of the Spinner to the Students-ArrayAdapter which you initialized in onCreate();
onCreate():
arrayList = new ArrayList<String>();
arrayList.add("Bob");
adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item, arrayList);
onItemSelected EventListener:
if(view.getText().toString().equals("Students"){
listView.setAdapter(adapter);
}

updating spinner based on another spinner's selected item

What is wrong with this code?
I'm having two spinners. One for 'category' and the second for 'subcategory'. When an item is chosen in the category spinner I want to re-load the relevant sub-categories accordingly.
mSpnrCategory.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// SubCategories
String[] aSubCategory = new String[SUBCATS.length];
... Some code for preparing aSubCategory.
ArrayAdapter<String> dataAdapterSubCategories = new ArrayAdapter<String>(NewRequestActivity.this, android.R.layout.simple_spinner_item, aSubCategory);
dataAdapterSubCategories.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mSpnrSubCategory.setAdapter(dataAdapterSubCategories);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
when the activity is loaded this code is called and it works fine loading the subcategories of the first category. However, when I change category manually this code is called but results with empty spinner for subcategories. I can't figure out why. Any ideas?
Define your subcategory adapter (mSpnrSubCategory) as a global variable.
Then whenever category spinner click, change the data of subcategory then notifyDataSetChanged();
ArrayAdapter<String> mSpnrSubCategory;
ArrayList<String> aSubCategory;
mSpnrCategory.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// SubCategories
...
mSpnrSubCategory.notifyDataSetChanged();
}
});
I suggest to use ArrayList<String> instead of String[]
Hope this help

How to add default value in Spinner other then adapter Value

i have some data coming from server and i have to show that value in Android Spinner.
Adapter that is attach to Spinner also getiing from server.
please let me know how to show initial value coming from server.and show adapter value after click on Spinner.
Spinner mySpinner = (Spinner) findViewById(R.id.householdspinner);
mySpinner.setAdapter(new ArrayAdapter<String>(Edit_Voter_Information.this,
android.R.layout.simple_spinner_dropdown_item,
householdIncome));
mySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0,
View arg1, int position, long arg3) {
String item = arg0.getItemAtPosition(position).toString();
house_Hold_Income = item;
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
}
Initially i stored some values in String[] names. when activity is run spinner position 0(zero)is selected by default.
Check my code below:
ArrayAdapter arrayAdapter = new ArrayAdapter(this, R.layout.spinner_style, names);
spinner.setAdapter(arrayAdapter);
spinner.getSelectedItemPosition();//which returns names[0] initially.
if click other position(i) it returns names[i], where i =0,1,2,..

OnClickListener for list view?

How can I insert an OnClickListener for list view? I want to show an alert dialog with two buttons, "yes" and "no", when I click an item.
private void loadListViewData() {
DatabaseHandler db = new DatabaseHandler(getApplicationContext());
List<String> lables = db.getAllLabels();
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(
this,
android.R.layout.simple_dropdown_item_1line,
lables);
listview.setAdapter(dataAdapter);
}
If you want to set Click Listener on item, then use setOnItemClickListener, on listview, using this method you will have access to clicked item position too, simply do as below:
listView.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView adapterView, View convertView, int position, long id)
{
//Do some operation
}
});

Categories

Resources