Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android intent 傳遞對象以及返回刷新

Android intent 傳遞對象以及返回刷新

編輯:關於Android編程

之前項目需要,找過這方面知識。所以今天也總結一下。大家都知道activity跳轉用intent,Android的當前頁面跳轉到新的頁面。當然跳轉的同時常常要攜帶數據或者對象。那我下面就說說跳轉帶對象吧。還有在例子當中,新的activity返回時,也有對象一起返回。下面看代碼,只要入門Android了都看得懂,我就不再寫很多注釋了。

 

有兩種對象類型可以傳遞,一是Parcelable,推薦用這個。二是Serializable。當然你在class建立對象時,都要對這兩個進行接口。看代碼。

 

package com.example.intentdemo;

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

public class User implements Parcelable{

	private String name;
	private String sex;
	private int eage;
	
	public String getName() {
		return name;
	}

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

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public int getEage() {
		return eage;
	}

	public void setEage(int eage) {
		this.eage = eage;
	}
	
	public static final Parcelable.Creator CREATOR = new Creator() {
		public User createFromParcel(Parcel source) {
			User user = new User();
			user.name = source.readString();
			user.sex = source.readString();
			user.eage = source.readInt();
			return user;
		}

		public User[] newArray(int size) {
			return new User[size];
		}
	};

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

	@Override
	public void writeToParcel(Parcel parcel, int arg1) {
	     parcel.writeString(name);  
	     parcel.writeString(sex);  
	     parcel.writeInt(eage);  
	}

}

package com.example.intentdemo;

import java.io.Serializable;

public class Student implements Serializable {
	private String name;
	private String sex;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public int getEage() {
		return eage;
	}
	public void setEage(int eage) {
		this.eage = eage;
	}
	private int eage;
}

MainActivity.class

 

 

package com.example.intentdemo;

import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener {

	private ArrayList mArrayListUsers = new ArrayList();
	private ArrayList mArrayListStudent = new ArrayList();

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

		InitUI();
	}

	private void InitUI() {
		Button Parcelable = (Button) findViewById(R.id.button1);
		Button Serializable = (Button) findViewById(R.id.button2);

		if (Parcelable != null) {
			Parcelable.setOnClickListener(this);
		}
		if (Serializable != null) {
			Serializable.setOnClickListener(this);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public void onClick(View arg0) {
		if (arg0.getId() == R.id.button1) {
			setParcelable();
		} else if (arg0.getId() == R.id.button2) {
			setSerializable();
		}

	}

	private void setSerializable() {
		Student student = new Student();
		student.setName(Serializable);
		student.setSex(nv);
		student.setEage(25);

		mArrayListStudent.add(student);
		mArrayListStudent.add(student);

		Intent mIntent = new Intent(this, IntentSerializableActivity.class);
		Bundle mBundle = new Bundle();
		mBundle.putSerializable(student_key, student); // 傳遞一個user對象
		mBundle.putSerializable(student_Arry, mArrayListStudent); // 傳遞一個user對象列表
		mIntent.putExtras(mBundle);

		startActivityForResult(mIntent, 200); // 帶返回結果的Intent,標志碼是1

	}

	private void setParcelable() {
		User user = new User();
		user.setName(Parcelable);
		user.setSex(nan);
		user.setEage(25);

		mArrayListUsers.add(user);
		mArrayListUsers.add(user);

		Intent mIntent = new Intent(this, IntentParcelableActivity.class);
		Bundle mBundle = new Bundle();
		mBundle.putParcelable(User_key, user); // 傳遞一個user對象
		mBundle.putParcelableArrayList(User_Arry, mArrayListUsers); // 傳遞一個user對象列表
		mIntent.putExtras(mBundle);

		startActivityForResult(mIntent, 100); // 帶返回結果的Intent,標志碼是1

	}

	// 返回activity頁面刷新
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		TextView show = (TextView) findViewById(R.id.test);
		if (resultCode == RESULT_OK && requestCode == 100) {

			Bundle bundle = this.getIntent().getExtras();
			if (bundle != null) {
				mArrayListUsers = bundle.getParcelableArrayList(User_Arry);
			}

			show.setText();
			show.append(IntentParcelableActivity返回的數據:  + 
);
			for (int i = 0; i < mArrayListUsers.size(); i++) {
				User userArrayList = new User();
				userArrayList = mArrayListUsers.get(i);
				show.append(name is:  + userArrayList.getName() + 

						+ age is:  + userArrayList.getEage() + 
);
			}

		} else if (resultCode == RESULT_OK && requestCode == 200) {

			Bundle bundle = this.getIntent().getExtras();
			if (bundle != null) {
				mArrayListStudent = (ArrayList) bundle
						.getSerializable(student_Arry);
			}

			show.setText();
			show.append(IntentSerializableActivity返回的數據:  + 
);
			for (int i = 0; i < mArrayListStudent.size(); i++) {
				Student studentArrayList = new Student();
				studentArrayList = mArrayListStudent.get(i);
				show.append(name is:  + studentArrayList.getName() + 

						+ age is:  + studentArrayList.getEage() + 
);
			}

		}
	}

}

接受數據新的activity

 

IntentParcelableActivity.class

 

package com.example.intentdemo;

import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.KeyEvent;
import android.view.Menu;
import android.widget.TextView;

public class IntentParcelableActivity extends Activity {
	private ArrayList mArrayListUsers = new ArrayList();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		TextView mTextView = new TextView(this);

		User user = new User();
		Bundle bundle = this.getIntent().getExtras();
		if (bundle != null) {
			user = bundle.getParcelable(User_key);
			mArrayListUsers = bundle.getParcelableArrayList(User_Arry);
		}

		mTextView.append(You name is:  + user.getName() + 

				+ You age is:  + user.getEage() + 
 + 
);

		for (int i = 0; i < mArrayListUsers.size(); i++) {
			User userArrayList = new User();
			userArrayList = mArrayListUsers.get(i);
			mTextView.append(You name is:  + userArrayList.getName() + 

					+ You age is:  + userArrayList.getEage() + 
);
		}

		setContentView(mTextView);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
	// 返回按鍵調用
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			Intent intent = new Intent();
			Bundle mBundle = new Bundle();  
			mBundle.putParcelableArrayList(User_Arry, mArrayListUsers);   //傳遞一個user對象列表
		    intent.putExtras(mBundle);  	
		    setResult(RESULT_OK, intent);
			finish();
		}
		return true;
	}

}

IntentSerializableActivity.class

 

 

package com.example.intentdemo;

import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.KeyEvent;
import android.view.Menu;
import android.widget.TextView;

public class IntentSerializableActivity extends Activity {
	private ArrayList mArrayListStudent=new ArrayList();
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		TextView mTextView = new TextView(this);

		Student student = new Student();
		Bundle bundle = this.getIntent().getExtras();
		if (bundle != null) {
			student = (Student) bundle.getSerializable(student_key);
			mArrayListStudent = (ArrayList) bundle.getSerializable(student_Arry);
		}

		mTextView.append(You name is:  + student.getName() + 

				+ You age is:  + student.getEage() + 
 + 
);

		for (int i = 0; i < mArrayListStudent.size(); i++) {
			Student studentArrayList = new Student();
			studentArrayList = mArrayListStudent.get(i);
			mTextView.append(You name is:  + studentArrayList.getName() + 

					+ You age is:  + studentArrayList.getEage() + 
);
		}

		setContentView(mTextView);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
	// 返回按鍵調用
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			Intent intent = new Intent();
			Bundle mBundle = new Bundle();  
		    mBundle.putSerializable(student_Arry, mArrayListStudent);   //傳遞一個user對象列表
		    intent.putExtras(mBundle);  	
		    setResult(RESULT_OK, intent);
			finish();
		}
		return true;
	}

}

xml布局我就不貼了,需要源碼可以下載。

 

其實看一下代碼,都是能看明白。也很容易實踐,用過一遍之後就好很多了。

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved