0

Make an AdapterArray become an IntArray because I can't send to another activity an adapter array.

I'm creating a racing timer in Android Studio Java and I have saved my data about the race (example: time of all turns) in an Array Adapter. But when I try to send it to another Activity the function doesn't accept ArrayAdapter so I created a function to turn ArrayAdapter into an IntArray the problem is that whenever I run that function the program crashes. I'm looking for an answer to this problem by correcting my code or doing a completely different one if I had to.

public int[] AdapterToInt(ArrayAdapter adapter) {

    int[] a = new int[adapter.getCount()];
    for (int i = 0; i < adapter.getCount(); i++)
    {
        a[i] = Integer.valueOf(adapter.getItem(i));
    }
    return a;
}

public void onButtonClickSaveTimes(View view) {

    int[] array;
    array = AdapterToInt(adapter1);
    TextView textoexemplo = (TextView) findViewById(R.id.textViewhorario);    
   Intent myIntent = new Intent(getBaseContext(),   Main5Activity.class);
    myIntent.putExtra("car1", AdapterToInt(adapter1));
    myIntent.putExtra("car2", AdapterToInt(adapter2));
    myIntent.putExtra("car3", AdapterToInt(adapter3));
    myIntent.putExtra("car4", AdapterToInt(adapter4));
    myIntent.putExtra("car5", AdapterToInt(adapter5));
     startActivity(myIntent);

}

I expect it to send the ArrayAdapter to another activity or at least making my function work and turn ArrayAdapter into ArrayInt so i can send it through myIntent.putExtra, but the actual output is a app crash.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62

1 Answers1

0

The problem is likely here:

a[i] = Integer.valueOf(adapter.getItem(i));

Since adapater.getItem(i) returns an object that you likely will not be able to convert into an integer, you are probably going to have issues here.

You could instead use adapter.getItemId(i) which would return a long as opposed to an object.

However, that idea also doesn't seem to great so I would try to see if anything from here works for you.

And if none of that works it probably makes more sense to just send over all the data you need to reconstruct your ArrayAdapter to the other activity.

This link may also help: How do I pass custom ArrayAdapter Object between Intents with serialization

Anirudh
  • 2,648
  • 2
  • 13
  • 16