如何在两个Intent之间传递和检索ArrayList <integer>

时间:2016-06-13 04:50:30

标签: java android xml

这是我的第一个活动

ArrayList<Integer> deletedPositions = new ArrayList<>();
.
.
.
deletedPositions.add(2);
Log.d("Deleted Position is: ", " " + deletedPositions);
// I can see 2 in my Log


public void onBackPressed(){
        super.onBackPressed();
        Log.e("----------------->", "Back Pressed");
        Intent intent=new Intent();
        intent.putExtra("isdeleted",true);
        intent.putExtra("pos",deletedPositions);
        setResult(2,intent);
        finish();
    }

这是我的接收活动

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        Log.d("----------->","onActivityResult Called " + requestCode + " " + requestCode + " " + data);
        super.onActivityResult(requestCode, resultCode, data);
       if(requestCode==2 && data != null)
      {
            ArrayList<Integer> positionsToBeRemoved = data.getIntegerArrayListExtra("pos");

但是我的数据应该是2,这应该是2 我在哪里做错了? 谢谢

2 个答案:

答案 0 :(得分:0)

试试这可能会对你有所帮助

ArrayList<Integer> deletedPositions = new ArrayList<>();
Intent intent=new Intent();
intent.putExtra("isdeleted",true);
intent.putIntegerArrayListExtra("pos",deletedPositions);
setResult(2,intent);

获得结果

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
   Log.d("----------->","onActivityResult Called " + requestCode + " " + requestCode + " " + data);
   super.onActivityResult(requestCode, resultCode, data);
   if(requestCode==2 && data != null)
   {
       ArrayList<Integer> positionsToBeRemoved = data.getExtras().getIntegerArrayList("pos");

   }

答案 1 :(得分:0)

删除super.onBackPressed();在onBackPressed方法中行并检查。

第一项活动

public class MainActivity extends AppCompatActivity
{

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


        startActivityForResult(new Intent(this, Main2Activity.class), 100);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode==100 && resultCode==100 && data!=null)
        {
            System.out.println("pos "+data.getExtras().getIntegerArrayList("pos"));


        }
    }
}

第二项活动

public class Main2Activity extends AppCompatActivity
{
    ArrayList<Integer> mDeletedPosLst;

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

        mDeletedPosLst = new ArrayList<>();

        mDeletedPosLst.add(1);
        mDeletedPosLst.add(3);
        mDeletedPosLst.add(5);
        mDeletedPosLst.add(6);
    }

    @Override
    public void onBackPressed()
    {
        //super.onBackPressed();

        Intent intent=new Intent();
        intent.putExtra("isdeleted",true);
        intent.putExtra("pos",mDeletedPosLst);
        setResult(100,intent);
        finish();
    }
}