Spinner within a tab - android

i have a problem with the spinner in a tab host:
in fact i successed to make dynamically using
Spinner spinner = new Spinner(isChild() ? getParent() : this);
but i want it defined by the XML file. like this
Spinner spinner = (Spinner) this.findViewById(R.id.widget10);
I got the problem when i use (OnItemSelectedListener())
when opening the the dialogue of selection??
please i need a help,
think u.

Let us suppose, there are two class1 and class2
In class1 define a method
public static View makeSpinner(Context context) {
View v = LayoutInflater.from(context).inflate(R.layout.spinner, null);
Spinner spinner = (Spinner) v.findViewById(R.id.spinner);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(context, android.R.layout.simple_spinner_dropdown_item);
adapter.add("one");
adapter.add("two");
adapter.add("three");
spinner.setAdapter(adapter);
return v;
}
and in the class2 where u need to access the spinner define it's setContentView as below:-
setContentView(MainActivity.makeSpinner(getParent()));

I found a different solution.
My problem was that I had a MapView in the same activity as the spinner. Because of this I couldn't use the described technique.
And actually I didn't wanted to redesign my whole project so I just replaced my spinner in the XML-File with an FrameLayout.
Now my code looks like this:
Spinner spinner = new Spinner(isChild() ? getParent() : this);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getParent(),
android.R.layout.simple_spinner_item, data);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
FrameLayout frame = (FrameLayout)findViewById(R.id.frameLayoutSpinnerTarget);
frame.addView(spinner);

Related

Create Spinner and Adapter without any XML

I can create a very simple Spinner with code like this
final List<String> categories = new ArrayList<>();
categories.add("First");
categories.add("Second");
categories.add("Third");
final Spinner spinner = new Spinner(context);
final ArrayAdapter<String> dataAdapter = new ArrayAdapter<>(context, R.layout.spinner, categories);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);
however, this requires R.layout.spinner in my app. How can I avoid this XML with own, pure Java code, e.g. by implementing an own ArrayAdapter-like class?
Extend you custom adapter from ArrayAdapter and try to override getView() with your own code. You should be able to create the adapter with a null resource id. You will have to build you own views since you will no longer be using a resource.

Using listView and spinner in 1 layout

I use listview with cursor adapter and spinner in 1 layout.
I don't know how to use setAdapter into these 2 different thing.
Spinner sp = (Spinner) findViewById(R.id.spinner);
SpinnerAdapter adapter = new SpinnerAdapter(this, R.layout.spinner_layou, R.id.txt, item);
//sp.setAdapter(adapter); <----- I should have to set my spinner adapter here, but it seems to make it crash
cursorAdapter = new NotesCursorAdapter(this, null, 0); //my own class which extend CursorAdapter
ListView list = (ListView) findViewById(android.R.id.list);
list.setAdapter(cursorAdapter);
I have google it and find some clue by using ArrayAdapter. But what i found is ArrayAdapter used for the same type adapter (in my case, I use 2 different adapter(Spinner and CursorAdapter) CMIIW. Is anyone can help me on this?

How to programatically set entries of spinner in android?

I have made various string-arrays in the string.xml file and I have to set different arrays as entries for the spinner according to certain condition in Java. Is it possible or is database the only way to do so. Thanks in advance.
You need to use an adapter and populate with tha array in xml file.
Specify the name of your array in xml at createFromResource method (second parameter).
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.my_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mySpinner.setAdapter(adapter);
You have to extract your data from file:
String[] testArray = getResources().getStringArray(R.array.testArray);
Then, you have to inflate in the spinner:
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item, testArray );
mySpinner.setAdapter(spinnerArrayAdapter);
You can start with using ArrayAdapter, it is a simple class to populate spinner items programmatically.
String data[];
//... do your stuff to get populate this array
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item, data);
mySpinner.setAdapter(spinnerArrayAdapter);
You can also modify the view of dropdown items and customize them further by overriding this class.

how to access spinner inside different layout android

I've been trying to solve a problem which i been having for past week , i am trying to a access spinner inside different xml layout, all i want is to access the spinner and add array to the spinner.
This is the code i am using and it doesn't work
setContentView(R.layout.mainreg);
LinearLayout mainLayout = (LinearLayout)findViewById(R.id.main_layout_id);
View view123 = getLayoutInflater().inflate(R.layout.one2reg, mainLayout,false);
Spinner spin = (Spinner) view123.findViewById(R.id.spinnerproblem);
ArrayAdapter<String> adapter =
new ArrayAdapter<String> (this, android.R.layout.simple_spinner_item,items);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin.setAdapter(adapter);
mainLayout.addView(view123);
pass true for attach to root while inflating layout, like this.
View view123 = getLayoutInflater().inflate(R.layout.one2reg, mainLayout,true);

Android application crashes when populating spinner

I'm trying to populate the spinner, but the application crashes on spiner.setAdapter(adapter)...
final Context c=this;
ArrayList<CountryItem> countriesArray = GetCountries1();
ArrayAdapter<CountryItem> adapter = new ArrayAdapter<CountryItem>(
c,android.R.layout.simple_spinner_item, countriesArray);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
final Spinner spiner=(Spinner)findViewById(R.id.spinner1);
spiner.setAdapter(adapter);
I can't find any errors here, and eclipse debug mode does not show anything usefull...
Here is the example from google...is it so much different than my code?
Spinner spinner = (Spinner) findViewById(R.id.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);
What error do you get?
Did you check if the countriesArray has elements or if it is empty?
Is it working when you use, e.g., a simple string array? If yes, then the problem lies with the CountryItem objects.
Other than that, the only difference I see is that you pass the context as a final variable, but I don't know if this causes the error. Try to pass just this and see if it works.

Categories

Resources