
19/03/16, 10:14:00
|
|
Betatester oficial
|
|
Fecha de registro: dic 2008
Localización: Valencia
Mensajes: 625
Modelo de smartphone: NEXUS 5 - ONEPLUS 3
Tu operador: Pepephone
|
|
|
Dos Arrays
Vereis, tengo este Json el cual tiene varios arrays que muestro en un solo layout, pero me sale todo como una lista. ¿Hay alguna manera de mostrar los dos arrays por separado?, es decir en dos listas o yo que se, algo para poner cuales son los locales y cuales los visitantes.
Código:
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.herprogramacion.restaurantericoparico.R;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
public class Goles_Adapter extends ArrayAdapter {
// Atributos
private RequestQueue requestQueue;
JsonObjectRequest jsArrayRequest;
private static final String URL_BASE = "http://cadetes.esy.es/2015/goles.php";
private static final String URL_JSON = "";
private static final String TAG = "";
private static final String ESCUDO = "http://ffcv.es/ncompeticiones/";
List<Goles_Local> items;
public Goles_Adapter(Context context) {
super(context,0);
// Crear nueva cola de peticiones
requestQueue= Volley.newRequestQueue(context);
// Nueva peticion JSONObject
jsArrayRequest = new JsonObjectRequest(
Request.Method.GET,
URL_BASE + URL_JSON,
null,
new Response.Listener<JSONObject>() {
@override
public void onResponse(JSONObject response) {
items = parseJson(response);
notifyDataSetChanged();
}
},
new Response.ErrorListener() {
@override
public void onErrorResponse(VolleyError error) {
Log.d(TAG, "Error Respuesta en JSON: " + error.getMessage());
}
}
);
// Anyadir peticion a la cola
requestQueue.add(jsArrayRequest);
}
@override
public int getCount() {
return items != null ? items.size() : 0;
}
@override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
// Referencia del view procesado
View listItemView;
//Comprobando si el View no existe
listItemView = null == convertView ? layoutInflater.inflate(
R.layout.goles_row,
parent,
false) : convertView;
// Obtener el item actual
Goles_Local item = items.get(position);
// Obtener Views
TextView textoNombre = (TextView) listItemView.findViewById(R.id.tv_Nombre);
TextView textoMinuto = (TextView) listItemView.findViewById(R.id.tv_Minuto);
// Actualizar los Views
textoNombre.setText(item.getNombre());
textoMinuto.setText(item.getMinuto());
// Anyadir peticion a la cola
return listItemView;
}
public List<Goles_Local> parseJson(JSONObject jsonObject){
// Variables locales
List<Goles_Local> goles_Local_local = new ArrayList<>();
JSONArray jsonArray= null;
try {
// Obtener el array del objeto
jsonArray = jsonObject.getJSONArray("golesLocal");
for(int i=0; i<jsonArray.length(); i++){
try {
JSONObject objeto= jsonArray.getJSONObject(i);
Goles_Local goles_Local_Local = new Goles_Local(
objeto.getString("nombre"),
objeto.getString("minuto"));
goles_Local_local.add(goles_Local_Local);
} catch (JSONException e) {
Log.e(TAG, "Error de parsing: " + e.getMessage());
}
}
} catch (JSONException e) {
e.printStackTrace();
}
JSONArray jsonArray1= null;
try {
// Obtener el array del objeto
jsonArray1 = jsonObject.getJSONArray("golesVisitante");
for(int i=0; i<jsonArray1.length(); i++){
try {
JSONObject objeto= jsonArray1.getJSONObject(i);
Goles_Local goles_Local_Local = new Goles_Local(
objeto.getString("nombre"),
objeto.getString("minuto"));
goles_Local_local.add(goles_Local_Local);
} catch (JSONException e) {
Log.e(TAG, "Error de parsing: " + e.getMessage());
}
}
} catch (JSONException e) {
e.printStackTrace();
}
return goles_Local_local;
}
}
Última edición por Merche300 Día 28/08/16 a las 08:12:29.
|