I'm working with spinners plus the arrayadapters that come with them but I just can't get my head around what setDropDownViewResource() does. How is it related to the layout you can pass when contructing the arrayadapter (i.e. Context context, int textViewResourceId, List objects))?
ArrayAdapter<String> adp1;
ArrayList<String> arrayList1 = new ArrayList<String>(Arrays.asList(getResources().getStringArray(R.array.sp1_array)));
Spinner sp1 = (Spinner) view.findViewById(R.id.spinner1);
adp1 = new ArrayAdapter<String> (mcntxt, R.layout.my_simple_list_item, arrayList1);
adp1.setDropDownViewResource(R.layout.my_simple_dropdown_item_1line);
sp1.setAdapter(adp1);
The ID you are using on the constructor will be used for the Spinner main window, or prompt, as the documentation states.
When you call setDropDownViewResource(), you are establishing the layout to be used for the itens on the spinners dropdown list. Note that this applies for spinners used as dropdown.
Related
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?
String[] s={"1","2","3","4"};
ArrayAdapter<String> adp=new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_spinner_dropdown_item,s);
spin.setAdapter(adp);
Now I want to get "4" at zero position of Spinner.
AFAIK, you can't change the position of the items inside a Spinner after you have set its Adapter.
If you want 4 to be the first item, you have to order your list accordingly:
String[] s={"4","1","2","3"};
ArrayAdapter<String> adp = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_dropdown_item, s);
spin.setAdapter(adp);
need to do so when you click on a specific button (application type translator and arrow key) changed one of the selected items to another spinner itam swapped Well, here is how it can be implemented?
here is the code but it does not work!
int spinner1Index = spinner.getSelectedItemPosition();
spinner.setSelection(spinner2.getSelectedItemPosition());
spinner2.setSelection(spinnerfirst);
can offer something similar or fix!
You would have to change the adapter of your spinner and call notifyDataSetChanged()
I would suggest adding this to your switch case for when the item is selected.
Set a custom adapter for the spinner on item click.
//An array of what you want to populate the spinner with
Array spinnerArray [] = {"One", "Two", "Etc.."};
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item, spinnerArray);
spinnerArrayAdapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item );
//Before setting the adapter, clear and notify the previous ArrayList used
arrayList.clear();
arrayList.notify();
spinner.setAdapter(spinnerArrayAdapter);
I have a question that, I want to load items in a spinner on ItemClick of another Spinner. Actually I two spinners, data is loaded into first spinner from json_parsing and I have to load data in second Spinner after selecting an item from first spinner, So, I don't know how it will implemented? Please suggest me the right solution.
Thanks in advance.
You can do it like this,
First time you data will be loaded in First and Second Spinner.
On Selection of item from First Spinner do this.
1.) Clear the previous ArrayList or Array whateven you have passed the
Second Spinner.
2.) Fill the ArrayList or Array of new data & Update the Second Spinnner using
adapter.notifyDataSetChanged();
second_spinner.setSelection(0);
First Set an OnItemClickListner for your First Spinner. In the OnItemClickListner Method first parse your XML. After completing XML parsing, set parsed data to the adapter and set that adapter with your second spinner
Set an OnItemClickListener on your first spinner that would prepare and set the adapter to the second spinner.
Here is a more complete code example:
Spinner spinner1 = (Spinner) findViewById(R.id.spinner1);
Cursor c1 = (some code for getting a cursor from an data source, for example, a sqlite database)
SimpleCursorAdapter adapter1 = new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, c1, new String[]{"column_name"}, new int[]{android.R.id.text1});
spinner1.setAdapter(adapter1);
Spinner spinner2 = (Spinner) findViewById(R.id.spinner2);
Cursor c2 = (some code for getting a cursor from an data source, for example, a sqlite database)
SimpleCursorAdapter adapter2 = new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, c2, new String[]{"column_name"}, new int[]{android.R.id.text1});
spinner2.setAdapter(adapter2);
spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Cursor c_new = (create a new cursor);
adapter2.changeCursor(c_new);
adapter2.notifyDataSetChanged(); // this is important for notifying the UI
spinner2.setAdapter(adapter2);
}
});
What you do is set a listener to the first Spinner, there change the Cursor of the second Adapter to a new one, notify the UI and reset the Adapter of the second Spinner.
I have an activity which has a Spinner widget to display categories. Initially I was using an ArrayAdapter to populate the the spinner as in the following code
private static final String[] arrayCategories = {
"Business",
"Personal"
};
mCatSpinner = (Spinner) findViewById(R.id.thecategory);
ArrayAdapter<String> catAdapter = new ArrayAdapter<String>(this, R.layout.track_category_item, arrayCategories);
catAdapter.setDropDownViewResource(R.layout.track_category_dropdown_item);
mCatSpinner.setAdapter(catAdapter);
This works fine, and the spinner displays the first array item by default if no selection is made. It does show the selected item when an item is actually selected
But now I want to use a SimpleCursorAdapter to pull the list contents from a db. So I changed it to
SimpleCursorAdapter scaCategories = new SimpleCursorAdapter(this, R.layout.track_category_item,cCategories,new String[] {DBAdapter.KEY_CATEGORIES_NAME},new int[]{R.id.text1});
scaCategories.setDropDownViewResource(R.layout.track_category_dropdown_item);
mCatSpinner = (Spinner) findViewById(R.id.thecategory);
mCatSpinner.setAdapter(scaCategories);
This populates the dropdown, but it does not display the first item in the spinner. Even if selected, it does not show the selected item.
I tried to setSlection to the first item using
if(mCatSpinner.isSelected() != true) {
mCatSpinner.setSelection(0);
}
but it didn't work
What is wrong?
Ok, it would help if I specified the widget id in the layout xml. <:(