Click here to Skip to main content
15,892,575 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to pass class object between two activities in my android application, but I always get null on the other activity.

here is my code :

To pass data :

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());
        }
    }



To get data :

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



my parcable class:

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);

       }
   }



code to prepare adapter for 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);


Adapter Class

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;
    }
}




Am I doing anything wrong ?
Posted
Updated 1-Jul-14 5:42am
v2

1 solution

Are you sure that you have problem in p?
Products p = (Products) getIntent().getParcelableExtra(EXTRA_MESSAGE_DATA); 

Why are trying to get same detail again, while you getting mCurrentProduct?
Products mCurrentProduct = (Products) mBundleData.getParcelable(EXTRA_MESSAGE_DATA);
 
Share this answer
 
Comments
rjrohit2303 1-Jul-14 11:41am    
Yes, I have problem in "p". Edited the post.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900