【问题标题】:How to pass value of array from one Activity to other?如何将数组的值从一个活动传递到另一个活动?
【发布时间】:2014-11-11 12:37:42
【问题描述】:

我正在构建一个 android 应用程序,用户可以在其中选择他们最喜欢的东西。

当用户点击物品的图片时,物品的名称被添加到一个数组中。

现在我想知道如何将该数组的值解析为任何片段并将其显示在我的微调器列表中。

例如:用户通过单击相应的图像来选择移动设备和平板电脑,然后将这些值添加到数组名称“stuffarray”中,现在我想通过“提交”按钮将此数组传递给我的片段,当我单击我的片段的微调器应该在列表中有移动和平板电脑的价值。

这是我的材料选择代码: 我正在构建一个 android 应用程序,用户可以在其中选择他们最喜欢的东西。

当用户点击物品的图片时,物品的名称被添加到一个数组中。

现在我想知道如何将该数组的值解析为任何片段并将其显示在我的微调器列表中。

例如:用户通过单击相应的图像来选择移动设备和平板电脑,然后将这些值添加到数组名称“stuffarray”中,现在我想通过“提交”按钮将此数组传递给我的片段,当我单击我的片段的微调器应该在列表中有移动和平板电脑的价值。

这是我的材料选择代码:

submite = (ImageButton) findViewById(R.id.nextscreen);      
next.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
    // TODO Auto-generated method stub

    Intent innext = new Intent(getApplicationContext(), MainActivitytabnew.class);

    startActivity(innext);              

});
img1 = (ImageButton) findViewById(R.id.imageButton1);


 img1.setBackgroundResource(R.drawable.mobile);   
 img1.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub

     isClicked1=!isClicked1;
        if (isClicked1) {
            img1.setImageResource(R.drawable.mobile);
            start();
            stuff1 = "mobile";

               myList.add(stuff1);



        }else {
            img1.setImageResource(R.drawable.mobile);
            myList.remove(sport1);
            //sport1 = "";  
            txt1.setText("");
        }
}
});

img2 = (ImageButton) findViewById(R.id.imageButton2);
img2.setBackgroundResource(R.drawable.tablet);
img2.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub

    isClicked2=!isClicked2;
    if (isClicked2) {
        img2.setImageResource(R.drawable.tablet);
        start();
        stuff2 = "tablet";
       myList.add(stuff2);
    }else {
        img2.setImageResource(R.drawable.tablet);
       // sport2 = "";
        myList.remove(sport2);
    }
}
});

【问题讨论】:

  • 你可以将arraylist设为静态。
  • 不,我不希望我的数组列表是静态的
  • 使用单例类的概念。4
  • 使用单例类的概念。
  • 如何使用单例类进行数组解析

标签: android arrays parsing android-fragments


【解决方案1】:

您也可以在打开之前设置值。

第一个活动

Intent intent = new Intent( this, YourOtherClass.class );
int[] myArray = { 0, 1, 2, 3 }; 
YourOtherClass.array = myArray;
startActivity( intent ); 

第二次活动

public static int[] array;

this 是您的活动。

【讨论】:

  • @thanks 你现在能告诉我如何将这个数组值添加到我的微调器中
  • 那是完全不同的故事。搜索微调器适配器。
  • 另外,如果此答案对您有帮助,请将其标记为正确。
【解决方案2】:

使用 putExtra():

int helloworld = 100;

Intent intent = new Intent( this, Class2.class );
intent.putExtra( "myInt", helloworld );
startActivity( intent ); 

然后在您的下一个活动中使用 getExtra() 见documentation for intent

int passedInt = getIntent().getExtras().getInt( "myInt" );

【讨论】:

  • 我的代码已经完成 - Intent innext = new Intent(getApplicationContext(), MainActivitytabnew.class); innext.putExtra(mysportlist, myList);开始活动(下一个); Toast.makeText(getApplicationContext(), mysportlist, Toast.LENGTH_SHORT).show();
  • 但是当我为检查它的值敬酒时,它是空的
  • 您还需要从其他类中获取值。 array[0] = getIntent().getExtras().getInt( "int" );
猜你喜欢
  • 1970-01-01
  • 2011-10-27
  • 2013-07-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-31
  • 1970-01-01
相关资源
最近更新 更多