Two arrays in the same layout

1

You see, I have this .Json which has several arrays what I would like is to see in two lists, goals and local minute in one and goals and minute visitor in another, but something fails me. Can you help me. Thanks

    public class Goles_Adapter extends ArrayAdapter {

    // Atributos
    private RequestQueue requestQueue;
    JsonObjectRequest jsArrayRequest;
    private static final String URL_BASE = "http://www.ffcv.es/ncompeticiones/server.php?action=getActa&tmp=2015/2016&jor=9&cmp=284&idl=0201045301&idv=0201239301";
    private static final String URL_JSON = "";
    private static final String TAG = "";
    private static final String ESCUDO = "http://ffcv.es/ncompeticiones/";
    List<Goles> items;
    List<Goles_V> items_v;

    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 item = items.get(position);

        // Obtener Views
        TextView textoNombre = (TextView) listItemView.findViewById(R.id.tv_jornada);
        TextView textoMinuto = (TextView) listItemView.findViewById(R.id.tv_Fecha);


        // Actualizar los Views
        textoNombre.setText(item.getNombre());
        textoMinuto.setText(item.getMinuto());

        View listItemView1;

        listItemView1 = null == convertView ? layoutInflater.inflate(
                R.layout.goles_row,
                parent,
                false) : convertView;

        Goles_V item1 = items_v.get(position);

        // Obtener Views
        TextView textoNombre1 = (TextView) listItemView1.findViewById(R.id.tv_jornada1);
        TextView textoMinuto1 = (TextView) listItemView1.findViewById(R.id.tv_Fecha1);


        // Actualizar los Views
        textoNombre1.setText(item1.getNombre());
        textoMinuto1.setText(item1.getMinuto());

        // Anyadir peticion a la cola

        return listItemView;
    }

    public List<Goles> parseJson(JSONObject jsonObject){
        // Variables locales
        List<Goles> rankingAmonestacionesCadetes = 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 RankingAmonestacionesCadetes = new Goles(
                            objeto.getString("nombre"),
                            objeto.getString("minuto"));


                    rankingAmonestacionesCadetes.add(RankingAmonestacionesCadetes);

                } catch (JSONException e) {
                    Log.e(TAG, "Error de parsing: " + e.getMessage());
                }
            }

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

        public List<Goles_V> parseJson(JSONObject jsonObject){
            // Variables locales
            List<Goles_V> goles_Visitante = new ArrayList<>();
            JSONArray jsonArray_visi= null;

            try {
                // Obtener el array del objeto
                jsonArray_visi = jsonObject.getJSONArray("golesVisitante");

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

                    try {
                        JSONObject objeto= jsonArray_visi.getJSONObject(i);

                        Goles_V goles_visitante = new Goles_V(
                                objeto.getString("nombre"),
                                objeto.getString("minuto"));


                        goles_Visitante.add(goles_visitante);

                    } catch (JSONException e) {
                        Log.e(TAG, "Error de parsing: " + e.getMessage());
                    }
                }

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


            return rankingAmonestacionesCadetes;
        }
    }
}

But on the line

public List<Goles_V> parseJson(JSONObject jsonObject){

There is something wrong.

Error155, 9) error: illegal start of expression
Error155, 39) error: ';' expected
Error155, 61) error: ';' expected
Error:Execution failed for task ':app:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.

Well and halfway got this code works properly when there is the same number of items in each array, I mean a draw, but when there are more local items than visitors gives this error:

java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1

this is the code:

public class Goles_Adapter_J25 extends ArrayAdapter {

// Atributos
private RequestQueue requestQueue;
JsonObjectRequest jsArrayRequest;
private static final String URL_BASE = "http://juveniles.esy.es/2015/jornadas/info_jornadas/info_j_25.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_local;
List<Goles_Visi> items_visi;

public Goles_Adapter_J25(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>() {

                public void onResponse(JSONObject response) {
                    items_local = parseJson_local(response);
                    items_visi = parseJson_visitante(response);
                    notifyDataSetChanged();
                }
            },
            new Response.ErrorListener() {

                public void onErrorResponse(VolleyError error) {
                    Log.d(TAG, "Error Respuesta en JSON: " + error.getMessage());

                }
            }
    );

    // Anyadir peticion a la cola
    requestQueue.add(jsArrayRequest);
}

public int getCount() {
    return items_local != null ? items_local.size() : 0;
}


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_dos_listas_row,
            parent,
            false) : convertView;


    // Obtener el item actual
    Goles_Local item = items_local.get(position);

    // Obtener Views
    TextView textoNombre = (TextView) listItemView.findViewById(R.id.tv_Nombre_Local);
    TextView textoMinuto = (TextView) listItemView.findViewById(R.id.tv_Minuto_Local);


    // Actualizar los Views
    textoNombre.setText(item.getNombre());
    textoMinuto.setText(item.getMinuto());

    // Obtener el item actual
    Goles_Visi item_visi = items_visi.get(position);

    // Obtener Views
    TextView textoNombre1 = (TextView) listItemView.findViewById(R.id.tv_Nombre_Visi);
    TextView textoMinuto1 = (TextView) listItemView.findViewById(R.id.tv_Minuto_Visi);


    // Actualizar los Views
    textoNombre1.setText(item_visi.getNombre_visi());
    textoMinuto1.setText(item_visi.getMinuto_visi());





    // Anyadir peticion a la cola

    return listItemView;
}



public List<Goles_Local> parseJson_local(JSONObject jsonObject) {
    // Variables locales
    List<Goles_Local> goles_Local_local = new ArrayList<>();
    JSONArray jsonArray_Local = null;

    try {
        // Obtener el array del objeto
        jsonArray_Local = jsonObject.getJSONArray("golesLocal");

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

            try {
                JSONObject objeto = jsonArray_Local.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;
}

public List<Goles_Visi> parseJson_visitante (JSONObject jsonObject){
            // Variables locales
            List<Goles_Visi> goles_Visitante = new ArrayList<>();
            JSONArray jsonArray_visi = null;

            try {
                // Obtener el array del objeto
                jsonArray_visi = jsonObject.getJSONArray("golesVisitante");

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

                    try {
                        JSONObject objeto = jsonArray_visi.getJSONObject(i);

                        Goles_Visi goles_visitante = new Goles_Visi(
                                objeto.getString("minuto"),
                                objeto.getString("nombre"));


                        goles_Visitante.add(goles_visitante);

                    } catch (JSONException e) {
                        Log.e(TAG, "Error de parsing: " + e.getMessage());
                    }
                }

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


            return goles_Visitante;
        }

}

    
asked by Rafel C.F 20.03.2016 в 18:31
source

2 answers

1

I think you have the closures badly because you have a method public List<Goles> without closing where you add another public List<Goles_V> without closing the previous one I hope you understand.

      public List<Goles> parseJson(JSONObject jsonObject) {
            ..//

            try {
            ..//

            } catch (JSONException e) {
                e.printStackTrace();
            }
        } <---- agregar este

        public List<Goles_V> parseJson (JSONObject jsonObject){
            ..//

            try {
            ..//

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

            return rankingAmonestacionesCadetes;
        }

    }

} <---- borrar uno del final 

In addition to the method public List<Goles> parseJson(JSONObject jsonObject) you have as return List<Goles> and this is not present in the code I imagine you want to return List<Goles> rankingAmonestacionesCadetes = new ArrayList<>(); .

With what would be something like this for example:

        public List<Goles> parseJson(JSONObject jsonObject) {
            ..//

            List<Goles> rankingAmonestacionesCadetes = new ArrayList<>();

            try {
            ..//

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

         return rankingAmonestacionesCadetes; <--- agregar return

        } <---- agregar este

        public List<Goles_V> parseJson (JSONObject jsonObject){
            ..//

            try {
            ..//

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

            return rankingAmonestacionesCadetes;
        }
    }
} <---- borrar uno del final 

Update:

I get the feeling that you got confused by looking at the example I put in and mixing the try or something like that I hope it will help you better now.

   public List<Goles> parseJson(JSONObject jsonObject){
        // Variables locales
        List<Goles> rankingAmonestacionesCadetes = 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 RankingAmonestacionesCadetes = new Goles(
                            objeto.getString("nombre"),
                            objeto.getString("minuto"));


                    rankingAmonestacionesCadetes.add(RankingAmonestacionesCadetes);

                } catch (JSONException e) {
                    Log.e(TAG, "Error de parsing: " + e.getMessage());
                }
            }

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

         return rankingAmonestacionesCadetes;
    }

    public List<Goles_V> parseJson(JSONObject jsonObject){
            // Variables locales
            List<Goles_V> goles_Visitante = new ArrayList<>();
            JSONArray jsonArray_visi= null;

            try {
                // Obtener el array del objeto
                jsonArray_visi = jsonObject.getJSONArray("golesVisitante");

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

                    try {
                        JSONObject objeto= jsonArray_visi.getJSONObject(i);

                        Goles_V goles_visitante = new Goles_V(
                                objeto.getString("nombre"),
                                objeto.getString("minuto"));


                        goles_Visitante.add(goles_visitante);

                    } catch (JSONException e) {
                        Log.e(TAG, "Error de parsing: " + e.getMessage());
                    }
                }

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


            return rankingAmonestacionesCadetes;
     }
 }

This I leave it as a detail does not suppose any error but it is outside the convention.

Goles RankingAmonestacionesCadetes = new Goles(
                        objeto.getString("nombre"),
                        objeto.getString("minuto"));


                 rankingAmonestacionesCadetes.add(RankingAmonestacionesCadetes);

There is no error in the use of RankingAmonestacionesCadetes which is the identifier / name that you assign to the object, since you are using PascalCase or UpperCamelCase, for the name and this is usually used for the name of the classes but not It is mandatory if it is highly advisable to follow the conventions or at least the most common ones can look at something in this link:

link

I mention it to you just in case because I see that in almost all of your code you use Lower Camel Case (CamelCase) or some variation to name your variables like for example here:

 List<Goles> rankingAmonestacionesCadetes = new ArrayList<>();

And maybe your IDE is autocomplete with RankingAmonestacionesCadetes because they have a class with that name or similar with that name or something written in some other place.

You can try something like this:

Goles rankingAmonestacionesCadetes = new Goles(
                        objeto.getString("nombre"),
                        objeto.getString("minuto"));

OR Change the name Goles rankingAmonestacionesCadetesTemp to not mix it with rankingAmonestacionesCadetes = new ArrayList<>();

Goles rankingAmonestacionesCadetesTemp = new Goles(
                        objeto.getString("nombre"),
                        objeto.getString("minuto"));

               rankingAmonestacionesCadetes.add(
                            rankingAmonestacionesCadetesTemp);
  • RankingAmonestacionesCadetes - > PascalCase / UpperCamelCase
  • rankingAmonestacionesCadetes - > LowerCamelCase / (CamelCase)
answered by 21.03.2016 / 06:37
source
0

Review the following lines:

//Comprobando si el View no existe
listItemView = null == convertView ? layoutInflater.inflate(
        R.layout.goles_row,
        parent,
        false) : convertView;

The error is that in an abbreviated if you can not use any sentence that needs termination. Notice that the inflater ends in parentheses and this is always missing the semicolon. Try changing it to this:

//Comprobando si el View no existe
if(listItemView == null){
    layoutInflater.inflate(R.layout.goles_row,parent,false);
}else{
    convertView;
}

I have seen that you have repeated these sentences in at least two cases.

I await your response!

    
answered by 20.03.2016 в 20:49