简体   繁体   中英

How to pass arraylist generated from csv to Activity

I've created two java files that read a .csv of results for a monthly fishing competition, but I'm stuck on getting it to display in an activity. I was thinking that I'd need to use an intent to pass the arraylist from the reader to another activity, but I'm not sure how to do this and what I've tried so far throws up unexpected end of decalration, no method or unknown variable or field errors.

the idea is to eventually get it into a table, but I'd just like to display something just now

Already tried

System.out.println

and variations on

    Intent intent = getIntent();  
    intent.putStringArrayListExtra("OverallBoatCamp", OverallBoatChamp) test);

read about parcelable or serialiseable but one article mentioned arraylist is already serialiseable

tutorials I've come accross either are too basic just dealing with simple arrays, or don't say how to get this to display.

Code has no errors in AIDE and logcat shows csv is being read ok, with only a ARDT failed lock error. App runs and compiles fine but I just can't seem to get anything to display.

Code is:

BoatLeaderboard.java

      import android.app.*;
      import android.os.*;
      import java.util.*;
      import java.io.*;
      import java.nio.charset.*;
      import org.apache.http.impl.conn.*;
      import android.util.*;
      import android.content.*;

      public class BoatLeaderboards extends Activity
     {
 @Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.boatleaderboards);
    getActionBar().setDisplayHomeAsUpEnabled(true);

    readMatchData();

}
private List<OverallBoatChamp> boatchamplist = new ArrayList<>();
private void readMatchData(){

    InputStream is = getResources().openRawResource(R.raw.boatl);
    BufferedReader Reader = new BufferedReader(
    new InputStreamReader (is, Charset.forName("UTF-8"))
    );

    String line = "";
    try {

        //Step over headers
        Reader.readLine();

    while ( (line = Reader.readLine()) != null) {
        Log.d("BoatLeaderboards", "Line: " + line);
        //Split by commas
        String[] tokens = line.split(",");
        //read the data
        OverallBoatChamp champlist = new OverallBoatChamp();
        champlist.setAngler(tokens[0]);
        if (tokens.length >= 2 && tokens[1].length() > 0){
        champlist.setOct(Integer.parseInt(tokens[1]));
        } else {
            champlist.setOct(0);
        }
        if (tokens.length >= 3 && tokens[2].length() > 0){
            champlist.setNov(Integer.parseInt(tokens[2]));
        } else {
            champlist.setNov(0);
        }
        if (tokens.length >= 4 && tokens[3].length() > 0){
            champlist.setDec(Integer.parseInt(tokens[3]));
        } else {
            champlist.setDec(0);
        }
        if (tokens.length >= 5 && tokens[4].length() > 0){
            champlist.setJan(Integer.parseInt(tokens[4]));
        } else {
            champlist.setJan(0);
        }
        if (tokens.length >= 6 && tokens[5].length() > 0){
            champlist.setFeb(Integer.parseInt(tokens[5]));
        } else {
            champlist.setFeb(0);
        }
        if (tokens.length >= 7 && tokens[6].length() > 0){
            champlist.setMar(Integer.parseInt(tokens[6]));
        } else {
            champlist.setMar(0);
        }
        if (tokens.length >= 8 && tokens[7].length() > 0){
            champlist.setApr(Integer.parseInt(tokens[7]));
        } else {
            champlist.setApr(0);
        }
        if (tokens.length >= 9 && tokens[8].length() > 0){
            champlist.setMay(Integer.parseInt(tokens[8]));
        } else {
            champlist.setMay(0);
        }
        if (tokens.length >= 10 && tokens[9].length() > 0){
            champlist.setJun(Integer.parseInt(tokens[9]));
        } else {
            champlist.setJun(0);
        }
        if (tokens.length >= 11 && tokens[10].length() > 0){
            champlist.setJul(Integer.parseInt(tokens[10]));
        } else {
            champlist.setJul(0);
        }
        if (tokens.length >= 12 && tokens[11].length() > 0){
            champlist.setAug(Integer.parseInt(tokens[11]));
        } else {
            champlist.setAug(0);
        }
        if (tokens.length >= 13 && tokens[12].length() > 0){
            champlist.setSep(Integer.parseInt(tokens[12]));
        } else {
            champlist.setSep(0);
        }

        boatchamplist.add(champlist);

        Log.d("BoatLeaderboards","Just created: " + champlist);

    }
    } catch (IOException e){
        Log.wtf("BoatLeaderboards", "Error Reading Data File! on line" + line, e);

        e.printStackTrace();
    }

}

}

OverallBoatChamp.java

   import org.xml.sax.ext.*;

    public class OverallBoatChamp {
private String angler;
private int oct;
private int nov;
private int dec;
private int jan;
private int feb;
private int mar;
private int apr;
private int may;
private int jun;
private int jul;
private int aug;
private int sep;

public String getAngler() {
    return angler;
}

public void setAngler(String angler){
    this.angler = angler;
}

public int getOct() {
    return oct;
}

public void setOct(int oct){
    this.oct = oct;
}

public int getNov() {
    return nov;
}

public void setNov(int nov){
    this.nov = nov;
}

public int getDec() {
    return dec;
}

public void setDec(int dec){
    this.dec = dec;
}

public int getJan() {
    return jan;
}

public void setJan(int jan){
    this.jan = jan;
}

public int getFeb() {
    return feb;
}

public void setFeb(int feb){
    this.feb = feb;
}

public int getMar() {
    return mar;
}

public void setMar(int mar){
    this.mar = mar;
}

public int getApr() {
    return apr;
}

public void setApr(int apr){
    this.apr = apr;
}

public int getMay() {
    return may;
}

public void setMay(int may){
    this.may = may;
}

public int getJun() {
    return jun;
}

public void setJun(int jun){
    this.jun = jun;
}

public int getJul() {
    return jul;
}

public void setJul(int jul){
    this.jul = jul;
}

public int getAug() {
    return aug;
}

public void setAug(int aug){
    this.aug = aug;
}

public int getSep() {
    return sep;
}

public void setSep(int sep){
    this.sep = sep;
}

@Override
public String toString(){
    return "OverallBoatChamp{" +
    "Angler='" + angler + '\''+
    ", Oct=" + oct +
    ", Nov=" + nov +
    ", Dec=" + dec +
    ", Jan=" + jan +
    ", Feb=" + feb +
    ", Mar=" + mar +
    ", Apr=" + apr +
    ", May=" + may +
    ", Jun=" + jun +
    ", Jul=" + jul +
    ", Aug=" + aug +
    ", Sep=" + sep +
    '}';    
    }

}

ArrayList is serializable only if its element type is serializable, your class does not implement serializable.

To pass a serializable ArrayList you must use

intent.putExtra("OverallBoatCamp", OverallBoatChamp) test);

and then in the activity which you want to pass the ArrayList

ArrayList<OverallBoatChamp> mArrayList = (ArrayList<OverallBoatChamp>) getIntent().getSerializableExtra("OverallBoatCamp");

But keep in mind that parcelable has much better performace than serializable.

Have your class to implement Parcelable like this:

public class OverallBoatChamp implements Parcelable {

    public static Parcelable.Creator<OverallBoatChamp> CREATOR = new Parcelable.Creator() {
        @Override
        public OverallBoatChamp createFromParcel(Parcel parcel) {
            return new OverallBoatChamp(parcel);
        }

        @Override
        public OverallBoatChamp[] newArray(int size) {
            return new OverallBoatChamp[size];
        }
    }

    public OverallBoatChamp(Parcel parcel) {
        angler = parcel.readString();
        jan = parcel.readInt();
        // Do the same for all other fields
    }

    @Override
    public void writeToParcel(Parcel parcel, int flags) {
        parcel.writeString(angler);
        parcel.writeInt(jan);
        // Do the same for all other fields
    }

    @Override
    public int describeContents() {
        return 0;
    }

    // Your getters and setters

}

Then pass your data through the intent with the following code:

intent.putParcelableArrayListExtra("OverallBoatChamp", overallBoatChamps);

And in your activity:

ArrayList<OverallBoatChamp> overallBoatChamps = getIntent().getParcelableArrayListExtra("OverallBoatChamp");

Tip: store your intent extra keys in constants, usually in the activity where the extras belong to.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM