Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 将多个值传递给另一个活动,仅检索最后一个值_Android - Fatal编程技术网

Android 将多个值传递给另一个活动,仅检索最后一个值

Android 将多个值传递给另一个活动,仅检索最后一个值,android,Android,这件事让我抓狂。基本上,我试图做的是将多个值传递给下一个活动,但是当接收到下一个值中的值时,只有最后一个值通过 在活动A中,我有这个 public void aberdeen1(View v) { Intent i = new Intent(this, details.class); Bundle extras = new Bundle(); extras.putString(storename, "storenamevalue");

这件事让我抓狂。基本上,我试图做的是将多个值传递给下一个活动,但是当接收到下一个值中的值时,只有最后一个值通过

在活动A中,我有这个

public void aberdeen1(View v) {
        Intent i = new Intent(this, details.class);
        Bundle extras = new Bundle();
        extras.putString(storename, "storenamevalue");
        extras.putString(address1, "streetaddress");
        extras.putString(address2, "streetaddress2");
        extras.putString(town, "City");
        extras.putString(postcode, "AB12 3CD");         
        extras.putString(telnumber, "01234 567890");
        extras.putString(faxnumber, "01234 567899");
        i.putExtras(extras);
        startActivity(i);
    }
    Bundle extras = getIntent().getExtras();
String storename = extras.getString(StoreListA.storename);
storename1 = (TextView) findViewById(R.id.storename);
storename1.setText(storename);

String address1 = extras.getString(StoreListA.address1);
address01 = (TextView) findViewById(R.id.address1);
address01.setText(address1);

String address2 = extras.getString(StoreListA.address2);
address02 = (TextView) findViewById(R.id.address2);

String town = extras.getString(StoreListA.town);
town1 = (TextView) findViewById(R.id.town);

String postcode = extras.getString(StoreListA.postcode);
postcode1 = (TextView) findViewById(R.id.postcode); 

String telnumber = extras.getString(StoreListA.telnumber);
telnumber1 = (TextView) findViewById(R.id.telnumber);

String faxnumber = extras.getString(StoreListA.faxnumber);
faxnumber1 = (TextView) findViewById(R.id.faxnumber);

}
在活动B中,我有这个

public void aberdeen1(View v) {
        Intent i = new Intent(this, details.class);
        Bundle extras = new Bundle();
        extras.putString(storename, "storenamevalue");
        extras.putString(address1, "streetaddress");
        extras.putString(address2, "streetaddress2");
        extras.putString(town, "City");
        extras.putString(postcode, "AB12 3CD");         
        extras.putString(telnumber, "01234 567890");
        extras.putString(faxnumber, "01234 567899");
        i.putExtras(extras);
        startActivity(i);
    }
    Bundle extras = getIntent().getExtras();
String storename = extras.getString(StoreListA.storename);
storename1 = (TextView) findViewById(R.id.storename);
storename1.setText(storename);

String address1 = extras.getString(StoreListA.address1);
address01 = (TextView) findViewById(R.id.address1);
address01.setText(address1);

String address2 = extras.getString(StoreListA.address2);
address02 = (TextView) findViewById(R.id.address2);

String town = extras.getString(StoreListA.town);
town1 = (TextView) findViewById(R.id.town);

String postcode = extras.getString(StoreListA.postcode);
postcode1 = (TextView) findViewById(R.id.postcode); 

String telnumber = extras.getString(StoreListA.telnumber);
telnumber1 = (TextView) findViewById(R.id.telnumber);

String faxnumber = extras.getString(StoreListA.faxnumber);
faxnumber1 = (TextView) findViewById(R.id.faxnumber);

}

每个文本视图只显示传真号码。需要一些帮助,我尝试过其他方法,但这似乎是最好的方法。

您应该在这两个活动中使用常量,而不仅仅是将数据发送到的活动。还要记住,键是putString调用中的第一个参数,值是第二个参数。很难从代码中分辨出哪个是键,哪个是值

例:

extras.putString(StoreListA.storename,“这是名称的值”)

//存储类

public static class Store implements Serializable{
        String storeName;
        String address1;
        String address2;
        String town;
        String postCode;
        String telNumber;
        String faxNumber;

        Store(String storeName, 
              String address1, 
              String address2, 
              String town, 
              String postCode, String telNumber, String faxNumber){

            this.storeName = storeName;
            this.address1 = address1;
            this.address2 = address2;
            this.town = town;
            this.postCode = postCode;
            this.telNumber = telNumber;
            this.faxNumber = faxNumber;
        }
    }

    public void aberdeen1(View v) {
        Intent i = new Intent(this, details.class);
        i.putExtra("store", new Store("storenamevalue",
                                       "streetaddress",
                                       "streetaddress2",
                                       "City",
                                       "AB12 3CD",
                                       "01234 567890","01234 567899"));
        startActivity(i);
    }

//In Another Activity (Details).

//Whichever Activity you are coming from.

Activivty.Store store = (Activity.Store)getIntent()
                         .getExtras()
                         .getSerializable("store");

有关在活动之间传递对象的详细信息,请参见反复检查后的和,在工作了一天之后回家进行检查时,这些键不是唯一的。不太好,今天早上喝了一杯咖啡后,我换了,我们都很好。

你确定你的钥匙都是独一无二的吗?我知道这听起来是个很愚蠢的问题,但我该怎么检查钥匙呢?我在搜索时看到了前面提到的。所以这些键是你的StoreListA.storename等,你能在OP中发布吗?是的。我已经把它们放在OP中了,我只放了两个在那里,因为我想确保在进一步处理其他文本视图之前,前两个可以工作。我只知道StoreListA.storename和StoreListA.address1far@panini经过反复检查,钥匙并不是唯一的,在工作了一天之后,回到家里又发现了这一点。不好,今天早上喝了一杯咖啡后,我换了,我们都很好