Android spinner prompt text not showing [duplicate] - android

This question already has answers here:
How to make an Android Spinner with initial text "Select One"?
(36 answers)
Closed 9 years ago.
The first year from the data array is shown instead of the text from prompt in my spinner. I tried adding the prompt in XML, but I also tried from code. Furthermore, it gives me a "resource not found error", when adding the spinnerSelector attribute.
XML
<Spinner
android:id="#+id/spinnerYear"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_marginLeft="10dip"
android:layout_marginRight="10dip"
android:drawSelectorOnTop="true"
android:padding="5dip"
android:prompt="#string/spinner_header"
android:background="#drawable/selector_yearspinnerback"
android:layout_below="#+id/linearLayout_gender_btns"
android:layout_centerHorizontal="true"></Spinner>
-- android:spinnerSelector="#drawable/category_arrow"
Code
ArrayList<String> yearList = new ArrayList<String>();
int now = new Date().getYear() + 1900;
for (int i = now; i > now - 110; i--) {
yearList.add(i + "");
}
Spinner spinner = (Spinner) findViewById(R.id.spinnerYear);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, yearList);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);

Perhaps you are seeing the spinner drop down items as list without any prompt text. There are two modes in which spinner shows the items, dropdown and dialog.
Add this attribute to your spinner as an XML atrtribute:
android:spinnerMode="dialog"
And you will now get items in a popup dialog select list instead of drop down list.

You have to set adapter.setDropDownViewResource (android.R.layout.simple_spinner_dropdown_item); after
spinner.setAdapter(adapter);
So the fixed code would be:
ArrayList<String> yearList = new ArrayList<String>();
int now = new Date().getYear() + 1900;
for (int i = now; i > now - 110; i--) {
yearList.add(i + "");
}
Spinner spinner = (Spinner) findViewById(R.id.spinnerYear);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, yearList);
spinner.setAdapter(adapter);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
(I hope it works for you like it works for me :D!)

For me, both android:prompt XML attibute as well as Spinner.setPrompt work, and list selector displays correct title.
Try to find bug in your code, or make call to Spinner.getPrompt at some point and print this to log, to find our from where you get invalid title.

Related

How to let user choose a specific year from a spinner

Hello I want the user to be able to click a spinner and choose a year from those I provide. I would provide a range from 1970 to current year, but I don't know how to implement it.
If I create a spinner and give it an array like this
<string-array name="spinnerItems">
<item>1970</item>
<item>1971</item>
</string-array>
Then I would need to add more than 40 items by hand, and I would need to update the app manually every year. Also the spinner onClick callback returns not the string value but the index of the string in the array, so for example 1972 would be index 3.
Also I will provide another spinner where the user will choose a year that's greater or equal of the year choosen in the first spinner.
In the end I want tos end an api request with an interval of years.
How can I do this?
I think you have to create year picker instead of creating number list of spinner
Try this
ArrayList<String> years = new ArrayList<String>();
int thisYear = Calendar.getInstance().get(Calendar.YEAR);
for (int i = 1900; i <= thisYear; i++) {
years.add(Integer.toString(i));
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, years);
Spinner spinYear = (Spinner)findViewById(R.id.yearspin);
spinYear.setAdapter(adapter);
Hope this will help..
You can add items to a spinner programmatically.
You can find out with a simple command which year we have right now and can froce the program to add numbers from e.g. 1800 to the current year to your spinner.
WARNING: Pseudo code
Spinner mySpinner = (Spinner) findViewById(R.id.spinner);
Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
ArrayAdapter<String> adapter;
List<String> list;
list = new ArrayList<String>();
for(int i = 1800; i <= year; i++)
{
list.add(i);
}
adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item, list);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mySpinner.setAdapter(adapter);

How to set string from sqlite to Spinner in Android?

I have Spinner in my app and set String[] programmatic.
speciality = (Spinner) findViewById(R.id.general_specality_s);
specialityadapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, speciality_list);
speciality.setAdapter(specialityadapter);
and get selected string like this speciality.getSelectedItem().toString(); and store in sqlite. Now I want to get that value from sqlite and need to set in Spinner1.
String SpinnerText=c.getString(c.getColumnIndex("speciality"));
How to set this string to speciality / Spinner ? Thanks in advance.
Fetch your data into an array and use an array adapter
String[] spinnerArray = your data goes here;
Spinner spinner = (Spinner)findViewById(R.id.yourspinnerid);
ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, spinnerArray);
spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(spinnerAdapter);

TextView is ListView is not shown correctly

I have an app that take strings from database and put it in ListView.
this is the code for getting my String from database:
public void setLogView(String date){
ArrayAdapter<TextView> adapter = new ArrayAdapter<TextView>(this, android.R.layout.simple_list_item_1);
listView.setAdapter(adapter);
DataBaseMain dataBase = new DataBaseMain(this);
dataBase.open();
String[][] all = dataBase.dayLog(date);
dataBase.close();
if(all == null)
return;
String temporay = "";
for(int j = 0; j < all[0].length; j++){
temporay = "";
for (int i = 0; i < all.length; i++){
TextView text = new TextView(this);
temporay = temporay + " " + all[i][j];
text.setText(temporay);
adapter.add((TextView)text);
}
}
}
Its seems that i get new TextView in my ListView but the text is messed up.
I checked my temporay string and is fine.
Is somewhere in putting him in the ListView.
No error in logcat or exceptions.
here is what i got in my app ListView insted of my wanted text.(i wanted to put picutrue but i dont have enough repetion:
There, it becomes clear from the image you provided
Try, this..
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
listView.setAdapter(adapter);
Your adapter to appending textView object instead of String you are providing.
then add temporay instead of textView inside you loop..like
adapter.add(temporay);
This, will certainly solve your issue.
Change your adapter to ArrayAdapter<String>, and add temporay instead of the whole listview.
Or else, you can extend the ArrayAdapter in and override the getView()
Assuming that, you are trying to display the text in custom listview using separate layout.xml which contains only textview in it.
Check my example given below, this is how i did to achieve this:
First of all fetch the data you are want to display and store it in an ArrayList. Here, al_rec_id, al_rec_name are arraylists of the type Integer and String, respectively.
cursor = database.query("records_data", new String[]{"rec_id", "rec_name"}, "cat_id=?", new String[]{cat_id+""}, null, null, null);
if(cursor.getCount() == 0)
Toast.makeText(getBaseContext(), "No records found.", Toast.LENGTH_SHORT).show();
else
{
while(cursor.moveToNext())
{
al_rec_id.add(cursor.getInt(0));
al_rec_name.add(cursor.getString(1));
}
cursor.close();
}
After that, bind this arraylist with ArrayAdapter and then set this adapter to listview as below. Here, array_adapter_all_records is an ArrayAdapter of the type String
array_adapter_all_records = new ArrayAdapter<String>(this, R.layout.single_row_home, R.id.textViewSingleRowHome, al_rec_name);
listview_all_records.setAdapter(array_adapter_all_records);
This is my single_row_home.xml code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="15dp"
android:background="#ffffff" >
<TextView
android:id="#+id/textViewSingleRowHome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
style="#style/listview_only_textview"
android:text="TextView" />
</RelativeLayout>
Thats it. And you're done...!!!
Create a class custom adapter which will extend your ArrayList Adapter in that You can either inflate a different xml which contains your textview or you can create a dynamic textview as you are doing it now in your getView method in custom Adapter. If You need an example let me know.

Clear spinner contents on button click

I have a spinner that is dynamically loaded with data as following
final String[] sku = CrownApplication.mDb.getAllSKUs(Qsearch);
if((sku.length>=1)){
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(CrownTakeOrder.this,android.R.layout.simple_spinner_item, sku);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mSpner.setAdapter(dataAdapter);
}
This works fine,now I have a button that on clicking gets the value and sets other fields blank e.g Edittext as below. The only problen is I am not able to clear the spinner so once everything else is cleared the spinner still remains with the old Values
if (!mError) {
mSKU = mSpner.getSelectedItem().toString();
Qsearch =mQuery.getText().toString();
quantity =mQuantity.getText().toString();
String[] parts = mSKU.split(" - ");
str1 = parts[0];
str2 = parts[1];
addBody(Qsearch,mSKU,quantity);
mQuery.setText("");
mTxtview.setText("");
mQuantity.setText("");
mSKU = "empty";
//mSpner.setAdapter(null);
}
I have tried to use
mSpner.setAdapter(null);
But my app crashes....How to empty spinner? I am coding on
android:minSdkVersion="11"
android:targetSdkVersion="15"
try this
mSpner.setAdapter(new ArrayAdapter<String>(CrownTakeOrder.this, android.R.layout.simple_spinner_item, new String[]));

Dynamic spinner options does not open

I have within a TabActivity a Spinner that will be generated dynamically. Just to test, I did so manually:
Spinner sp_departure = (Spinner) findViewById(R.id.spinner_departure);
// This array will be generated through a database
String[] array_spinner = new String[2];
array_spinner[0] = "Departure 1";
array_spinner[1] = "Departure 2";
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, array_spinner);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp_departure.setAdapter(adapter);
When I run the app looks ok since option selected is "Departure 1" but when I click to open the options I get some errors and the application is closed.
Any idea what could be wrong?
Thanks in advance.
- Update
This is what was generated by LogCat: http://pastebin.com/1QPKZdKB
Yes you might have set setContetView(R.layout.yourxml)...,
Change it to :
View viewToLoad = LayoutInflater.from(this.getParent()).inflate(R.layout.yourxm, null);
this.setContentView(viewToLoad);
and use
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getParent(), android.R.layout.simple_spinner_item, array_spinner);
Try, it may helps you

Categories

Resources