展会信息港展会大全

Android Intent传递对象和ArrayList【大明进化十八】
来源:互联网   发布日期:2016-01-14 09:44:00   浏览:938次  

导读:以前做项目的时候,需要Intent 传递复杂类型,例如:数组,ArrayList类型,传递类对象,今天抽空写了个例子,方便以后用的时候查询!有问题可以留言;转载请标明出处:...

以前做项目的时候,需要Intent 传递复杂类型,例如:数组,ArrayList类型,传递类对象,今天抽空写了个例子,方便以后用的时候查询!有问题可以留言;

转载请标明出处:

http://blog.csdn.net/wdaming1986/article/details/6762633

程序主界面:点击list按钮传递数据:

点击parcelable传递数据:点击serializable传递数据:

下面看代码:

一、MainAcitivty。java类的代码:

<span style="font-size:16px;color:#000000;">package com.cn.daming;

import java.io.Serializable;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

import android.app.Activity;

import android.app.ListActivity;

import android.content.Intent;

import android.graphics.Color;

import android.graphics.drawable.GradientDrawable;

import android.graphics.drawable.GradientDrawable.Orientation;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

public class MainActivity extends Activity implements Serializable{

private static final long serialVersionUID = 1L;

private String s_name;

private int s_number;

private String s_sex;

private Button list_Button;

private Button ser_Button;

private Button par_Button;

private ArrayList<String> m_list;

publicfinal static String PAR_KEY = "com.cn.daming.parcelable";

publicfinal static String SER_KEY = "com.cn.daming.serializable";

publicfinal static String LIST_KEY = "com.cn.daming.ArrayList";

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

initlist();

drawBackground();

initList_Button();

initPar_Button();

inintSer_Button();

}

public void initlist()

{

m_list = new ArrayList<String>();

m_list.add("大明ArrayList");

m_list.add("年龄:25岁");

m_list.add("性别:男");

}

public void drawBackground()

{

GradientDrawable grad = new GradientDrawable(

Orientation.TL_BR,

new int[] {Color.rgb(0, 0, 127),

Color.rgb(0, 0, 255),

Color.rgb(127, 0, 255),

Color.rgb(127, 127, 255),

Color.rgb(127, 255, 255),

Color.rgb(255, 255, 255)}

);

this.getWindow().setBackgroundDrawable(grad);

}

public void initList_Button()

{

list_Button = (Button)findViewById(R.id.list_button);

list_Button.setOnClickListener(new OnClickListener(){

@Override

public void onClick(View arg0) {

Intent list_intent = new Intent();

list_intent.putStringArrayListExtra(LIST_KEY, m_list);

list_intent.setClass(MainActivity.this, ShowListView.class);

startActivity(list_intent);

}

});

}

public void initPar_Button()

{

par_Button = (Button)findViewById(R.id.par_button);

par_Button.setOnClickListener(new OnClickListener(){

@Override

public void onClick(View arg0) {

Student m_Student = new Student();

m_Student.setName("大明例子");

m_Student.setAge(25);

m_Student.setSex("男");

Intent p_Intent = new Intent(MainActivity.this,ShowParView.class);

Bundle mBundle = new Bundle();

mBundle.putParcelable(PAR_KEY, m_Student);

p_Intent.putExtras(mBundle);

startActivity(p_Intent);

}

});

}

public void inintSer_Button()

{

ser_Button = (Button)findViewById(R.id.ser_button);

ser_Button.setOnClickListener(new OnClickListener(){

@Override

public void onClick(View arg0) {

MainActivity s_activity = new MainActivity();

s_activity.setS_name("Daming Serlizable!");

s_activity.setS_number(25);

s_activity.setS_sex("男");

Intent mIntent = new Intent(MainActivity.this,ShowSerView.class);

Bundle mBundle = new Bundle();

mBundle.putInt("state", 3);

mBundle.putSerializable(SER_KEY, s_activity);

mIntent.putExtras(mBundle);

startActivity(mIntent);

}

});

}

public void setS_name(String s_name) {

this.s_name = s_name;

}

public String getS_name() {

return s_name;

}

public void setS_number(int s_number) {

this.s_number = s_number;

}

public int getS_number() {

return s_number;

}

public void setS_sex(String s_sex) {

this.s_sex = s_sex;

}

public String getS_sex() {

return s_sex;

}

}</span>

二、Student。java类的代码:

package com.cn.daming;

import android.os.Parcel;

import android.os.Parcelable;

public class Student implements Parcelable{

private String name;

private int age;

private String sex;

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;

}

public String getSex() {

return sex;

}

public void setSex(String sex) {

this.sex = sex;

}

public static final Parcelable.Creator<Student> CREATOR = new Creator<Student>() {

public Student createFromParcel(Parcel source) {

Student mStudent = new Student();

mStudent.name = source.readString();

mStudent.age = source.readInt();

mStudent.sex = source.readString();

return mStudent;

}

public Student[] newArray(int size) {

return new Student[size];

}

};

@Override

public int describeContents() {

// TODO Auto-generated method stub

return 0;

}

@Override

public void writeToParcel(Parcel parcel, int arg1) {

parcel.writeString(name);

parcel.writeInt(age);

parcel.writeString(sex);

}

}

三、ShowListView。java类的代码:

package com.cn.daming;

import java.util.ArrayList;

import android.app.Activity;

import android.content.Intent;

import android.graphics.Color;

import android.graphics.drawable.GradientDrawable;

import android.graphics.drawable.GradientDrawable.Orientation;

import android.os.Bundle;

import android.widget.TextView;

public class ShowListView extends Activity{

private Intent list_intent;

private ArrayList<String> m_arrayList;

private TextView list_textview;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.show_list_view);

drawBackground();

list_textview = (TextView)findViewById(R.id.list_text_view);

list_intent = getIntent();

m_arrayList = list_intent.getExtras().getStringArrayList(MainActivity.LIST_KEY);

m_arrayList.get(0);

list_textview.setText(m_arrayList.get(0)+" \n"+m_arrayList.get(1)+"\n"+m_arrayList.get(2));

}

public void drawBackground()

{

GradientDrawable grad = new GradientDrawable(

Orientation.TL_BR,

new int[] {Color.rgb(0, 0, 127),

Color.rgb(0, 0, 255),

Color.rgb(127, 0, 255),

Color.rgb(127, 127, 255),

Color.rgb(127, 255, 255),

Color.rgb(255, 255, 255)}

);

this.getWindow().setBackgroundDrawable(grad);

}

}

四、ShowParView。java类的代码:

package com.cn.daming;

import android.app.Activity;

import android.graphics.Color;

import android.graphics.drawable.GradientDrawable;

import android.graphics.drawable.GradientDrawable.Orientation;

import android.os.Bundle;

import android.widget.TextView;

public class ShowParView extends Activity{

private TextView par_text_view;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.show_par_view);

drawBackground();

par_text_view = (TextView)findViewById(R.id.par_text_view);

Student p_student = (Student)getIntent().getParcelableExtra(MainActivity.PAR_KEY);

par_text_view.setText("姓名: " + p_student.getName()+"\n"+

"年龄: " + p_student.getAge() + "\n" +

"性别: " + p_student.getSex() + "\n" +

"类:" + p_student.getClass());

}

public void drawBackground()

{

GradientDrawable grad = new GradientDrawable(

Orientation.TL_BR,

new int[] {Color.rgb(0, 0, 127),

Color.rgb(0, 0, 255),

Color.rgb(127, 0, 255),

Color.rgb(127, 127, 255),

Color.rgb(127, 255, 255),

Color.rgb(255, 255, 255)}

);

this.getWindow().setBackgroundDrawable(grad);

}

}

五、ShowSerView。java类的代码:

package com.cn.daming;

import android.app.Activity;

import android.graphics.Color;

import android.graphics.drawable.GradientDrawable;

import android.graphics.drawable.GradientDrawable.Orientation;

import android.os.Bundle;

import android.widget.TextView;

public class ShowSerView extends Activity{

private TextView ser_text_view;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.show_ser_view);

drawBackground();

ser_text_view = (TextView)findViewById(R.id.ser_text_view);

MainActivity s_activity = (MainActivity)getIntent().getSerializableExtra(MainActivity.SER_KEY);

ser_text_view.setText("You name is: " + s_activity.getS_name() + "\n"+

"You age is: " + s_activity.getS_number() +"\n"+

"You sex is: " + s_activity.getS_sex());

}

public void drawBackground()

{

GradientDrawable grad = new GradientDrawable(

Orientation.TL_BR,

new int[] {Color.rgb(0, 0, 127),

Color.rgb(0, 0, 255),

Color.rgb(127, 0, 255),

Color.rgb(127, 127, 255),

Color.rgb(127, 255, 255),

Color.rgb(255, 255, 255)}

);

this.getWindow().setBackgroundDrawable(grad);

}

}

xml布局文件

一、main。xml布局文件:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

>

<TextView

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_gravity="center_horizontal"

android:gravity="center_horizontal"

android:text="@string/hello"

android:textSize="12pt"

/>

<Button

android:id="@+id/list_button"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_gravity="center_horizontal"

android:gravity="center_horizontal"

android:layout_marginTop="10dip"

android:text="Intent传递list"

android:textSize="12pt"

/>

<Button

android:id="@+id/par_button"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_marginTop="10dip"

android:layout_gravity="center"

android:text="parcelable传递对象"

android:textSize="12pt"

/>

<Button

android:id="@+id/ser_button"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_gravity="center_horizontal"

android:gravity="center_horizontal"

android:layout_marginTop="10dip"

android:text="serializable传递对象"

android:textSize="12pt"

/>

</LinearLayout>

二、show_list_view.xml布局文件:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

>

<TextView

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_marginBottom="5dip"

android:layout_gravity="center_horizontal"

android:gravity="center_horizontal"

android:text="传递过来的ArrayList的值"

android:textSize="12pt"

/>

<TextView

android:id="@+id/list_text_view"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_marginBottom="10dip"

android:layout_gravity="center_horizontal"

android:gravity="center_horizontal"

android:textSize="12pt"

/>

</LinearLayout>

三、show_par_view.xml布局文件:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

>

<TextView

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_gravity="center_horizontal"

android:layout_marginBottom="10dip"

android:gravity="center_horizontal"

android:text="接受从MainActivity中传递过来的对象"

android:textSize="12pt"

/>

<TextView

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_marginBottom="5dip"

android:layout_gravity="center_horizontal"

android:gravity="center_horizontal"

android:text="传递过来的Parcelable值"

android:textSize="12pt"

/>

<TextView

android:id="@+id/par_text_view"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_gravity="center_horizontal"

android:gravity="center_horizontal"

android:textSize="12pt"

/>

</LinearLayout>

四、show_ser_view.xml布局文件:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

>

<TextView

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_marginBottom="5dip"

android:layout_gravity="center_horizontal"

android:gravity="center_horizontal"

android:text="传递过来的Serializable值"

android:textSize="12pt"

/>

<TextView

android:id="@+id/ser_text_view"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_marginBottom="10dip"

android:layout_gravity="center_horizontal"

android:gravity="center_horizontal"

android:textSize="12pt"

/>

</LinearLayout>

Manifest.xml布局文件:

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

package="com.cn.daming"

android:versionCode="1"

android:versionName="1.0">

<application android:icon="@drawable/icon" android:label="@string/app_name">

<activity android:name=".MainActivity"

android:label="@string/app_name">

<intent-filter>

<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />

</intent-filter>

</activity>

<activity android:name=".ShowListView"/>

<activity android:name=".ShowParView"/>

<activity android:name=".ShowSerView"/>

</application>

<uses-sdk android:minSdkVersion="8" />

</manifest>

赞助本站

人工智能实验室

相关热词: android开发 教程

AiLab云推荐
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港