3

I am lost again. My object is as follows:

public class LogInfo implements Serializable{


public ArrayList<Point[][]> strokes;
public LinkedList<byte[]> codes;
public int[] times;

...
}

First of all I populate the ListView from an ArrayList of this objects. Then I select an object from the ListView, and I would like to populate new list in new fragment with fields

public ArrayList<Point[][]> strokes;
public LinkedList<byte[]> codes;

However, to create an ArrayAdapter I can't just pass an object to it (as I understand). I need to pass an array. the problem is, that I would like to pass an object previously created and selected, and then populate the list from its fields (not only strokes or codes, but both of them).

How should my ObjectAdapter class look like and what class should it extend? To select an Object from ArrayList of Objects I used:

public class LogInfoObjectAdapter extends ArrayAdapter<LogInfo>

Example (real life):

I have a lot of cars on the parking and I need to make a list of them, so I use ArrayAdapter to populate the list. After I choose one car from the list (car object) it has two arrays in it (for example broken bulbs and broken tires, but both array are same size). Now I want to will the new list with information from selected car. I hope it is clear enough. My problem is that to use ArrayAdapter I have to pass an array in the constructor, but I want to pass the whole object and inside my adapter class process it and add choosen fields to ListView

Marek
  • 3,935
  • 10
  • 46
  • 70
  • Check: [How to use ArrayAdapter](http://stackoverflow.com/questions/2265661/how-to-use-arrayadaptermyclass) – Paresh Mayani May 20 '13 at 10:21
  • Paresh Mayani, this is not what I am looking for, I already did what is in the answer posted by you. Now I need to load fields from selected object int a new list. Maybe it will be more clear with an example. I update my question – Marek May 20 '13 at 10:23

2 Answers2

1

If you have an object with multiple lists, you don't need to extend ArrayAdapter, you can just extend the BaseAdapter and implement the methods needed (getCount(), getView(), etc).

public class Adapter extends BaseAdapter {

class LogInfo implements Serializable {


    public ArrayList<Point[][]> strokes;
    public LinkedList<byte[]> codes;
    public int[] times;
}

private LogInfo mInfo;
public Adapter(LogInfo info) {
    mInfo = info;
}



@Override
public int getCount() {
    if (mInfo != null && mInfo.strokes != null) {
    return mInfo.strokes.size();
    }
    return 0;
}

@Override
public Object getItem(int i) {
    return null;
}

@Override
public long getItemId(int i) {
    return 0;
}

@Override
public View getView(int i, View view, ViewGroup viewGroup) {
    if (mInfo != null) {
    Point[][] p = mInfo.strokes.get(i);
    byte[] b = mInfo.codes.get(i);
    //create the view
    }
    return null;
}

}

bogdan
  • 782
  • 3
  • 7
  • Could you give more information on how to iterate through my object passed in a constructor of BaseAdapter? I never used it. I pass in a constructor LogInfo object (not an array). Thank you in advance. – Marek May 20 '13 at 10:41
  • Thank you, my solution is a little different that yours but I understood it. – Marek May 20 '13 at 12:45
0

1) Array adapter has method getItem() you can use it to get particaular item by index. 2) Make your LogInfo implements IIterable interface http://developer.android.com/reference/java/lang/Iterable.html

public class LogInfo implements Serializable, Iterable<Point[][]>{


public ArrayList<Point[][]> strokes;
public LinkedList<byte[]> codes;
public int[] times;

public abstract Iterator<Point[][]> iterator (){

//Iterator implementation

}

Now you can use this object directly in other list whitch has following signature

public class LogInfoObjectAdapter extends ArrayAdapter<Point[][]>
Artem Zelinskiy
  • 2,201
  • 16
  • 19
  • Greensy what do you think about BaseAdapter? – Marek May 20 '13 at 10:37
  • Why do you need base adapter? do you want to show different types of data in one list? – Artem Zelinskiy May 20 '13 at 10:39
  • the previous answer was about BaseAdapter. Now I am confused. I just want to choose an object from first list (in first Fragment) and then create another list in new fragment and display information from its two arrays. P.S why did you extend ArrayAdapter? I would like to have access to all fields of object not only to strokes. – Marek May 20 '13 at 10:43