Rana Umerfarooq
Rana Umerfarooq

Reputation: 71

How to get all values from text view of custom list view

I want to get all Values of Text View from list view. how i can do it. i have a list view and put a array list in the list view. my list view is a custom list view. i want to get all data from price text view and then i want to show total of the price . and i do some code but give some error
It's my code.

public class SelectedProductFromShopingCartShow extends AppCompatActivity {

    ArrayList<ShowProducts> arrayList = new ArrayList<>();
    String condition = "SelectedItemsFromShoppingCart";
    CustomAdapter customAdapter;
    ListView listView;
    TextView tvTotal;
    int total;
    private static final String TAG = "SelectedProductFromShop";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_selected_product_from_shoping_cart_show);
        listView = (ListView) findViewById(R.id.listViewSelectedItemsOfShopingCart);
        tvTotal = (TextView) findViewById(R.id.tvTotal);

        arrayList = (ArrayList<ShowProducts>) getIntent().getSerializableExtra("selectedList");

        customAdapter = new CustomAdapter(SelectedProductFromShopingCartShow.this, R.layout.show_selected_item_of_shopingcart, condition, arrayList);
        listView.setAdapter(customAdapter);
        total = getTotal(listView);
        tvTotal.setText(String.valueOf(total).toString());
    }

    public int getTotal(ListView listView) {
        int sum = 0;
        for (int i = 0; i < listView.getCount(); i++) {
            Log.d(TAG, "getTotal: "+listView.getCount());
            Log.d(TAG, "getTotal: "+arrayList.size());
            View view = listView.getChildAt(i);
            TextView tvprice = (TextView) view.findViewById(R.id.tvSetPriceOfSeletedItem);
            TextView tvquantity = (TextView) view.findViewById(R.id.tvQunatitySetOfSelectedItem);

            int price = Integer.parseInt(tvprice.getText().toString());
            int quantity = Integer.parseInt(tvquantity.getText().toString());
            sum = sum + price * quantity;
        }
        return sum;

    }
}

And it's my error. i don't know how i can do it. please help me.

07-26 18:03:49.404 21155-21155/com.sizdom.sizdomstockmanager E/AndroidRuntime: FATAL EXCEPTION: main
                                                                               java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sizdom.sizdomstockmanager/com.sizdom.sizdomstockmanager.SelectedProductFromShopingCartShow}: java.lang.NullPointerException
                                                                                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2204)
                                                                                   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2254)
                                                                                   at android.app.ActivityThread.access$600(ActivityThread.java:141)
                                                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
                                                                                   at android.os.Handler.dispatchMessage(Handler.java:99)
                                                                                   at android.os.Looper.loop(Looper.java:137)
                                                                                   at android.app.ActivityThread.main(ActivityThread.java:5069)
                                                                                   at java.lang.reflect.Method.invokeNative(Native Method)
                                                                                   at java.lang.reflect.Method.invoke(Method.java:511)
                                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
                                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
                                                                                   at dalvik.system.NativeStart.main(Native Method)
                                                                                Caused by: java.lang.NullPointerException
                                                                                   at com.sizdom.sizdomstockmanager.SelectedProductFromShopingCartShow.getTotal(SelectedProductFromShopingCartShow.java:88)
                                                                                   at com.sizdom.sizdomstockmanager.SelectedProductFromShopingCartShow.onCreate(SelectedProductFromShopingCartShow.java:38)
                                                                                   at android.app.Activity.performCreate(Activity.java:5104)
                                                                                   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1092)
                                                                                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
                                                                                   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2254) 
                                                                                   at android.app.ActivityThread.access$600(ActivityThread.java:141) 
                                                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 
                                                                                   at android.os.Handler.dispatchMessage(Handler.java:99) 
                                                                                   at android.os.Looper.loop(Looper.java:137) 
                                                                                   at android.app.ActivityThread.main(ActivityThread.java:5069) 
                                                                                   at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                                   at java.lang.reflect.Method.invoke(Method.java:511) 
                                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
                                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
                                                                                   at dalvik.system.NativeStart.main(Native Method) 

I check out multiple sites for its solutions. but i don't find any soluotion

Custom Adapter

public class CustomAdapter extends ArrayAdapter {
    private List<Integer> list;
    private List<ShowProducts> listShowProducts;
    private Context context;
    private int resource;
    private String condition;
    String uri;
    private static final String TAG = "CustomAdapter";
    int i = 0;

    public CustomAdapter(@NonNull Context context, @LayoutRes int resource, List<ShowProducts> objects) {
        super(context, resource, objects);
        this.context = context;
        this.resource = resource;
        this.listShowProducts = objects;
    }

    @Override
    public int getCount() {
        return super.getCount();
    }

    @Nullable
    @Override
    public Object getItem(int position) {
        return super.getItem(position);
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

        View view = LayoutInflater.from(context).inflate(resource, parent, false);   
            final ShowProducts showProducts = listShowProducts.get(position);

            ImageView imageView = (ImageView) view.findViewById(R.id.imageViewOfSelecteditem);
            ImageView plus = (ImageView) view.findViewById(R.id.imageviewPlus);
            ImageView minus = (ImageView) view.findViewById(R.id.imageviewminus);
            TextView tvSetNameOfSeletedItem = (TextView) view.findViewById(R.id.tvSetNameOfSeletedItem);
            TextView tvSetSizeOfSeletedItem = (TextView) view.findViewById(R.id.tvSetSizeOfSeletedItem);
            TextView tvSetPriceOfSeletedItem = (TextView) view.findViewById(R.id.tvSetPriceOfSeletedItem);
            final TextView tvQunatitySetOfSelectedItem = (TextView) view.findViewById(R.id.tvQunatitySetOfSelectedItem);
           tvSetNameOfSeletedItem.setText(showProducts.getProduct_name().toString());
            tvSetSizeOfSeletedItem.setText(showProducts.getSize_name());
            tvSetPriceOfSeletedItem.setText(String.valueOf(showProducts.getSize_price()).toString());
            uri = showProducts.getProduct_photo().toString();

            Picasso.with(context).load(uri).into(imageView);

            plus.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int a=Integer.parseInt(tvQunatitySetOfSelectedItem.getText().toString());
                    a++;
                    if (a<=showProducts.getSize_quantity())
                    tvQunatitySetOfSelectedItem.setText(String.valueOf(a).toString());
                }
            });

            minus.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int a=Integer.parseInt(tvQunatitySetOfSelectedItem.getText().toString());
                    a--;
                    if (a>0)
                    tvQunatitySetOfSelectedItem.setText(String.valueOf(a).toString());
                }
            });
        }
        return view;
    }
}

enter image description here

it's my list view it show the value when i get value but look an arror

I debug the app

 public int getTotal(ListView listView) {
        int sum = 0;
        for (int i = 0; i < listView.getCount(); i++) { // i=0;
            Log.d(TAG, "getTotal: "+listView.getCount()); 
            Log.d(TAG, "getTotal: "+arrayList.size()); // arraylist=4;
            View view = listView.getChildAt(i); //view=null;
            TextView tvprice = (TextView) view.findViewById(R.id.tvSetPriceOfSeletedItem);
            Log.d(TAG, "getTotal: "+tvprice.getText());
            TextView tvquantity = (TextView) view.findViewById(R.id.tvQunatitySetOfSelectedItem);

Upvotes: 0

Views: 2310

Answers (3)

phatnhse
phatnhse

Reputation: 3940

The ListView loads data from your ArrayList<ShowProducts> arrayList

If your view updates, please update your model inside arrayList too.

Then you want to get the total value of all products, just make it done by calculating through your arrayList. Don't touch View

First, make sure your model contains 2 field: quantity and price. Second, define 2 methods inside your Adapter

@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

    View view = LayoutInflater.from(context).inflate(resource, parent, false);
    ShowProducts showProducts = listShowProducts.get(position);
    // Handle your views...

    plus.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int quantity = Integer.valueOf(tvQunatitySetOfSelectedItem.getText().toString());
            quantity++;
            if (quantity <= showProducts.getSize_quantity()) {
                tvQunatitySetOfSelectedItem.setText(String.valueOf(quantity));
                showProducts.setQuantity(quantity); // If your model doesn't have this field, create it
            }
        }
    });

    minus.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int quantity = Integer.valueOf(tvQunatitySetOfSelectedItem.getText().toString());
            quantity--;
            if (quantity > 0) {
                tvQunatitySetOfSelectedItem.setText(String.valueOf(quantity));
                showProducts.setQuantity(quantity); // If your model doesn't have this field, create it
            }
        }
    });

    return view;
}

public float getTotal() { // You can use int, long or double ... It's up to you
    float result = 0;
    for (ShowProducts showProducts : listShowProducts) {
        result += showProducts.getPrice() * showProducts.getQuantity();
    }
    return result;
}

Then if you want to calculate total price -> simply call yourAdapter.getTotal()

Upvotes: 1

Tavinder Singh
Tavinder Singh

Reputation: 420

Since you didn't added any values inyour arraylist that's why you are getting nullpointer exception First call a function like below to add elements in your arraylist. Then you will be able to get the total price.

    private void addProducts() {
        ShowProducts products = new ShowProducts();
        products.price = 100;
        products.quantity = 10;
        arrayList.add(products);
        }

@Override
    protected void onCreate(Bundle savedInstanceState) {
// your code
addProducts();
//yourcode
}

You can use below function to get the total sum

private int total() {
int sum = 0;
for(ShowProducts product: arraylist) {
sum += product.price;
}

Upvotes: 1

Aditi
Aditi

Reputation: 389

You are not setting any price in tvprice and quantity in tvquantity textView. Thus a null value.

Upvotes: 1

Related Questions