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