2011-05-02 131 views
3

我已经尝试了许多不同的解决方案,这里发现了很多不同的解决方案和许多其他网站 - 但我不能让它工作!在活动之间传递自定义对象

我有一个对象“公寓”在另一项活动中使用的活动。 活动从类MyMapOverlayItem开始,并应该启动ApartmentInfoActivity,其中应使用Apartment对象。

MyMapOverlayItem.java:

package com.android.madsoft.spotter; 

import java.util.ArrayList; 

import android.content.Context; 
import android.content.Intent; 
import android.graphics.drawable.Drawable; 
import android.os.Bundle; 

import com.google.android.maps.ItemizedOverlay; 
import com.google.android.maps.OverlayItem; 

public class MyMapOverlayIcon extends ItemizedOverlay<OverlayItem> { 
    Context mContext; 
    ArrayList<Apartment> myList; 
    private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>(); 
    public MyMapOverlayIcon(Drawable defaultMarker) { 
     super(defaultMarker); 

    } 

    public MyMapOverlayIcon(Drawable defaultMarker, Context context, ArrayList<Apartment> myList) { 
     super(boundCenterBottom(defaultMarker)); 
      mContext = context; 
      this.myList = myList; 

     } 

    public void addOverlay(OverlayItem overlay) { 
     mOverlays.add(overlay); 
     populate(); 
    } 

    @Override 
    protected boolean onTap(int index) { 
     Apartment ap = myList.get(index); 
     ApartmentParcel ap1 = new ApartmentParcel(ap); 
     Intent i = new Intent(mContext, ApartmentInfoActivity.class); 
     Bundle b = new Bundle(); 
     b.putParcelable("app", ap1); 
     i.putExtras(b); 
//  i.putExtra("app",ap1); 
//  b.putSerializable("apartment", ap); 

     // WHAT TO DO!? 

     mContext.startActivity(i); 

     return true; 
    } 

    @Override 
    protected OverlayItem createItem(int i) { 
     return mOverlays.get(i); 
    } 

    @Override 
    public int size() { 
     return mOverlays.size(); 
    } 

} 

Apartment.java:

package com.android.madsoft.spotter; 

import java.io.IOException; 
import java.io.InputStream; 
import java.net.HttpURLConnection; 
import java.net.MalformedURLException; 
import java.net.URL; 
import java.util.ArrayList; 

import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 


public class Apartment { 


    private static final long serialVersionUID = 1L; 
    int latitude, longitude; 
    String id, 
    zipcode, 
    size, 
    rooms, 
    rentprice, 
    deposit, 
    takeoverdate, 
    rentperiod, 
    furniturized, 
    usagecost, 
    addeddate, 
    description, 
    prepaid; 
    ArrayList<String> imageList, phonenumbers; 
    int imagecount = 1; 
    ArrayList<Bitmap> bigBitmapList; 
    ArrayList<Bitmap> smallBitmapList; 

    public Apartment(String id, int latitude, int longitude, 
        String zipcode, String size, String rooms, 
        String rentprice, String deposit, String prepaid, String takeoverdate, 
        String rentperiod, String furniturized, String usagecost, 
        String addeddate, String description, ArrayList<String> imageList, ArrayList<String> phonenumbers){ 
     bigBitmapList = new ArrayList<Bitmap>(); 
     smallBitmapList = new ArrayList<Bitmap>(); 
     this.addeddate = addeddate; 
     this.deposit = deposit; 
     this.description = description; 
     this.furniturized = furniturized; 
     this.id = id; 
     this.imageList = imageList; 
     this.latitude = latitude; 
     this.longitude = longitude; 
     this.phonenumbers = phonenumbers; 
     this.prepaid = prepaid; 
     this.rentperiod = rentperiod; 
     this.rentprice = rentprice; 
     this.rooms = rooms; 
     this.size = size; 
     this.takeoverdate = takeoverdate; 
     this.usagecost = usagecost; 
     this.zipcode = zipcode; 

     for (String s :imageList){ 

      URL myFileUrl = null; 
      try { 
       myFileUrl= new URL(s); 
      } catch (MalformedURLException e) { 
       e.printStackTrace(); 
      } 
      try { 
       HttpURLConnection conn= (HttpURLConnection)myFileUrl.openConnection(); 
       conn.setDoInput(true); 
       conn.connect(); 
       InputStream is = conn.getInputStream(); 
       Bitmap image = BitmapFactory.decodeStream(is); //Saves the current icon 
       bigBitmapList.add(image); 
       //Icon will now be resized to 125px*100px    
        Bitmap resized; 
        int scalerate = (int)image.getHeight()/60; 
        int newHeight = (int)image.getHeight()/scalerate; 
        int newWidth = (int)image.getWidth()/scalerate; 
        resized = Bitmap.createScaledBitmap(image, newWidth, newHeight, false); 
        smallBitmapList.add(resized); 
      } 
      catch (IOException e) { 
       e.printStackTrace(); 
      } 
      } 
     } 

ApartmentParcel.java:

package com.android.madsoft.spotter; 

import java.io.IOException; 
import java.io.InputStream; 
import java.net.HttpURLConnection; 
import java.net.MalformedURLException; 
import java.net.URL; 
import java.util.ArrayList; 

import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.os.Parcel; 
import android.os.Parcelable; 

public class ApartmentParcel implements Parcelable { 

    private static final long serialVersionUID = 1L; 
    int latitude, longitude; 
    String id, 
    zipcode, 
    size, 
    rooms, 
    rentprice, 
    deposit, 
    takeoverdate, 
    rentperiod, 
    furniturized, 
    usagecost, 
    addeddate, 
    description, 
    prepaid; 
    ArrayList<String> imageList, phonenumbers; 
    int imagecount = 1; 
    ArrayList<Bitmap> bigBitmapList; 
    ArrayList<Bitmap> smallBitmapList; 

    public ApartmentParcel(String id, int latitude, int longitude, 
        String zipcode, String size, String rooms, 
        String rentprice, String deposit, String prepaid, String takeoverdate, 
        String rentperiod, String furniturized, String usagecost, 
        String addeddate, String description, ArrayList<String> imageList, ArrayList<String> phonenumbers){ 
     bigBitmapList = new ArrayList<Bitmap>(); 
     smallBitmapList = new ArrayList<Bitmap>(); 
     this.addeddate = addeddate; 
     this.deposit = deposit; 
     this.description = description; 
     this.furniturized = furniturized; 
     this.id = id; 
     this.imageList = imageList; 
     this.latitude = latitude; 
     this.longitude = longitude; 
     this.phonenumbers = phonenumbers; 
     this.prepaid = prepaid; 
     this.rentperiod = rentperiod; 
     this.rentprice = rentprice; 
     this.rooms = rooms; 
     this.size = size; 
     this.takeoverdate = takeoverdate; 
     this.usagecost = usagecost; 
     this.zipcode = zipcode; 

     for (String s :imageList){ 

      URL myFileUrl = null; 
      try { 
       myFileUrl= new URL(s); 
      } catch (MalformedURLException e) { 
       e.printStackTrace(); 
      } 
      try { 
       HttpURLConnection conn= (HttpURLConnection)myFileUrl.openConnection(); 
       conn.setDoInput(true); 
       conn.connect(); 
       InputStream is = conn.getInputStream(); 
       Bitmap image = BitmapFactory.decodeStream(is); //Saves the current icon 
       bigBitmapList.add(image); 
       //Icon will now be resized to 125px*100px    
        Bitmap resized; 
        int scalerate = (int)image.getHeight()/60; 
        int newHeight = (int)image.getHeight()/scalerate; 
        int newWidth = (int)image.getWidth()/scalerate; 
        resized = Bitmap.createScaledBitmap(image, newWidth, newHeight, false); 
        smallBitmapList.add(resized); 
      } 
      catch (IOException e) { 
       e.printStackTrace(); 
      } 
      } 
     } 

    public ApartmentParcel(Apartment ap){ 
     bigBitmapList = new ArrayList<Bitmap>(); 
     smallBitmapList = new ArrayList<Bitmap>(); 
     this.addeddate = ap.getAddeddate(); 
     this.deposit = ap.getDeposit(); 
     this.description = ap.getDescription(); 
     this.furniturized = ap.getFurniturized(); 
     this.id = ap.getId(); 
     this.imageList = ap.getImageList(); 
     this.latitude = ap.getLatitude(); 
     this.longitude = ap.getLongitude(); 
     this.phonenumbers = ap.getPhonenumbers(); 
     this.prepaid = ap.getPrepaid(); 
     this.rentperiod = ap.getRentperiod(); 
     this.rentprice = ap.getRentprice(); 
     this.rooms = ap.getRooms(); 
     this.size = ap.getSize(); 
     this.takeoverdate = ap.getTakeoverdate(); 
     this.usagecost = ap.getUsagecost(); 
     this.zipcode = ap.getZipcode(); 
     runMyParcel(); 
    } 

    private void runMyParcel(){ 
     for (String s :imageList){ 

      URL myFileUrl = null; 
      try { 
       myFileUrl= new URL(s); 
      } catch (MalformedURLException e) { 
       e.printStackTrace(); 
      } 
      try { 
       HttpURLConnection conn= (HttpURLConnection)myFileUrl.openConnection(); 
       conn.setDoInput(true); 
       conn.connect(); 
       InputStream is = conn.getInputStream(); 
       Bitmap image = BitmapFactory.decodeStream(is); //Saves the current icon 
       bigBitmapList.add(image); 
       //Icon will now be resized to 125px*100px    
        Bitmap resized; 
        int scalerate = (int)image.getHeight()/60; 
        int newHeight = (int)image.getHeight()/scalerate; 
        int newWidth = (int)image.getWidth()/scalerate; 
        resized = Bitmap.createScaledBitmap(image, newWidth, newHeight, false); 
        smallBitmapList.add(resized); 
      } 
      catch (IOException e) { 
       e.printStackTrace(); 
      } 
      } 
    } 

    public void setSmallBitmapList(ArrayList<Bitmap> smallBitmapList) { 
     this.smallBitmapList = smallBitmapList; 
    } 


    @Override 
    public int describeContents() { 
     // TODO Auto-generated method stub 
     return 0; 
    } 


    @Override 
    public void writeToParcel(Parcel out, int flags) { 
     out.writeString(id); 
     out.writeString(zipcode); 
     out.writeString(size); 
     out.writeString(rooms); 
     out.writeString(rentprice); 
     out.writeString(deposit); 
     out.writeString(takeoverdate); 
     out.writeString(rentperiod); 
     out.writeString(furniturized); 
     out.writeString(usagecost); 
     out.writeString(addeddate); 
     out.writeString(description); 
     out.writeString(prepaid); 
     out.writeStringList(phonenumbers); 
     out.writeList(bigBitmapList); 
     out.writeList(smallBitmapList); 

    } 

    private ApartmentParcel(Parcel in) { 
     id = in.readString(); 
     zipcode = in.readString(); 
     size = in.readString();  
     rooms = in.readString(); 
     rentprice = in.readString(); 
     deposit = in.readString(); 
     takeoverdate = in.readString(); 
     rentperiod = in.readString(); 
     furniturized = in.readString(); 
     usagecost = in.readString(); 
     addeddate = in.readString(); 
     description = in.readString(); 
     prepaid = in.readString(); 
     in.readStringList(phonenumbers); 
     in.readTypedList(bigBitmapList, Bitmap.CREATOR); 
     in.readTypedList(smallBitmapList, Bitmap.CREATOR); 
    } 

    public static final Creator<ApartmentParcel> CREATOR = new Creator<ApartmentParcel>() { 
     public ApartmentParcel createFromParcel(Parcel in) { 
      return new ApartmentParcel(in); 
     } 

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

ApartmentInfoActivity.java:

package com.android.madsoft.spotter; 

    import android.app.Activity; 
    import android.os.Bundle; 
    import android.widget.TextView; 

    public class ApartmentInfoActivity extends Activity { 


     @Override 
      protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.infoview); 
//  Apartment ap = (Apartment)getIntent().getSerializableExtra("apartment"); 
     Apartment ap = (Apartment)getIntent().getParcelableExtra("app"); 
     TextView zipView = (TextView)findViewById(R.id.txtInfoZip); 
     zipView.setText(ap.getZipcode()); 
    } 

    } 

回答

2

让您Aparment类实现Serializable

public class Apartment implements Serializable{ 

然后在MyMapOverlayIcon

Bundle b = new Bundle(); 
// WHAT TO DO!? 
b.putSerializable("apartment", ap); 

然后在ApartmentInfoActivity

Apartment ap = (Apartment) getIntent().getSerializableExtra("apartment"); 
+0

我刚试过。 – JReneM 2011-05-02 17:46:43

+0

在ApartmentInfoActivity中,ap为空 – JReneM 2011-05-02 17:48:42

+0

@cristian你刚刚救了我一天:)谢谢 – 2014-07-14 22:49:00

0

到活动之间共享对象的首选方法是使共享对象Parcelable http://developer.android.com/reference/android/os/Parcelable.html(另请参阅http://developer.android.com/reference/android/os/Parcelable.Creator.html)。然后你可以在某个地方定义一个String键,并使用它来在一个Bundle对象中放入和获取Parcelable物品。

在主要活动:

Intent newActivity = ...; 
Bundle extras = new Bundle(); 

// use your key here to put the parcelable into the bundle 

newActivity.putExtras(extras); 
startActivity(newActivity); 

而且在孩子活动

//child activity (onCreate()) 
Bundle intentExtras = getIntent().getExtras(); 
if(intentExtras != null){ 
    // use the key here to get your parcelable from the bundle 
} 

如果你在你的类的项目,正在实施Parcelable的集合,你可以处理它是这样的:

import java.util.ArrayList; 
import java.util.Collection; 

import android.graphics.Bitmap; 
import android.os.Parcel; 
import android.os.Parcelable; 

public class ExampleParcelable implements android.os.Parcelable { 

    private final String id; 
    private final ArrayList<Bitmap> bitmapList = new ArrayList<Bitmap>(); 

    public ExampleParcelable(String id, Collection<Bitmap> bitmaps){ 
     this.id = id; 
     this.bitmapList.addAll(bitmaps); 
    } 


    @Override 
    public void writeToParcel(Parcel out, int flags) { 
     // write stack to be duplicated in 'createFromParcel(Parcel in)' 
     out.writeString(id); 
     out.writeList(bitmapList); 
    } 

    @Override 
    public int describeContents() { 
     // your own logic here 
     return 0; 
    } 

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

     @Override 
     public ExampleParcelable createFromParcel(Parcel in) { 
      String id = in.readString(); 
      @SuppressWarnings("unchecked") 
      ArrayList<Bitmap> bitmaps = (ArrayList<Bitmap>)in.readArrayList(Bitmap.class.getClassLoader()); 
      return new ExampleParcelable(id, bitmaps); 
     } 

     @Override 
     public ExampleParcelable[] newArray(int size) { 
      return new ExampleParcelable[size]; 
     } 
    }; 
} 
+0

我已经试过了,但是无法让它正常工作。是否因为Apartment对象中存在类ArrayList ? – JReneM 2011-05-02 18:07:44

+0

如果需要,您可以获取位图的URI并传递该位置,但可以在Parcelable中包含列表。 – 2011-05-02 18:11:38

相关问题