Sachin Gurnani
Sachin Gurnani

Reputation: 2444

How to add Default value in spinner in android?

How to add the default value in spinner like selectone? I want to add one value in spinner that is visible on spinne,r but when I click on spinner all the values of adapter get displayed in front of me but not selectone which is the default value.

    try {

            json = new JSONObject(Status);

            // nameArray = json.names();

            valArray = json.getJSONArray("Machines");

        } catch (JSONException e) {
            e.printStackTrace();
        }



        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,

                android.R.layout.simple_spinner_item);
                adapter.setDropDownViewResource(
                    android.R.layout.simple_spinner_dropdown_item);

        for (int i = 0; i < valArray.length(); i++) {

            try {

                String machineName = valArray.getJSONObject(i)

                        .getString("MachineName").toString();

                // Integer machineID = Integer.parseInt(valArray.getJSONObject(

                // 0).getString("MachineID"));

                // Entities.machineID = machineID;

                adapter.add(machineName);

            } catch (JSONException e) {

                e.printStackTrace();

            }

        }



        sp_Machine.setAdapter(adapter);

Upvotes: 1

Views: 5148

Answers (2)

Yuval Perelman
Yuval Perelman

Reputation: 4809

here is a solution i came up with a while back. just use this adapter instead of a regular one:

    package perelman.yuval.carsapp;
    import java.util.ArrayList;
    import java.util.List;
    import android.content.Context;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ArrayAdapter;
    import android.widget.TextView;
    public class SpinnerDualAdapter<T> extends ArrayAdapter<T> {
    private ArrayAdapter<String> myAdapter;
    private boolean isPromptAdapter;
    private SpinnerDualAdapter(Context context, int textViewResourceId,
            List<T> objects) {
        super(context, textViewResourceId, objects);
        // TODO Auto-generated constructor stub
    }
    
    public SpinnerDualAdapter(Context context,
            int textViewResourceIdForDropdownView,
            int textViewResourceIdForDefaultView, List<T> objects,
            String defaultValue) {
        this(context, textViewResourceIdForDropdownView, objects);
        ArrayList<String> myArrayList = new ArrayList<String>();
        myArrayList.add(defaultValue);
        myAdapter = new ArrayAdapter<String>(context,
                textViewResourceIdForDefaultView, myArrayList);
        isPromptAdapter=true;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
            if(isPromptAdapter){
                isPromptAdapter=false;
                return myAdapter.getView(position, convertView, parent);
            }
            TextView tv=new TextView(getContext());
            tv.setText(this.getItem(position).toString());
        return tv;
    }
    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent){
        isPromptAdapter=false;
        return(super.getDropDownView(position, convertView, parent));
    }
    }

Upvotes: 0

user1143305
user1143305

Reputation:

If you are getting the values of the spinner dynamically, then this is the way to add select at first of the spinner value:

try{
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://10.0.2.2/android/classname_spinner.php");
    try{
        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        String responseBody = httpclient.execute(httppost,responseHandler);
        JSONObject json = new JSONObject(responseBody);

        JSONArray jArray = json.getJSONArray("output");
        arr = new String[jArray.length()+1];
        arr[0] = "-select-";
        for(int i=0;i<jArray.length();i++){
            JSONObject json_data = jArray.getJSONObject(i);
            String sclass = json_data.getString("spinner");
            arr[i+1] = sclass;
        }
    }catch (Exception e) {
        Log.e("log_tag","Error parsing classname data"+e.toString());
    }
}catch (Exception e) {
    Log.e("log_tag","Request failed"+e.toString());
}

now add the value you got to the spinner as shown below:

classSpinner = (Spinner) findViewById(R.id.editClass);
    ArrayAdapter<String> classNameAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,arr);
    classNameAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    classSpinner.setAdapter(classNameAdapter);

Now when want the spinner to get back to its default value on on click event, then just write in your on click event as:

reset.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                classSpinner.setSelection(0);
            }
        });

Hope this helps you.

Upvotes: 3

Related Questions