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:
Their is one way that I used, I have defined a static array in my Class and access that array from the adapter class. Hence whenever their is a value change in Adapter my list values are reflected in the Class itself.
(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:
The problem with that line of code is that you have there quiet many calls where one function returns null. You should split it up like this:
SomeType1 detail = Constants.sItem_Detail;
SomeType2 something = detail.get(i);
String strValue = something.get(com.example.sample.CartAdapter.KEY_TOTAL);
mGTotal += Double.parseDouble(strValue);
So you will find faster your problem. By the way you should also check in each line if the value is null.
Please note also that longVar = longVar + longValue
can be simplified to longVar += longValue
.
There is one more way.
You can pass the viewID
or view object
in your constructor and set the updated value from the adapter.
It will work 100%. It was working for me.
Use shared preferance to get data from adapter to activity