Reputation: 107
I am trying to transfer the data of an ArrayList
to another Activity
to display some of its content there in a new ArrayList
. I will be filtering the data to be displayed in the next Activity
. The content of the ArrayList
is from mysql database using json.
The line - productList = new ArrayList<myProduct>();
- causes my arraylist to go empty because it creates a new object. I dont know if im doing the proper way of coding but I hope you can provide me some of your idea.
Here are my codes:
Menu.java
public class Menu extends AppCompatActivity implements AdapterView.OnItemClickListener {
public ArrayList<myProduct> productList;
Button viewOrder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
String product = "http://10.0.2.2/myDB/product.php";
StringRequest stringRequestProduct = new StringRequest(Request.Method.GET, product, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d(TAG, response);
productList = new JsonConverter<myProduct>().toArrayList(response, myProduct.class);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(), "Error retrieving data", Toast.LENGTH_SHORT).show();
}
});
MySingleton.getInstance(getApplicationContext()).addToRequestQueue(stringRequestProduct);
viewOrder = (Button)findViewById(R.id.viewOrder);
viewOrder.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(Menu.this, Order.class);
startActivity(i);
}
});
}
}
myProduct.class
import com.google.gson.annotations.SerializedName;
public class myProduct {
@SerializedName("productID")
public int productID;
@SerializedName("categoryID")
public int categoryID;
@SerializedName("productName")
public String productName;
@SerializedName("productPrice")
public int productPrice;
public int productQuantity = 0;
public myProduct(int productID, int categoryID, String productName, int productPrice){
this.productID = productID;
this.categoryID = categoryID;
this.productName = productName;
this.productPrice = productPrice;
}
public int getProductID(){
return productID;
}
public int getCategoryID(){
return categoryID;
}
public String getProductName(){
return productName;
}
public int getProductPrice(){
return productPrice;
}
}
Order.Java
public class Order extends AppCompatActivity {
public ListView orderListView;
public myOrderAdapter orderAdapter;
public ArrayList<myProduct> productList, filter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_order);
orderListView = (ListView)findViewById(R.id.orderList);
productList = new ArrayList<myProduct>();
filter = new ArrayList<myProduct>();
for(myProduct item : productList){
//Condition before adding
if(item.productQuantity > 0) {
Log.d(TAG,item.getProductName());
filter.add(item);
}
}
orderAdapter = new myOrderAdapter(getApplicationContext(),filter);
orderListView.setAdapter(orderAdapter);
}
}
Upvotes: 1
Views: 635
Reputation: 4023
To achieve this , you need to implement Parcelable
(Note that, you can also achieve this by Serializable
but Parcelable is much faster than Serializable , and it is highly recommended for android )
You have to make your myProduct
class Parcelable
, you can implement in manually or there is a android studio plugin which make your model class parcelable . You can take a look at this post , to implement parcelable .
After implementing Parcelable
, in your Menu
class do something like this
viewOrder.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(Menu.this, Order.class);
Bundle bundle = new Bundle();
bundle.putParcelableArrayList("productList",productList);
intent.putExtras(bundle);
startActivity(i);
}
});
And in your Order
class , receive this in following way .
ArrayList<myProduct> productList=
(ArayList<myProduct>)bundle.getParcelableArrayList("productList");
EDIT Your myProduct class should be look like this , after implementing parcelable
public class myProduct implements Parcelable {
public int productID;
public int categoryID;
public String productName;
public int productPrice;
public int productQuantity = 0;
public myProduct(int productID, int categoryID, String productName, int productPrice){
this.productID = productID;
this.categoryID = categoryID;
this.productName = productName;
this.productPrice = productPrice;
}
public int getProductID(){
return productID;
}
public int getCategoryID(){
return categoryID;
}
public String getProductName(){
return productName;
}
public int getProductPrice(){
return productPrice;
}
protected myProduct(Parcel in) {
productID = in.readInt();
categoryID = in.readInt();
productName = in.readString();
productPrice = in.readInt();
productQuantity = in.readInt();
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(productID);
dest.writeInt(categoryID);
dest.writeString(productName);
dest.writeInt(productPrice);
dest.writeInt(productQuantity);
}
@SuppressWarnings("unused")
public static final Parcelable.Creator<myProduct> CREATOR = new Parcelable.Creator<myProduct>() {
@Override
public myProduct createFromParcel(Parcel in) {
return new myProduct(in);
}
@Override
public myProduct[] newArray(int size) {
return new myProduct[size];
}
};
}
Hope this helps. Let me know if it works.
Upvotes: 1
Reputation: 1405
implement the Serializable to your model class
import java.io.Serializable;
public class myProduct implements Serializable{
@SerializedName("productID")
public int productID;
@SerializedName("categoryID")
public int categoryID;
@SerializedName("productName")
public String productName;
@SerializedName("productPrice")
public int productPrice;
public int productQuantity = 0;
public myProduct(int productID, int categoryID, String productName, int productPrice){
this.productID = productID;
this.categoryID = categoryID;
this.productName = productName;
this.productPrice = productPrice;
}
public int getProductID(){
return productID;
}
public int getCategoryID(){
return categoryID;
}
public String getProductName(){
return productName;
}
public int getProductPrice(){
return productPrice;
}
}
and pass it in this way
Bundle bundle = new Bundle();
bundle.putSerializable("productList", productList);
intent.putExtras(bundle);
and get it as-
Intent intent = this.getIntent();
Bundle bundle = intent.getExtras();
List<myProduct> productList=
(List<myProduct>)bundle.getSerializable("productList");
Upvotes: 0
Reputation: 118
After creating your intent object add your json string as
intent.putExtra("list",jsonString);
And then start the other activity
At your other activity grab the extras by
String json = getIntent().getExtras().getString("list");
And then you can parse the json and use your list.
Upvotes: 0