Spinner with an initial text

1

Cordial greeting, by means of a query to a page:

http://localhost/baradm/ubica.php?id=002

I get the answer .json:

[{"DESCRP":"MESA 01"},{"DESCRP":"CUARTO 01"},{"DESCRP":"CUARTO 02"}]

This I have uploaded to a Spinner:

And I wish that the text LOCATION ... appears to me as an initial text and that this is not selectable, I have placed a help in this link , but I do not know how to implement it, I enclose the java classes with which I fill the spinner.

Thanks in advance.

PEDIDOFRAGMENT.JAVA

package com.windroid.dinas;

import...;


public class PedidoFragment extends Fragment{

    final static String urlAddress="http://10.0.3.2/baradm/ubica.php?id=";

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_pedido, container, false);
        final Spinner spinnerUbica = (Spinner) view.findViewById(R.id.spUbica);

        new Downloader(getActivity(),urlAddress+GlobalVariables.getUsr(),spinnerUbica).execute();

        return view;
    }

}

DOWNLOADER.JAVA

package com.windroid.dinas;

import...

public class Downloader extends AsyncTask<Void,Void,String> {

    Context c;
    String urlAddress;
    Spinner sp;

    ProgressDialog pd;


    public Downloader(Context c, String urlAddress, Spinner sp) {
        this.c = c;
        this.urlAddress = urlAddress;
        this.sp = sp;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

        pd=new ProgressDialog(c);
        pd.setTitle("Buscando");
        pd.setMessage("Buscando...Por favor, espere");
        pd.show();
    }

    @Override
    protected String doInBackground(Void... params) {
        return this.downloadData();
    }


    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);

        pd.dismiss();

        if(s==null)
        {
            Toast.makeText(c,"No se puede recuperar, valor nulo devuelto",Toast.LENGTH_SHORT).show();
        }else
        {
            Toast.makeText(c,"Exito",Toast.LENGTH_SHORT).show();

            //CALL PARSER CLASS TO PARSE
             DataParser parser=new DataParser(c,sp,s);
            parser.execute();
        }
    }

    private String downloadData()
    {
        HttpURLConnection con=Connector.connect(urlAddress);
        if(con==null)
        {
            return null;
        }

        InputStream is=null;
        try {

            is=new BufferedInputStream(con.getInputStream());
            BufferedReader br=new BufferedReader(new InputStreamReader(is));

            String line=null;
            StringBuffer response=new StringBuffer();

            if(br != null)
            {
                while ((line=br.readLine()) != null)
                {
                    response.append(line+"\n");
                }

                br.close();

            }else {
                return null;
            }

            return response.toString();

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(is != null)
            {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }
}

DATAPARSER.JAVA

package com.windroid.dinas;

import...


public class DataParser extends AsyncTask<Void,Void,Integer> {

    Context c;
    Spinner sp;
    String jsonData;

    ProgressDialog pd;
    ArrayList<String> spacecrafts=new ArrayList<>();

    public DataParser(Context c, Spinner sp, String jsonData) {
        this.c = c;
        this.sp = sp;
        this.jsonData = jsonData;
    }


    @Override
    protected void onPreExecute() {
        super.onPreExecute();


        pd=new ProgressDialog(c);
        pd.setTitle("Buscando");
        pd.setMessage("Buscando...Por favor, espere");
        pd.show();
    }

    @Override
    protected Integer doInBackground(Void... params) {
        return this.parseData();
    }

    @Override
    protected void onPostExecute(Integer result) {
        super.onPostExecute(result);

        pd.dismiss();

        if(result==0)
        {
            Toast.makeText(c,"No se puede analizar",Toast.LENGTH_SHORT).show();
        }else
        {
            //Toast.makeText(c,"Analizado con Exito",Toast.LENGTH_SHORT).show();

            //BIND
            ArrayAdapter adapter=new ArrayAdapter(c,android.R.layout.simple_spinner_dropdown_item,spacecrafts);

            adapter.add("UBICACION...");
            sp.setAdapter(adapter);


            sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

                    Toast.makeText(c,spacecrafts.get(position),Toast.LENGTH_SHORT).show();
                }

                @Override
                public void onNothingSelected(AdapterView<?> parent) {

                }
            });

        }
    }

    private int parseData()
    {
        try {
            JSONArray ja=new JSONArray(jsonData);
            JSONObject jo=null;

            spacecrafts.clear();
            Spacecraft s=null;

            for(int i=0;i<ja.length();i++)
            {
                jo=ja.getJSONObject(i);

                //int id=jo.getInt("id");
                String name=jo.getString("DESCRP");

                s=new Spacecraft();
                //s.setId(id);
                s.setName(name);

                spacecrafts.add(name);
            }

            return 1;

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

        return 0;

    }

}

ESPACECRAFT.JAVA

package com.windroid.dinas.mDataObject;

public class Spacecraft {

    String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
    
asked by user2683734 17.03.2017 в 10:35
source

1 answer

0

The data shown on the adapter is configured using ArrayList<String> spacecrafts , therefore "UBICACION..." is found as the last element:

ArrayAdapter adapter=new ArrayAdapter(c,android.R.layout.simple_spinner_dropdown_item,spacecrafts);

The options are:

select the element of the desired index, in this case 3 by means of the method setSelection () :

spinnerUbica.setSelection(3);
  

setSelection () : Jumps directly to a specific element of   the adapter data.

or ensure that it is inserted as the first element since if you check in onPostExectute() of your AsyncTask DataParser, it is being added later to your Spinner , for this reason it is shown at the end of the list.

...
...
     //BIND
     ArrayAdapter adapter=new ArrayAdapter(c,android.R.layout.simple_spinner_dropdown_item,spacecrafts);

     adapter.add("UBICACION...");
     sp.setAdapter(adapter);
...
...

You can do this by adding the desired element in the ArrayList before building the spinner:

...
...

     spacecrafts.add(0, "UBICACION..."); //* Agrega como primer elemento del List.
     //BIND
     ArrayAdapter adapter=new ArrayAdapter(c,android.R.layout.simple_spinner_dropdown_item,spacecrafts);

     //adapter.add("UBICACION...");
     sp.setAdapter(adapter);
...
...

This second option I think would be the right one to show the data correctly in spinner :

UBICACION...
MESA 01
CUARTO 01
CUARTO 02
    
answered by 17.03.2017 в 16:24