使用Intent和Bundle进行跳转Activity传值SparseArray数据

使用Intent和Bundle进行跳转Activity传值SparseArray数据

MainActivity.java

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.SparseArray;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private SparseArray mData;
    private Button btn;

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

    private void initData() {
        mData = new SparseArray<>();
        Text text = new Text("小白阳");
        Text text1 = new Text("小白阳2");
        mData.put(0, text);
        mData.put(1, text1);
    }

    private void initView() {
        btn = (Button) findViewById(R.id.btn);
        btn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn:
                Intent intent = new Intent(this, Main2Activity.class);
                Bundle bundle = new Bundle();
                bundle.putSparseParcelableArray("sparse", mData);
                intent.putExtras(bundle);
                startActivity(intent);
                break;
            default:
                break;
        }
    }
}

需要使Entity类Text实现Parcelable 接口
Text.java

import android.os.Parcel;
import android.os.Parcelable;

public class Text implements Parcelable {

    private String name;

    Text(String name) {
        this.name = name;
    }

    private Text(Parcel in) {
        name = in.readString();
    }

    public static final Creator CREATOR = new Creator() {
        @Override
        public Text createFromParcel(Parcel in) {
            return new Text(in);
        }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

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

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(name);
    }
}

Main2Activity.java

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.util.SparseArray;

public class Main2Activity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        Intent intent = getIntent();
        Bundle extras = intent.getExtras();
        SparseArray sparse = extras.getSparseParcelableArray("sparse");
        for (int i = 0; i < sparse.size(); i++) {
            Log.d("小白阳", sparse.get(i).getName());
        }
    }
}

程序运行结果:
这里写图片描述

你可能感兴趣的:(使用Intent和Bundle进行跳转Activity传值SparseArray数据)