Spinner Default text while the values in spinner are from database

I need a default text for a spinner where values in the spinner are from database,fetching all details from database into spinner by using arrayadapter,but im not able to apply default text like Select Size for it. Isaw many posts ,but i didnt find one which has same requirement. Help me please :( Here is the code

String siz =feedObj.getString("P_Size");
Size=(Spinner)findViewById(R.id.size);
if(siz!= null && !siz.isEmpty())
{
String[] seperatedsize = siz.split(", ");     
ArrayAdapter spinnersize = new ArrayAdapter(this,android.R.layout.simple_spinner_item,seperatedsize);
spinnersize.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Size.setAdapter(spinnersize);
//  spinnersize.add("select");
//  spinnersize.setPrompt("Select your Size!");
}
else
{   
layoutsize.setVisibility(View.GONE);
Size.setVisibility(View.GONE);
}

here is the image

image

Answers


Consider this handy's posts posted on Stack Overflow you got what you want.

  1. Show default value in Spinner in android

  2. How to make an Android Spinner with initial text "Select One"


if(siz!= null && !siz.isEmpty())
{
String[] seperatedsize = siz.split(", ");
**StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("Select your Size!").append(",");
for (int i = 0; i < arr.length; i++){
    stringBuilder.append(arr[i]).append(",");
}
String strTemp=sb.toString();
seperatedsize=strTemp.split(",");**     
ArrayAdapter spinnersize = new ArrayAdapter(this,android.R.layout.simple_spinner_item,seperatedsize);
spinnersize.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Size.setAdapter(spinnersize);
//  spinnersize.add("select");
//  spinnersize.setPrompt("Select your Size!");
}

You can try this out.

or

you can use ArrayList<String> to hold your data.After storing data in array list just call add(0,"Select your Size!") with ArrayList<String> object.

Demo code :

ArrayList<String> arrayList = new ArrayList<String>();
arrayList.add("b");
arrayList.add("c");
arrayList.add("d");
arrayList.add("e");
arrayList.add("f");
arrayList.add(0, "a");

Result : a,b,c,d,e,f


Need Your Help

Avoid deadlocks in a multithreaded process

multithreading deadlock idioms

What are the best practices/idioms should someone follow in order to avoid deadlocks?

Does TideSDK's Ti.UI.addTray work in Ubuntu?

ubuntu sdk tidesdk

I was testing out a TideSDK example using the following code from github.