Getting an attribute from an object inside an arraymap

65
February 22, 2019, at 10:00 AM

I'm creating a class called "partite", and this is the code

public class Partita {
    String HT;      //HomeTeam
    String AT;      //AwayTeam
    String dataP;   //dataPartita
    int HG;         //HomeGoal
    int AG;         //AwayGoal
    String FTR;     //Full time result
    public Partita(){
        this.HT = "";
        this.AT = "";
        this.dataP = "";
        this.HG = 0;
        this.AG = 0;
        this.FTR = "";
    }
    public Partita(String HT, String AT, String dataP, int HG, int AG, String FTR) {
        this.HT = HT;
        this.AT = AT;
        this.dataP = dataP;
        this.HG = HG;
        this.AG = AG;
        this.FTR = FTR;
    }
}

In the main activity I'm creating an ArrayList, putting a list of some "Partita" object, with attributes come from a json file, and then I create an ArrayMap and put the ArrayList inside, like this

for (int i = 0; i < arr.length(); i++) {
          JSONObject jsonPart = arr.getJSONObject(i);
          partite.add(new Partita(jsonPart.getString("HomeTeam"), 
          jsonPart.getString("AwayTeam"),jsonPart.getString("Date"), 
          jsonPart.getInt("FTHG"), jsonPart.getInt("FTAG"), 
          jsonPart.getString("FTR")));
          partitemap.put(i, partite.get(i));
          Log.i("partita", partite.get(i).HT + " " + partite.get(i).HG + ":" + partite.get(i).AG + " " + partite.get(i).AT);
                }

How can I use the ArrayMap instead of ArrayList to get the attributes of an object and use it?

Answer 1

Use this instead:

          for (int i = 0; i < arr.length(); i++) {
              JSONObject jsonPart = arr.getJSONObject(i);
              partitemap.put(i, new Partita(jsonPart.getString("HomeTeam"), 
                                            jsonPart.getString("AwayTeam"),
                                            jsonPart.getString("Date"), 
                                            jsonPart.getInt("FTHG"), 
                                            jsonPart.getInt("FTAG"), 
                                            jsonPart.getString("FTR")
                                           )
              );
          }
READ ALSO
how to save the value of a SeekBar and pass it to other activities?

how to save the value of a SeekBar and pass it to other activities?

I'm new in kotlin language and am trying to implement a SeekBar for resizing TextViews inside my setting activity and save its value by shared preference and then pass the values of the SeekBar to the main activity!

50
Looking for solution for updating a device owner app without factory reset

Looking for solution for updating a device owner app without factory reset

I have a kiosk mode Android non-Playstore tablet app (that we have been installing on the devices by using adb shell install)Kiosk mode is achieved through a combination of several DevicePolicyManager methods, setting user restrictions, and finally by setting...

77
Android: Navigation Back using same activity with toolbar

Android: Navigation Back using same activity with toolbar

I have an activity with differents layoutsEvery time I want to go from one layout to another I use the Visibility property

76
What is the best way to use a TabLayout with fragments sharing context

What is the best way to use a TabLayout with fragments sharing context

My goal is to have an Fragment that has context used to get a string from Stringsxml

86