I am trying to show list of Items along with GrandTotal Amount, but whenever i use this code in my code getting NullPointerException, see below that code:>
(1) There is one way to pass value from Adapter to Activity on which adapter is set,
i.e we write listview.setadapter(xyzadapter); in MainActivity, and we want to pass value from xyzadapter to MainActivity, then only one way I know, make one interface, define one method in that with parameters for passing value, and then implement it on adapter class,
(2) If we want to pass values from adapter to another activity in which it is not set, then we can use putExtra method to pass value,
Let me know if you have any issue...
Edited: for (1) answer
make one interface in your main package:
public interface DataTransferInterface {
public void setValues(ArrayList> al);
}
in your adapter class make object of Interface:
below this line public class CartAdapter extends BaseAdapter { and before constructor:
DataTransferInterface dtInterface;
in your construction pass this interface
in CartAdapter use this constructor:
public CartAdapter(Activity a, DataTransferInterface dtInterface) {
// TODO Auto-generated constructor stub
activity = a;
this.dtInterface = dtInterface;
inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
and use dtInterface.setValues(your Values to pass to Activity)
Now in your CartActivity.java
implement that interface like:
public class CartActivity extends Activity implements DataTransferInterface {
and change
mViewCartAdpt = new CartAdapter(CartActivity.this);
to
mViewCartAdpt = new CartAdapter(CartActivity.this, this);
now you will see red line below CartActivity (just move your mouse cursor on CartActivity) that shows add unimplemented methods, click on that will override setValues method
@Override
public void setValues(ArrayList> al) {
// TODO Auto-generated method stub
}
you can use Any type of data to pass instead of ArrayList
Let me know if you have any ussue: