chanding contents of spinners run-time (android), tried and failed - android

I recently posted a question on how to change the values of a spinner during program execution and was told to change the array used to make the adaptor and call notifyDataSetChanged();
I tried that but my spinner is not getting updated even though my array is. I attach the code below
public void onCreate(Bundle savedInstanceState)
{
res=getResources();
Boolean a;
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// First spinner whose value determines the value of the second spinner
spinner = (Spinner) findViewById(R.id.spinner1);
// ArrayAdaptor of first spinner
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource( this R.array.planets_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner2 = (Spinner) findViewById(R.id.spinner2);
spinner2.setVisibility(4);// spinner 2 is not visible initially
// ArrayAdaptor of first spinner
adapter2 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, spinner_drop);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(adapter2);
Context context = getApplicationContext();
spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
}
public class MyOnItemSelectedListener implements OnItemSelectedListener
{
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
{
if(pos!=0)
{
//copying string array of second spinner from strings.xml to the current string array
spinner_drop=res.getStringArray(R.array.activities_array2);
Toast.makeText(parent.getContext(), "The planet is " +spinner_drop[0] , Toast.LENGTH_SHORT).show(); //this toast shows up
adapter2.notifyDataSetChanged();
spinner2.refreshDrawableState();
spinner2.setVisibility(0);//this command works and the spinner is visible, but it is empty
}
}
public void onNothingSelected(AdapterView parent)
{
// Do nothing.
}
}
Any idea what I am doing wrong any ideas will help.
PS: I have tried removing the entire invisible, visible thing, doesnt help
Thanks in advance

Modify your onItemSelected method as follows, note the new addition I added below.
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
if (pos!=0) {
//copying string array of second spinner from strings.xml to the current
//string array
spinner_drop=res.getStringArray(R.array.activities_array2);
Toast.makeText(parent.getContext(), "The planet is " +spinner_drop[0],
Toast.LENGTH_SHORT).show(); //this toast shows up
adapter2.notifyDataSetChanged();
spinner2.setAdapter(adapter2); // <--- New Addition
// spinner2.refreshDrawableState();
spinner2.setVisibility(0); //this command works and the spinner is visible,
//but it is empty
}
}

Related

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,..

how to shorten spinner selected item text in android?

I have used android custom spinner.
My custom spinner look like bellow
My List data is like
List<String> list = new ArrayList<String>();
list.add("001-00434016457-Md. Mokhhlesur Rahman");
list.add("001-00434016724-Mohammad Nazim Ullah");
list.add("001-00434016725-Mohsin Kabir");
list.add("001-00434016881-Maruf Pervaz Khan");
list.add("001-00434017171-Md.Nazmul Hasan");
My Spinner Adapter and setting data in spinner is like bellow
Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);
My problem is spinner's item text is large like "001-00434016881-Maruf Pervaz"
I want to select only id like 001-00434016881.
I don't want to get full text of the spinner item selection. How can I remove name part like -Maruf Pervaz Khan from 001-00434016881-Maruf Pervaz on item selection from spinner.
Please Help me
Try this. If the row item maintains same pattern as 001-00434016881-Md.Some Name
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this, adapter.getItem(position).substring(0,adapter.getItem(position).lastIndexOf("-")), Toast.LENGTH_LONG).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});

Selection on second spinner not working

I am developing a page with 2 spinners, where the first spinner has some category populated from DB.
Based On the value selected from spinner1, the selectedCategoryName is passed in the method setReportNamesContent().
I am also getting the values populated in the second spinner, but I am unable to do the selection in the 2nd spinner.
My idea is to pass both these selected values on click of a button, but always the 1st value from the 2nd spinner is getting passed on click of the button.
protected void setReportCategory(String loginName) {
map = DBManager.getInstance().reportCategoryListByLoggedInUser(loginName);
reportCategoryList = new ArrayList<String>(map.keySet());
Collections.sort(reportCategoryList);
reportCategorySpinnner = (Spinner) findViewById(R.id.ReportCategoryList);
reportCategorySpinnner.setOnItemSelectedListener(this);
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, reportCategoryList);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
reportCategorySpinnner.setAdapter(dataAdapter);
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
switch(parent.getId()){
case R.id.ReportCategoryList:
selectedCategoryName = parent.getItemAtPosition(position).toString();
break;
case R.id.ReportNameList:
selectedReportName = parent.getItemAtPosition(position).toString();
break;
}
if(!isEmptyOrNull(selectedCategoryName))
setReportNamesContent(selectedCategoryName);
}
private void setReportNamesContent(String selectedCategoryName) {
reportNameList = map.get(selectedCategoryName);
Collections.sort(reportNameList);
reportNameSpinnner = (Spinner) findViewById(R.id.ReportNameList);
reportNameSpinnner.setOnItemSelectedListener(this);
ArrayAdapter<String> dataAdapter1 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, reportNameList);
dataAdapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
reportNameSpinnner.setAdapter(dataAdapter1);
}
I had already reffered various question in this site and tried all of them and are not working. Please advice for a working answer

ActionBar Dropdown Spinner item defaults to first item

I'm trying to set the index of the item that needs to be selected in the spinner by default, but it always defaults to 0 (1st item)
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
SpinnerAdapter spinnerAdapter =
new ArrayAdapter<String>(activity, android.R.layout.simple_spinner_dropdown_item,
names);
int selectedIndex = actionBar.getSelectedNavigationIndex();
if (selectedIndex != targetIndex) {
actionBar.setSelectedNavigationItem(targetIndex);
}
Above if block is called always. Even after setting index 2, next time I check it returns 0.
Edit: I suspect getSelectedNavigationIndex gives index of actionBar item rather than Spinner dropdown item. If that is the case, what method sets the index of selected item inside dropdown?
Make sure you call setListNavigationCallbacks method before changing selected element. I can't see it in your example, so I think that's the problem.
Here is an example:
actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
actionBar.setListNavigationCallbacks(adapter, this);
actionBar.setSelectedNavigationItem(position);
It works in my app without any problems.
Have you tried using shared prefences to save the value of the selected spinner. I used this code to save the users selection with shared preferences so the next time they opened the app the spinner was set to the last value they chose:
Spinner Spinner = (Spinner) findViewById(R.id.Spinner);
String[] spinnervalues = getSpinnervalues();
ArrayAdapter<CharSequence> spinnerAdapter = new ArrayAdapter<CharSequence>(this, android.R.layout.simple_spinner_item, makes);
spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Spinner.setAdapter(spinnerAdapter);
int position = Utils.getIndex(getSpinnerval(), makes);
Spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
String selected = (String) parentView.getSelectedItem();
Spinner Spinner = (Spinner) findViewById(R.id.Spinner);
String[] spinnervalues = Filter.this.getSpinnerval(selected);
ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(Filter.this, android.R.layout.simple_spinner_item, models);
spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Spinner.setAdapter(adapter);
}
public void onNothingSelected(AdapterView<?> parentView) {}
});
Spinner.setSelection(makeposition, true);
int position = Utils.getIndex(getSpinnerval());
if (position >= 0) {
Spinner Spinner = (Spinner) findViewById(R.id.Spinner);
Spinner.setSelection(position, true);
}
Then the get Util:
public String getSpinnerval() {
return getSharedPreferences().getString("val", "");
}
I altered the code a little so it might not be 100% right but may give you an idea.

how to add item to Spinner's ArrayAdapter?

i had a EditText , a button and a spinner . When click the button , the spinner will add a new item with name you entered in the EditText. But here is the question, my adapter.add() method seems doesn't work...here is my code:
public class Spr extends Activity {
Button bt1;
EditText et;
ArrayAdapter<CharSequence> adapter;
Spinner spinner;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
bt1 = (Button)this.findViewById(R.id.bt1);
et = (EditText)this.findViewById(R.id.et);
spinner = (Spinner)this.findViewById(R.id.spr);
adapter = ArrayAdapter.createFromResource(
this, R.array.planets_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
bt1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String temp = et.getText().toString();
adapter.add(temp);
adapter.notifyDataSetChanged();
spinner.setAdapter(adapter);
}
});
spinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener(){
#Override
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();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}});
}
}
thanks! ...still waitting
When you have created your ArrayAdapter you haven't assigned a resizeable List to it, so when you do add() it cannot increment the size of it and throws a UnsupportedOperationException.
Try something like this:
List<CharSequence> planets = new ArrayList<CharSequence>();
adapter = new ArrayAdapter<CharSequence>(context,
R.array.planets_array, planets);
//now you can call adapter.add()
You should use a List. With an Array such as CharSequence[] you would get the same UnsupportedOperationException exception.
Javi is right except don't reference an array for the second parameter.
adapter = new ArrayAdapter<CharSequence>(this,
android.R.layout.simple_spinner_item,
someList);
I believe this is working as designed, but not as expected. ArrayAdapter used to only take an array, but the list constructor was added later. I'm guessing its just doing a toArray() on your list. This is why you have to either call add on the adapter, or create a new adapter when your List changes.
you can create an arraylist and copy all recourse to this object then create arrayadaptor and send this arraylist and in onclicklistener of button, add edittext content to arraylist object then call notifydatasetchanged of adator

Categories

Resources