Search code examples
androidarrayssparse-matrix

Convert Array list to Sparse Array


I have copied some code from a project and want to reuse a small part of it in my private app.

The class contains a Sparse Array

public class GolfResult {

    String hcpAfter;
    String hcpBefore;
    SparseArray roundResults;

    public GolfResult() {
        hcpAfter = "";
        hcpBefore = "";
        roundResults = new SparseArray();
    }
}

I have created an ArrayList for roundResults that is filled with the necessary data.

Then I am trying to fill the instance with content.

GolfResult golferRes = new GolfResult();
SparseArray<RoundResults> hu= new SparseArray<>();
hu = roundresults; // *
golferRes.setHcpAfter("33");
golferRes.setHcpBefore("kk");
golferRes.setRoundResults(hu);

But the problem is that hu = roudresults is not possible, because of the error message:

required: Android.util.SparseArray found: java.util.Array List

Any help will be welcome.

After receiving two helpful answers I got a step further, but now I am facing the problem that my SparseArray hu is empty {}.

The content of hu should be the class roundresults that has the following structure:

public class RoundResults {
boolean actualRound;
private List<HoleResult> holeResults;
Integer roundId;
Integer roundNumber;
String unfinishedReason;

The arrayList roundresults has the size of 1 and has data in the objects.

unfinishedReason =""
holeResults = ArrayLIST size= 18
roundID = "1"
roundNumber = "1"
actualRound = true

hu ={}

mValues = All elements are null
mSize = 0

Does anybody have an idea why?


Solution

  • If I understand well your problem, maybe you can try with this:

    for ( int i=0; i<roundresults.size(); i++ ) {
        hu.put(i,roundresults.get(i));
    }