Marek
Marek

Reputation: 4081

How to create ListView from an Object with multiple arrays

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

Upvotes: 3

Views: 214

Answers (2)

bogdan
bogdan

Reputation: 782

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;
}

}

Upvotes: 1

Artem Zelinskiy
Artem Zelinskiy

Reputation: 2210

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[][]>

Upvotes: 0

Related Questions