Ayuda Spinner dependiente de otro con .json

28 views
Skip to first unread message

Alejandro Ayala

unread,
May 6, 2016, 6:29:56 PM5/6/16
to desarrolladores-android
Buenas tardes, lo que estoy intentando hacer  es, tener un spinner de Estado y otro de Ciudades, al seleccionar un Estado necesito que me muestre las Ciudades de ese Estado en el otro spinner. Los spinner los estoy llenando con un archivo .json. El spinner Estado funciona, el spinner Ciudades es donde tengo el problema, debido a que cuando me despliega los items me muestra correctamente las Ciudades pero no puedo seleccionar una opción de Ciudad. Se los agredeceria quien me pueda ayudar, gracias saludos.

MainActivity.java
public class MainActivity extends AppCompatActivity {

private List<Estado> re_edo;
private ParseJson parse_edo;
String id_edo;

private List<Municipio> re_mpio;
private ParseJson parse_mpio;
final ArrayList<String> municipio = new ArrayList<>();


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


//inicio spinner Estado

InputStream is_edo = getResources().openRawResource(R.raw.estado);

try {
parse_edo = new ParseJson();
re_edo = parse_edo.readJsonStream(is_edo);
System.out.println("Lectura Json terminada");
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}



final ArrayList<String> estado = new ArrayList<>();
// Recorrer objeto List<Receta> y concatenarlo en una variable para
// mostrarlo.
for (Estado r : re_edo) {
estado.add(r.getNombre());


}

//
ArrayAdapter<String> adapter_edo = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, estado);
adapter_edo.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item);

final Spinner spiner_edo = (Spinner) findViewById(R.id.spinner_estado);
spiner_edo.setAdapter(adapter_edo);
spiner_edo.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

String nombre=spiner_edo.getSelectedItem().toString();
for (Estado r : re_edo) {
municipio.clear();
if(r.getNombre().equals(nombre)){
System.out.println("sssi");
id_edo=r.getId();
System.out.println(r.getId());

}

for (Municipio r_mpio : re_mpio) {
if(r_mpio.getId_c_estado().equals(id_edo)){
municipio.add(r_mpio.getNombre());
}

}

}


}

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

}
});

//fin spinner Estado

//inicio spinner Municipio

InputStream is_mpio = getResources().openRawResource(R.raw.municipio);

try {
parse_mpio = new ParseJson();
re_mpio = parse_mpio.readJsonStream_mpio(is_mpio);
System.out.println("Lectura Json terminada");
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
ArrayAdapter<String> adapter_mpio = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, municipio);
adapter_mpio.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item);

final Spinner spiner_mpio = (Spinner) findViewById(R.id.spinner_mpio);
spiner_mpio.setAdapter(adapter_mpio);
spiner_mpio.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

String nombre=spiner_mpio.getSelectedItem().toString();
for (Municipio r : re_mpio) {

if(r.getNombre().equals(nombre)){
System.out.println("sssi");
id_edo=r.getId();
System.out.println(r.getId());

}

}
}

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

}
});

//fin spinner Municipio

}
}


ParseJson.java
 //Inicio Estado

public List<Estado> readJsonStream(InputStream in) throws IOException {
JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8"));
try {
return readEstadoArray(reader);
} finally {
reader.close();
}
}

public List<Estado> readEstadoArray(JsonReader reader) throws IOException {
List<Estado> estados = new ArrayList<Estado>();

reader.beginArray();

while (reader.hasNext()) {
estados.add(readEstado(reader));
}
reader.endArray();

return estados;
}

public Estado readEstado(JsonReader reader) throws IOException {
String id=null;
String nombre = null;


reader.beginObject();
while (reader.hasNext()) {
String name = reader.nextName();
if (name.equals("nombre")) {
nombre = reader.nextString();
} else if (name.equals("id")){
id=reader.nextString();
}else{
reader.skipValue();
}
}
reader.endObject();
return new Estado(id, nombre);
}

//Fin Estado

//Inicio Municipio

public List<Municipio> readJsonStream_mpio(InputStream in) throws IOException {
JsonReader reader_mpio = new JsonReader(new InputStreamReader(in, "UTF-8"));
try {
return readMunicipioArray(reader_mpio);
} finally {
reader_mpio.close();
}
}

public List<Municipio> readMunicipioArray(JsonReader reader_mpio) throws IOException {
List<Municipio> municipios = new ArrayList<Municipio>();

reader_mpio.beginArray();

while (reader_mpio.hasNext()) {
municipios.add(readMunicipio(reader_mpio));
}
reader_mpio.endArray();

return municipios;
}

public Municipio readMunicipio(JsonReader reader_mpio) throws IOException {
String id=null;
String nombre = null;
String id_c_estado=null;


reader_mpio.beginObject();
while (reader_mpio.hasNext()) {
String name = reader_mpio.nextName();
if (name.equals("nombre")) {
nombre = reader_mpio.nextString();
} else if (name.equals("id_c_estado")) {
id_c_estado = reader_mpio.nextString();
} else if (name.equals("id")){
id=reader_mpio.nextString();
}else{
reader_mpio.skipValue();
}
}
reader_mpio.endObject();
return new Municipio(id, id_c_estado, nombre);
}

//Inicio Municipio
}

Javier ExpoCode

unread,
May 23, 2016, 9:15:40 AM5/23/16
to desarrolladores-android
Hola Alejandro,

¿Podrías informar el JSON en el que tienes los estados y la relación Estado-Ciudades?

Happy Coding!
Reply all
Reply to author
Forward
0 new messages