Parcelable传个Map的简单应用_移动开发_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > 移动开发 > Parcelable传个Map的简单应用

Parcelable传个Map的简单应用

 2010/12/15 8:01:04  hellorheaven  http://hellorheaven.javaeye.com  我要评论(0)
  • 摘要:最简单的,主要是实现Parcelable的接口,重写那个CREATOR测试用的接收信息Activitypackagecom.nico;importandroid.app.Activity;importandroid.content.Intent;importandroid.os.Bundle;importandroid.os.Parcelable;publicclassTestextendsActivity{/**Calledwhentheactivityisfirstcreated
  • 标签:Map 应用

最简单的,主要是实现Parcelable的接口,重写那个CREATOR

?

测试用的接收信息Activity

package com.nico;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;

public class Test extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Intent i = getIntent();
        Person p = i.getParcelableExtra("yes");
        System.out.println("---->"+p.name);
        System.out.println("---->"+p.map.size());
    }
}

?发送的Activity

package com.nico;

import java.util.HashMap;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

public class TestNew extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Intent intent = new Intent();
        Person p = new Person();
        p.map = new HashMap<String,String>();
        p.map.put("yes", "ido");
        p.name="ok";
        intent.putExtra("yes", p);
        intent.setClass(this, Test.class);
        startActivity(intent);
    }
}

?Parcelable的实现类

package com.nico;

import java.util.HashMap;

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

public class Person implements Parcelable {

	public HashMap<String,String> map = new HashMap<String,String> ();
	
	public String name ;
	@Override
	public int describeContents() {
		return 0;
	}

	@Override
	public void writeToParcel(Parcel dest, int flags) {

		dest.writeMap(map);
		dest.writeString(name);
	}
	public static final Parcelable.Creator<Person> CREATOR = new Parcelable.Creator<Person>() {

		@Override
		public Person createFromParcel(Parcel source) {
			Person p = new Person();
			p.map=source.readHashMap(HashMap.class.getClassLoader());
			p.name=source.readString();
			return p;
		}

		@Override
		public Person[] newArray(int size) {
			// TODO Auto-generated method stub
			return null;
		}
	};

}

?

发表评论
用户名: 匿名