首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从意图中获取Extras (变量)

从意图中获取Extras (变量)
EN

Stack Overflow用户
提问于 2017-01-03 21:08:12
回答 2查看 278关注 0票数 0

我想在我的java (android)项目中从另一个variables中获得三个classmain activity。因此,我将它们放入一个bundle中,并将其放入一个Intent中。变量来自我在第三个类中创建的Object,如下所示:

代码语言:javascript
复制
public class Transaction {
    private float value;
    private int transaction_Date;
    private String description;

public Transaction(float value, int transaction_Date, String description){
    super();
    this.value = value;
    this.transaction_Date = transaction_Date;
    this.description = description;
}

public float getValue(){
    return value;
}
public int getTransaction_Date(){
    return transaction_Date;
}
public String getDescription(){
    return description;
}


public void setValue(float value) {
    this.value = value;
}

public void setTransaction_Date(int transaction_Date) {
    this.transaction_Date = transaction_Date;
}

public void setDescription(String description) {
    this.description = description;
}

下面是我试图将数据输入到提到的Intent中的尝试

代码语言:javascript
复制
public class AddMoneyTransaction extends AppCompatActivity implements View.OnClickListener {

Button addDepositButton;
EditText depositInput, depositInputDate, depositInputNote;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_add_money_transaction);

    //setup the Button and EditText

    addDepositButton = (Button)findViewById(R.id.addDepositButton);
    depositInput = (EditText) findViewById(R.id.depositInput);
    depositInputDate = (EditText) findViewById(R.id.depositInputDate);
    depositInputNote = (EditText) findViewById(R.id.depositInputNote);

    //get the onClickListener

    addDepositButton.setOnClickListener(this);

}

@Override
public void onClick(View view) {
    Intent depositIntent = new Intent(AddMoneyTransaction.this, AssetsOverview.class);
    Transaction deposit = new Transaction(100, 16, "random comment");
    deposit.setValue(Float.parseFloat(depositInput.getText().toString()));
    deposit.setTransaction_Date(Integer.parseInt(depositInputDate.getText().toString()));
    deposit.setDescription(depositInputNote.getText().toString());

    Bundle depositBundle = new Bundle();
    depositBundle.putFloat("value", Float.parseFloat(depositInput.getText().toString()));
    depositBundle.putInt("date", Integer.parseInt(depositInputDate.getText().toString()));
    depositBundle.putString("description", depositInputNote.getText().toString());

    depositIntent.putExtras(depositBundle);
    startActivity(depositIntent);
}

这(当然)是行不通的:

代码语言:javascript
复制
//populated transaction list

protected void populateTransactionList() {

    Intent depositIntent = getIntent();
    Transaction deposit = (Transaction) getIntent().getExtras();
}

那么我如何才能从我的Intent中得到我的三个变量呢?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-01-03 21:17:24

传递自定义对象的方法是实现Parcelable接口,类似于下面给出的事务类。

使事务类可以像下面给出的代码片段那样进行打包。

代码语言:javascript
复制
import android.os.Parcel;
import android.os.Parcelable;

public class `Transaction` implements Parcelable {
    private float value;
    private int transaction_Date;
    private String description;

    public Transaction(float value, int transaction_Date, String description) {
        super();
        this.value = value;
        this.transaction_Date = transaction_Date;
        this.description = description;
    }

    public float getValue() {
        return value;
    }

    public int getTransaction_Date() {
        return transaction_Date;
    }

    public String getDescription() {
        return description;
    }


    public void setValue(float value) {
        this.value = value;
    }

    public void setTransaction_Date(int transaction_Date) {
        this.transaction_Date = transaction_Date;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeFloat(this.value);
        dest.writeInt(this.transaction_Date);
        dest.writeString(this.description);
    }

    protected Transaction(Parcel in) {
        this.value = in.readFloat();
        this.transaction_Date = in.readInt();
        this.description = in.readString();
    }

    public static final Parcelable.Creator<Transaction> CREATOR = new Parcelable.Creator<Transaction>() {
        @Override
        public Transaction createFromParcel(Parcel source) {
            return new Transaction(source);
        }

        @Override
        public Transaction[] newArray(int size) {
            return new Transaction[size];
        }
    };
}

将Parcelable对象放入意图中,如下所示

代码语言:javascript
复制
        public class AddMoneyTransaction extends AppCompatActivity implements View.OnClickListener {

        Button addDepositButton;
        EditText depositInput, depositInputDate, depositInputNote;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_add_money_transaction);

            //setup the Button and EditText

            addDepositButton = (Button)findViewById(R.id.addDepositButton);
            depositInput = (EditText) findViewById(R.id.depositInput);
            depositInputDate = (EditText) findViewById(R.id.depositInputDate);
            depositInputNote = (EditText) findViewById(R.id.depositInputNote);

            //get the onClickListener
            addDepositButton.setOnClickListener(this);

        }

        @Override
        public void onClick(View view) {
            Intent depositIntent = new Intent(AddMoneyTransaction.this, AssetsOverview.class);
           Transaction deposit = new Transaction(100, 16, "random comment");
           deposit.setValue(Float.parseFloat(depositInput.getText().toString()));
           deposit.setTransaction_Date(
Integer.parseInt(depositInputDate.getText().toString()));
           deposit.setDescription(depositInputNote.getText().toString());
           depositIntent.putExtra("data",deposit);
           startActivity(depositIntent);
        }

和接收意图,如下所示

代码语言:javascript
复制
//populated transaction list

protected void populateTransactionList() {

    Intent depositIntent = getIntent();
    Transaction deposit = depositIntent.getParcelableExtra("data");
}
票数 2
EN

Stack Overflow用户

发布于 2017-01-03 21:18:23

代码语言:javascript
复制
Transaction deposit = (Transaction) getIntent().getExtras();

临时演员是Bundle。你知道的,因为你有:

代码语言:javascript
复制
depositIntent.putExtras(depositBundle);

来填补那些额外的麻烦。

因为您在那个putFloat("value", ...)上调用了putInt("date", ...)putInt("date", ...)putString("description", ...),所以您可以使用相关的getter来根据这些键从附加值中检索值。这可以通过以下方式做到:

  • 直接在由getFloatExtra()返回的Intent上调用IntentgetIntExtra()getStringExtra(),或
  • 通过getIntent().getExtras()获取临时演员的getIntent().getExtras(),然后调用Bundle上的getFloat()getInt()getString()

Transaction不能进入附加程序,因为您不能将任意的Java对象放入附加文件中。您可以制作Transaction Parcelable,在这种情况下,您可以将其直接放入临时演员中。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41452398

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档