开发者

Parcelable class

I have a class below and would like to implement Parcelable. I hit the error in. Please advice.

 in.readValue(row);

Error Message: The method readValue(ClassLoader) in the type Parcel is not applicable for the arguments (ArrayList)

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;

import android.os.Parcel;
import android.os.Parcelable;

public class cls_datatable implements Parcelable {
private ArrayList<String> column = new ArrayList<String>();
private ArrayList<cls_datarow> row = new ArrayList<cls_datarow>();
private int indexCounter = 0;
private Hashtable<Integer, Integer> IDnIndex = new Hashtable<Integer, Integer>();

public cls_datatable(Parcel in) {
    readFromParcel(in);
}

public class cls_datarow {

    private ArrayList<String> columnValues;
    private int id;

    public cls_datarow(int id) {
        this.id = id;
        columnValues = new ArrayList<String>();
        for (int i = 0; i < column.size(); i++)
            columnValues.add(null);
    }

    public cls_datarow() {
        columnValues = new ArrayList<String>();
        for (int i = 0; i < column.size(); i++)
            columnValues.add(null);
    }

    public int getID() {
        return id;
    }

    public 开发者_如何学GoString get(String columnName) {
        return columnValues.get(column.indexOf(columnName));
    }

    public String get(int columnIndex) {
        return columnValues.get(columnIndex);
    }

    public void set(String columnName, String value) {
        columnValues.set(column.indexOf(columnName), value);
    }

    public void set(int columnIndex, String value) {
        columnValues.set(columnIndex, value);
    }

    public int getColumnSize() {
        return column.size();
    }
}

public void addColumn(String columnName) {
    column.add(columnName);
}

// Depreciated function
public void addAllColumns(ArrayList<String> columns) {
    column.addAll(columns);
}

public void addAllColumns(String[] columns) {
    for (String col : columns) {
        column.add(col);
    }
}

public int getColumnSize() {
    return column.size();
}

public int getColumnIndex(String columnName) {
    return column.indexOf(columnName);
}

public int getRowSize() {
    return row.size();
}

public cls_datarow getRow(int rowIndex) {
    return row.get(rowIndex);
}

public void remove(int rowIndex) {
    row.remove(rowIndex);
    refreshIDnIndex();
}

public void remove(cls_datarow dataRow) {
    row.remove((int) IDnIndex.get(dataRow.getID()));
    refreshIDnIndex();
}

public void update(cls_datarow dataRow) {
    row.set(IDnIndex.get(dataRow.getID()), dataRow);
}

public void add(cls_datarow row) {
    IDnIndex.put(row.getID(), this.row.size());
    this.row.add(row);
}

public void addAllRows(ArrayList<cls_datarow> row) {
    for (cls_datarow r : row) {
        IDnIndex.put(r.getID(), this.row.size());
        this.row.add(r);
    }
}

public cls_datarow newRow() {
    cls_datarow dr = new cls_datarow(indexCounter);
    indexCounter++;
    return dr;
}

public cls_datatable copy() {
    cls_datatable dt = new cls_datatable(null);
    dt.addAllColumns(column);
    dt.addAllRows(row);
    return dt;
}

// inappropriate method
public ArrayList<cls_datarow> getAllRows() {
    return row;
}

public void clear() {
    row.clear();
}

public cls_datarow[] select(HashMap<String, String> condition) {
    ArrayList<cls_datarow> rs = new ArrayList<cls_datarow>();
    boolean isMatch = true;
    Iterator<String> i;

    for (cls_datarow r : row) {
        isMatch = true;
        i = condition.keySet().iterator();
        while (i.hasNext()) {
            String key = i.next();
            if (r.get(key) == null && condition.get(key) != null)
                isMatch = false;
            if (r.get(key) != null && condition.get(key) == null)
                isMatch = false;
            if (r.get(key) != null && condition.get(key) != null
                    && !r.get(key).equals(condition.get(key)))
                isMatch = false;
        }

        if (isMatch)
            rs.add(r);
    }

    return rs.toArray(new cls_datarow[rs.size()]);
}

private void refreshIDnIndex() {
    IDnIndex.clear();
    for (int i = 0; i < row.size(); i++)
        IDnIndex.put(row.get(i).getID(), i);
}

@Override
public int describeContents() {
    // TODO Auto-generated method stub
    return 0;
}


@Override
public void writeToParcel(Parcel dest, int flags) {

    // We just need to write each field into the
    // parcel. When we read from parcel, they
    // will come back in the same order
    dest.writeStringList(column);
    dest.writeValue(row);
    dest.writeInt(indexCounter);
    dest.writeValue(IDnIndex);
}

private void readFromParcel(Parcel in) {

    // We just need to read back each
    // field in the order that it was
    // written to the parcel
    in.readStringList(column);
     in.readValue(row);
    //indexCounter
    //IDnIndex
}


public static final Parcelable.Creator CREATOR =
    new Parcelable.Creator() {
        public cls_datatable createFromParcel(Parcel in) {
            return new cls_datatable(in);
        }

        public cls_datatable[] newArray(int size) {
            return new cls_datatable[size];
        }
    };

}


The error is because readValue is expecting an object of type ClassLoader and you are passing it an ArrayList. But your bigger problem is that you can't just parcel and unparcel a list of objects of any type. Look at the documentation for writeValue:

http://developer.android.com/reference/android/os/Parcel.html#writeValue(java.lang.Object)

Your class cls_datarow needs to be supported by this method somehow. I would just have cls_datarow implement Paracable and use writeTypedList to write it and createTypedArrayList to read it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜