2012-03-08 83 views
2

我是新的android.and我需要一些幫助 我有ArrayList這是一個對象實現Parciable .and我想要傳遞這個ArrayList到另一個活動。 這是我NewsEntity.javaandroid傳遞對象包含bitMap到另一個活動

公共類NewsEntity實現Parcelable { /** * * / 私營位圖位圖; 私有字符串標題; 私人字符串簡要; 私人字符串細節; 私人字符串記者; private String ImageFile; 私人字符串日期; private String readCount; private String shareCount; public int addRead_count; public int addShare_count;

private static ByteBuffer dst; 
private static byte[] bytesar; 
public NewsEntity() { 
    bitmap=null; 
    title=""; 
    brief=""; 
    details=""; 
    reporter=""; 
    ImageFile=""; 
    date=""; 
    readCount=""; 
    shareCount=""; 
    addRead_count=0; 
    addShare_count=0; 
} 

public void setTitle(String title) { 
    this.title = title; 
} 
public String getTitle() { 
    return title; 
} 
public void setBrief(String brief) { 
    this.brief = brief; 
} 
public String getBrief() { 
    return brief; 
} 
public void setDetails(String details) { 
    this.details = details; 
} 
public String getDetails() { 
    return details; 
} 
public void setReporter(String reporter) { 
    this.reporter = reporter; 
} 
public String getReporter() { 
    return reporter; 
} 

public void setImageFile(String imageFile) { 
    ImageFile = imageFile; 
} 
public String getImageFile() { 
    return ImageFile; 
} 
public void setDate(String date) { 
    this.date = date; 
} 
public String getDate() { 
    return date; 
} 
public void setReadCount(String readCount) { 
    this.readCount = readCount; 
} 
public String getReadCount() { 
    return readCount; 
} 
public void setShareCount(String shareCount) { 
    this.shareCount = shareCount; 
} 
public String getShareCount() { 
    return shareCount; 
} 

public Bitmap getBitmap() { 
     return bitmap; 
} 

public void setBitmap(Bitmap bitmap) { 
     this.bitmap = bitmap; 
} 

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

    @Override 
    public void writeToParcel(Parcel out, int flags) { 
     try{ 
      out.writeString(title); 
     out.writeString(brief); 
     out.writeString(details); 
     out.writeString(reporter); 
     out.writeString(readCount); 
     out.writeString(shareCount); 
     out.writeInt(addRead_count); 
     out.writeInt(addShare_count); 
     out.writeString(date); 
     out.writeString(ImageFile); 

    } 

    public static final Parcelable.Creator<NewsEntity> CREATOR = new Parcelable.Creator<NewsEntity>() { 
     //public class MyCreator implements Parcelable.Creator<MyProduct> {  
      public NewsEntity createFromParcel(Parcel in) { 
        return new NewsEntity(); 
       } 

       public NewsEntity[] newArray(int size) { 
        return new NewsEntity[size]; 
       } 
      }; 
      private NewsEntity(Parcel in) { 
       try{ 
        title=(String) in.readString(); 
       brief=(String) in.readString(); 
       details=(String) in.readString(); 
       date=(String) in.readString(); 
       reporter=(String) in.readString(); 
       readCount=(String) in.readString(); 
       shareCount=(String) in.readString(); 
       ImageFile=(String) in.readString(); 
       addRead_count=(Integer) in.readInt(); 
       addShare_count=(Integer) in.readInt(); 
       bitmap=in.readParcelable(Bitmap.class.getClassLoader()); 

       } 
       catch(Exception e){} 
       } 

}

和這條線,其通過ArrayList的意圖 newsDetailsIntent.putExtra( 「顯示」,result_news); startActivity(newsDetailsIntent);

我需要help.how做

在此先感謝

+0

這個問題問了很多次以活動之間的傳遞位圖.....嘗試自己更好 – 2012-03-08 14:22:31

+0

可能的重複[如何將位圖對象從1個活動傳遞給另一個](http://stackoverflow.com/questions/2459524/how-can-i-pass-a- bitmap-object-from-1-activity-to-another) – 2012-03-08 14:22:48

+0

我已經閱讀了很多文章,但sti我不會再嘗試。謝謝samir – 2012-03-08 14:31:19

回答

2

使用你的類像這樣(位圖):

package br.problema.domain; 

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

public class Product implements Parcelable { 
    private long id; 
    private String name; 
    private String tag; 
    private Bitmap image; 


    public Product(long id, String name, String tag, Bitmap image) { 
     super(); 
     this.id = id; 
     this.name = name; 
     this.tag = tag; 
     this.image = image; 
    } 
    public Product(Parcel in){ 
     id = in.readLong(); 
     name = in.readString(); 
     tag = in.readString(); 
     image = (Bitmap) in.readValue(Bitmap.class.getClassLoader()); 
    } 


    public long getId() { 
     return id; 
    } 
    public void setId(long id) { 
     this.id = id; 
    } 


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


    public String getTag() { 
     return tag; 
    } 
    public void setTag(String tag) { 
     this.tag = tag; 
    } 


    public Bitmap getImage() { 
     return image; 
    } 
    public void setImage(Bitmap image) { 
     this.image = image; 
    } 


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


    @Override 
    public void writeToParcel(Parcel dest, int flags) { 
     dest.writeLong(id); 
     dest.writeString(name); 
     dest.writeString(tag); 
     dest.writeValue(image); 
    } 


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

     public Product[] newArray (int size){ 
      return new Product[size]; 
     } 
    }; 
} 
+1

優秀,謝謝! – Belial09 2016-06-22 12:22:52

相關問題