Problems with creating spinner - android

In my app I'm developing, I have a spinner in an alert dialog box.
The spinner works fine, but when I added the following lines to add the array to the spinner, my app crashing a few seconds after starting up:
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.newFileTypeArray, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
newFileType.setAdapter(adapter);
What am I doing wrong?
These Android spinners seem to be a bit complicated as well, I don't think I'll be able to remember how to make them without referencing to the Android docs.

Problem solved.
I realised that the following line:
final Spinner newfiletypespinner = (Spinner) findViewById(R.id.newfiletypespinner);
I had to change to:
final Spinner newfiletypespinner = (Spinner) newFileDialogInflated.findViewById(R.id.newfiletypespinner);
With "newFileDialogInflated" being the previously inflated view so I could have a custom AlertDialog view:
final View newFileDialogInflated = View.inflate(this, R.layout.newfileview, null);
But thanks for the help!

Hard to say for sure whether you've omitted it from your snippet or omitted it from your code, but do you initialize newFileType as a spinner?
Spinner newFileType = (Spinner)findViewById(R.id.newFileTypeSpinner);
or similar? If you're trying to set its adapter before initializing it, that would explain it.

Related

Why getApplicationContext and Activity context inflate different style of the spinner?

I have a strange problem. I have 2 pre-defined spinner in the xml. However, the item and related dropdown item are rendering in runtime.
However, the following 2 statement provide different result of the layout. I have no idea why the result is like this. But the main difference is getApplicationContext() and this
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, getSrvNumList());
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(),
android.R.layout.simple_spinner_item, getSrvNumList());
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
As #MikeM. commented, it is the issue about the context. Hope the following answer can help someone with this issue.
Use Activity context instead of Application context
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MyActivity.this,
android.R.layout.simple_spinner_item, getSrvNumList());
Otherwise, the application will get different theme to render the spinner.
According to this post (look for Chapter 3), we can consider 2 types of Context: UI-Context (for example getActivity(), view.getContext()...) and non-UI-Context (such as getApplicationContext()...).
When inflate view, let use UI-Context to keep its application theme. That rule is:
* Do you need to access UI related stuff? Use UI-Context.
* Otherwise, Use Non-UI Context.

how to disable spinner click? setclickable(false) not working

i want to disable dropdown list of spinner if it have only one item, i have almost tried everything but still i didn't get the solution, some times i got nullpointerexception while trying any solution, here is my code.
ArrayAdapter<String> adapter =new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_spinner_item,str_fname);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp_childName.setAdapter(adapter);
if(str_fname.length==1){
sp_childName.setBackground(null);
sp_childName.setClickable(false);
tv_childClassname.setText(str_className[0]);
}else{
sp_childName.setOnItemSelectedListener(new onItemSelected());
}

Listview and dialog in android

I have a small custom dialog in wich I have a ListView with id:list. I want to populate it with strings I have in my resources (R.array.tones), but I have really big problems making this work, have tried many different solutions this is the latest which I think would work but it throws an null pointer exception on the toneList.
Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.tone_dialog);
dialog.setTitle(R.string.tonePromptTitle);
ListView toneList = (ListView)findViewById(R.id.list);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.tones, android.R.layout.simple_list_item_1);
toneList.setAdapter(adapter);
dialog.show();
My class is just extending Activity not ListActivity, and I would like to have it that way otherwise I must create a new class just for the listview.
I apologize for the long code, I'm new here and hasn't really figured out all functionality yet.
You should look for the View inside of the dialog:
ListView toneList = (ListView)dialog.findViewById(R.id.list);
:)
You are not searching your dialog layout for toneList. Try the following instead:
ListView toneList = (ListView)dialog.findViewById(R.id.list);

Problem in Android Spinner inside the Tabbars

I am making an application and i am facing a problem.
I am using Tab bars and in one of my Tab bar i am using Spinner.
It loads Perfectly but when i click on it. it gives me:
android.view.WindowManager$BadTokenException: Unable to add window -- token android.app.LocalActivityManager$LocalActivityRecord#44647ef8 is not valid; is your activity running?
This error.
i am using the following code snippet
ArrayList<String> ageList;
Spinner age;
age = (Spinner) findViewById(R.id.country);
ageList = new ArrayList<String>();
ageList.add("10-20");
ageList.add("21-35");
ageList.add("36-60");
ageList.add("61-100");
ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, ageList); //array you are populating
adapter2.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
age.setAdapter(adapter2);
age.setSelection(0, true);
This code work fine when i am using seperate activity not the tab bars. but in tab bars it gives me the above error exception when i click on the spinner to open a list.
Please guide me Thanks a bunch
I m done with this
the problem was with my layout
setContentView(R.layout.age)
Instead of this i used Layout Inflator as below:
View viewToLoad = LayoutInflater.from(this.getParent()).inflate(R.layout.country, null);
this.setContentView(viewToLoad);
and then called the Spinner n Bingooo! It works Just Perfect
Thanks all of you.. :)

Using a spinner on another view with a different class defined (Android)

Having a problem loading an array into a spinner that is located on a different view. The array is defined properly in arrays.xml with a name of beerstyles. The beerstylespinner is defined as the id of a spinner in carbonationcalculator_view.xml. This works when the code is in the main java class but not the additional carbonationcalculator class. Everything works with the exception of the spinner not being populated with the array.
Here is the code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.carbonationcalculator_view);
Spinner s = (Spinner) findViewById(R.id.beerstylespinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.beerstyles, R.layout.carbonationcalculator_view);
adapter.setDropDownViewResource(R.layout.carbonationcalculator_view);
s.setAdapter(adapter);
}
I'm not sure this will fix the problem, but there seems to be some confusion on what layout to set for the Adapter's drop down resource. setContentView() should be used for the view you want set for the activity; however, the drop down resource should be what you want each row to look like.
What you should be using is a something like android.R.layout.simple_list_item_1. You can emulate the demo List1.java, but instead of their constructor you would use:
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.beerstyles, android.R.layout.simple_list_item_1);
Also, you don't need to call ArrayAdapter.setDropDownViewResource() after using ArrayAdapter.createFromResource() - the third parameter is the drop down view resource.

Categories

Resources