Android studio - pass list data between two Activities

Foreword: usually pass int or string data, this time you need to pass List data, record the method

At first, Bundle was used to transfer. There are many tutorials on the Internet for this method, so I won’t write it. It was successful at the beginning, and it could indeed be delivered, but when I opened it again after a few days, it failed after modifying the data. So I changed the method.

Reference: Transfer List data between Activities

Record the method and sample code. The sample code is in the article linked above
1. Create an entity class Bean, and the Bean must implements Serializable:

package com.example.intentlist;
 
import java.io.Serializable;
 
public class Bean implements Serializable {
	private int id;
	private String name;
	private int age;
 
	public Bean() {
 
	}
 
	public Bean(int id, String name, int age) {
		this.id = id;
		this.name = name;
		this.age = age;
	}
 
	public int getId() {
		return id;
	}
 
	public void setId(int id) {
		this.id = id;
	}
 
	public String getName() {
		return name;
	}
 
	public void setName(String name) {
		this.name = name;
	}
 
	public int getAge() {
		return age;
	}
 
	public void setAge(int age) {
		this.age = age;
	}
 
}

2. Activity to be transferred:

package com.example.intentlist;
 
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
 
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
 
public class MainActivity extends ActionBarActivity {
	List<Bean> lstBean;
 
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
 
		lstBean = new ArrayList<Bean>();
		Bean bean = new Bean(1, "张三", 12);
		lstBean.add(bean);
		Bean bean1 = new Bean(2, "李四", 13);
		lstBean.add(bean1);
		Bean bean2 = new Bean(3, "王五", 14);
		lstBean.add(bean2);
		Bean bean3 = new Bean(4, "赵六", 15);
		lstBean.add(bean3);
		((Button) findViewById(R.id.btn_next)).setOnClickListener(new OnClickListener() {
 
			@Override
			public void onClick(View arg0) {
				Intent intentPut = new Intent(MainActivity.this, IntentTestActivity.class);
				intentPut.putExtra("lstBean", (Serializable) lstBean);
				startActivity(intentPut);
			}
		});
 
	}
}

3. Activity that receives the list:

package com.example.intentlist;
 
import java.util.List;
 
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.widget.TextView;
 
public class IntentTestActivity extends ActionBarActivity {
 
	@SuppressWarnings("unchecked")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_intent_test);
		Intent intentGet = getIntent();
		List<Bean> lstBean = (List<Bean>) intentGet.getSerializableExtra("lstBean");
		String result = "IntentTestActivity" + "\n";
		for (Bean bean : lstBean) {
			result += bean.getId() + "---" + bean.getName() + "---" + bean.getAge() + "\n";
		}
 
		((TextView) findViewById(R.id.tv_result)).setText(result);
	}
}

Guess you like

Origin blog.csdn.net/zzzzzwbetter/article/details/130057516