2014-06-27 111 views
0

我想在我的android應用程序中的兩個活動之間傳遞類對象,但我總是在其他活動上得到空值。intent.getExtra()返回空對象

這裏是我的代碼:

來傳遞數據:

public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { 

    try { 
     Products mCurrentProduct = (Products) adapterView.getAdapter().getItem(i); 
     Intent mProductDescription = new Intent(getBaseContext(), Activity_ProductDescription.class); 
     Bundle mBundle = new Bundle(); 
     mBundle.putParcelable(GlobalStrings.EXTRA_MESSAGE_DATA, mCurrentProduct); 
     mProductDescription.putExtras(mBundle); 

     if (mProductDescription != null) 
      startActivity(mProductDescription); 
    } 
    catch (Exception e) 
    { 
     Log.d("Selection erro :",e.getMessage()); 
    } 
} 

獲取數據:

Intent mIntent = getIntent(); 
     Bundle mBundleData = mIntent.getExtras(); 
     Products mCurrentProduct = (Products) mBundleData.getParcelable(EXTRA_MESSAGE_DATA); 
     Products p = (Products) getIntent().getParcelableExtra(EXTRA_MESSAGE_DATA); // p is always null here. 

我parcable類:

public class Products implements Parcelable { 

    public String productName; 
    public double productPrice; 
    public String productSize; 
    public String productWeight; 
    public String productDescription; 
    public String brandName; 
    public byte[] productImage; 
    public String seller; 

    public Products(String productName, double productPrice, String productSize, String productWeight, 
        String productDescription, String brandName, byte[] productImage, String seller) { 

     this.productName = productName; 
     this.productPrice = productPrice; 
     this.productSize = productSize; 
     this.productWeight = productWeight; 
     this.productDescription = productDescription; 
     this.brandName = brandName; 
     this.productImage = productImage; 
     this.seller = seller; 
    } 

    public String getProductName() { 
     return productName; 
    } 

    public double getProductPrice() { 
     return productPrice; 
    } 

    public String getProductSize() { 
     return productSize; 
    } 

    public String getProductWeight() { 
     return productWeight; 
    } 

    public String getProductDescription() { 
     return productDescription; 
    } 

    public String getBrandName() { 
     return brandName; 
    } 

    public byte[] getProductImage() { 
     return productImage; 
    } 

    public String getSeller() { 
     return seller; 
    } 

    private Products(Parcel p) { 
     this.productName = p.readString(); 
     this.productPrice = p.readDouble(); 
     this.productSize = p.readString(); 
     this.productWeight = p.readString(); 
     this.productDescription = p.readString(); 
     this.brandName = p.readString(); 
     p.readByteArray(productImage); 
     this.seller = p.readString(); 
    } 
    public static final Creator<Products> CREATOR = new Creator<Products>() { 

     @Override 
     public Products createFromParcel(Parcel parcel) { 
      return new Products(parcel); 
     } 

     @Override 
     public Products[] newArray(int i) { 
      return new Products[i]; 
     } 
    }; 
    @Override 
    public int describeContents() { 
     return 0; 
    } 

    @Override 
    public void writeToParcel(Parcel parcel, int i) { 
     parcel.writeString(productName); 
     parcel.writeString(productSize); 
     parcel.writeString(productWeight); 
     parcel.writeString(productDescription); 
     parcel.writeString(brandName); 
     parcel.writeString(seller); 
     parcel.writeByteArray(productImage); 
     parcel.writeDouble(productPrice); 

    } 
} 

編寫代碼適配器爲listview:

JSONObject jsonObject = new JSONObject(s); 
JSONArray jsonArray = jsonObject.getJSONArray("GetProductsResult"); 

for (int i=0; i < jsonArray.length();i++ ) 
{ 
JSONObject jsonObjectArr = jsonArray.getJSONObject(i); 

productsList.add(new Products(
     jsonObjectArr.getString("ProductName"), 
     jsonObjectArr.getDouble("ProductPrice"), 
     jsonObjectArr.getString("ProductSize"), 
     jsonObjectArr.getString("ProductWeight"), 
     jsonObjectArr.getString("ProductDescription"), 
     jsonObjectArr.getString("BrandName"), 
     jsonObjectArr.getString("ProductImage").getBytes(), 
     jsonObjectArr.getString("Seller"))); 

} 

ArrayAdapter<Products> adapter = new ProductListItemAdapters(this,R.layout.list_products,productsList); 
mListViewProductList = (ListView) findViewById(R.id.listViewProductList); 
mListViewProductList.setAdapter(adapter); 
mListViewProductList.setOnItemClickListener(this); 

適配器類別

public class ProductListItemAdapters extends ArrayAdapter<Products> { 

Context mainContext; 
List<Products> productList; 
int resourceId; 

public ProductListItemAdapters(Context context, int resource, List<Products> objects) { 
    super(context, resource, objects); 
    mainContext = context; 
    productList = objects; 
    resourceId = resource; 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 

    View itemView = convertView; 
     try { 

      if (itemView == null) { 

       LayoutInflater inflater = (LayoutInflater) mainContext 
         .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
       itemView = inflater.inflate(R.layout.list_products, parent, false); 
      } 

      Products prod = productList.get(position); 

      TextView txtViewSeller = (TextView) itemView.findViewById(R.id.textView_productSeller); 
      txtViewSeller.setText(prod.getSeller()); 

      TextView txtViewBrand = (TextView) itemView.findViewById(R.id.textView_productBrand); 
      txtViewBrand.setText(prod.getBrandName()); 

      TextView txtViewProduct = (TextView) itemView.findViewById(R.id.textView_productName); 
      txtViewProduct.setText(prod.getProductName()); 

      TextView txtViewDesc = (TextView) itemView.findViewById(R.id.textView_productDesc); 
      txtViewDesc.setText(prod.getProductDescription()); 

      TextView txtViewPrice = (TextView) itemView.findViewById(R.id.textView_productPrice); 
      txtViewPrice.setText(String.valueOf(prod.getProductPrice())); 

     } 
     catch(Exception e) { 
      Log.d("err", e.toString());} 

    return itemView; 
} 

}

難道我做錯了什麼?

+0

分享您的適配器代碼在這裏 –

+0

http://stackoverflow.com/a/24305177/ 1318946 –

+0

使用Serializable代替..它的一個簡單的方法.. – Nepster

回答

1

根據Android推薦,我會始終建議使用Parcelable

ParcelParcelable很快,但其文檔說明您不能將其用於通用序列化存儲,因爲實施會因不同版本的Android而有所不同(即OS更新可能會破壞依賴於該應用程序的應用程序)

簡單工具Serializable接口。它簡單易用。 類似:

public class Products implements Serializable{ 
    //write all your getter/setter methods here 
} 

而且在你的意圖的簡單方法putExtra()發送的對象,並把它作爲

getIntent().getSerializableExtra("Key"); 
+0

爲什麼你在Android中教他一個不好的方法? –

1

不是回答你的實際問題,而是你犯了錯誤。閱讀時,寫入宗地的字段序列必須相同。

// You followed this sequesnce for writting 
parcel.writeString(productName); 
    parcel.writeString(productSize); 
    parcel.writeString(productWeight); 
    parcel.writeString(productDescription); 
    parcel.writeString(brandName); 
    parcel.writeString(seller); 
    parcel.writeByteArray(productImage); 
    parcel.writeDouble(productPrice); 

但是在讀

this.productName = p.readString(); 
    this.productPrice = p.readDouble(); 
    this.productSize = p.readString(); 

序列不匹配。

+0

Pankaj是正確的..這是非常重要的。 parcelables必須按照它們將被讀取的確切順序編寫。 – erik

+0

'this.productName = p.readString(); this.productSize = p.readString(); 這個。productWeight = p.readString(); this.productDescription = p.readString(); this.brandName = p.readString(); this.seller = p.readString(); p.readByteArray(productImage); this.productPrice = p.readDouble();' 我改變了現在這樣的序列,但仍然沒有運氣。 – Rohit

+0

是的現在這是正確的。如果您的問題沒有解決,請分享您的適配器代碼。 –