【问题标题】:Pass argument to parameter将参数传递给参数
【发布时间】:2019-09-27 14:09:23
【问题描述】:

我创建了一个实现 Parcelable 的类“Item”并设置了他的构造函数

public Item(Parcel in) {
    this.mName = in.readString();
    this.mFilePath = in.readString();
    this.mId = in.readInt();
    this.mLength = in.readInt();
    this.mTime = in.readLong();
}

我的问题是,当我使用该类时,我不知道要传递什么参数,例如下面的示例:

 Item item = new  Item(//What to put her!); 
    item.setId(c.getInt(c.getColumnIndex("_id")));
    item.setName(c.getString(c.getColumnIndex(DBHelperItem.COLUMN_NAME_RECORDING_NAME)));
    item.setFilePath(c.getString(c.getColumnIndex(DBHelperItem.COLUMN_NAME_RECORDING_FILE_PATH)));
    item.setLength(c.getInt(c.getColumnIndex(DBHelperItem.COLUMN_NAME_RECORDING_LENGTH)));
    item.setTime(c.getLong(c.getColumnIndex(DBHelperItem.COLUMN_NAME_TIME_ADDED))); 
return item;

【问题讨论】:

  • 添加第二个具有您需要的语义的构造函数。
  • 但我想使用那个特定的构造函数! @亨利
  • 我不明白你为什么想要那个。但在这种情况下,您必须手动构建具有正确结构的包裹。
  • 你能给我看一个简单的。 @亨利

标签: java android methods parameter-passing


【解决方案1】:

Item.java

public class Item {

    private String mName;
    private String mFilePath;
    private int mId;
    private int mLength;
    private long mTime;

    public Item(Parcel in) {
        this.mName = in.readString();
        this.mFilePath = in.readString();
        this.mId = in.readInt();
        this.mLength = in.readInt();
        this.mTime = in.readLong();
    }
}

Parcel.java(根据我们从您的代码中可以看到,它应该包含以下方法)

public class Parcel {

    public String readString() {

        //read a string
        return null;
    }

    public int readInt() {

        //read an int
        return 0;
    }

    public long readLong() {

        //read a long
        return 0;
    }
}

Test.java(你的主要方法所在)

public class Test {

    public static void main(String[] args) {


        Parcel p = new Parcel();

        /*Pass the parcel like this*/

        Item item = new Item(p);

        /*
         * 
         * Rest of the code
         * 
         * */
    }
}

也许您的方法定义与提供的代码不同。您应该相应地更改它们。

【讨论】:

    【解决方案2】:

    您可以在项目类中拥有其他构造函数

    public Item(mName,mFilepath,mId,mLength,mTime) {
        this.mName = mname;
        this.mFilePath = mFilepath;
        this.mId = mId;
        this.mLength = mLength;
        this.mTime = mTime;
    }
    

    在您的活动中初始化项目中的所有字段

     int id=(c.getInt(c.getColumnIndex("_id")));
     String name =   
      (c.getString(c.getColumnIndex(DBHelperItem.COLUMN_NAME_RECORDING_NAME)));
     String filePath=     
      (c.getString(c.getColumnIndex(DBHelperItem.COLUMN_NAME_RECORDING_FILE_PATH)));
     int length=     
      (c.getInt(c.getColumnIndex(DBHelperItem.COLUMN_NAME_RECORDING_LENGTH)));
     double time =    
      (c.getLong(c.getColumnIndex(DBHelperItem.COLUMN_NAME_TIME_ADDED))); 
    

    现在您可以使用上述字段创建新项目

    Item item = new Item(id, name,filePath,lenfth,time);

    这是我使用 Parcelable 的示例:https://github.com/riyaza15/ParcelableDemoenter code here

    【讨论】:

      【解决方案3】:

      Parcel.writeStringArray

      使用 Parcel.writeStringArray 捆绑您的字符串,然后将其传递给构造函数

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-01-27
        • 1970-01-01
        • 2021-04-13
        • 2021-04-18
        相关资源
        最近更新 更多