Cordial saludo, mediante una consulta a una página:
http://localhost/baradm/ubica.php?id=002
obtengo la respuesta .json:
[{"DESCRP":"MESA 01"},{"DESCRP":"CUARTO 01"},{"DESCRP":"CUARTO 02"}]
Esto lo he cargado a un Spinner:
Y deseo que el texto UBICACION... me aparezca como texto incial y que este no sea seleccionable, he ubicado una ayuda en este link, pero no se como implementarlo, adjunto las clases java con las que lleno el spinner.
Gracias de antemano.
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;
}
}