Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7661341
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:35:07+00:00 2026-05-31T13:35:07+00:00

I am working on a shopping cart assignment for that I have created a

  • 0

I am working on a shopping cart assignment for that I have created a custom ListView that contains the Picture, Price and Add to Cart button in each row.
I also have a textview at the top which increments everytime the user presses the Add to cart.
The Button in every row is perfectly working fine but I don’t know how to increment the counter and set it to the textview as whenever I do this under onClick event I get runtime error.
I am sharing with you the code.

package uet.cs08.bll;

import java.util.ArrayList;

import uet.cs08.android.R;
import uet.cs08.android.mainActivity2;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class itemBaseAdapter extends BaseAdapter  {

 public int count = 5;
 private ArrayList<ItemInfo> itemList;
 private LayoutInflater mInflater;

 public itemBaseAdapter(Context context, ArrayList<ItemInfo> itemList){
     this.itemList=itemList;
     mInflater= LayoutInflater.from(context);

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

}

@Override
public Object getItem(int idx) {
     return itemList.get(idx);
}

@Override
public long getItemId(int idx) {
    // TODO Auto-generated method stub
    return idx;
}

@Override
public View getView(int idx,  View convertView, ViewGroup parent) {
    final ViewHolder holder;
    if(convertView==null){

        convertView=mInflater.inflate(R.layout.item_list_row, null);
        holder = new ViewHolder();
        holder.txtItemName=(TextView)  convertView.findViewById(R.id.itemName);
        holder.txtPic=(ImageView) convertView.findViewById(R.id.pic);
        holder.txtPrice=(TextView) convertView.findViewById(R.id.price);
        holder.counter = (TextView) convertView.findViewById(R.id.tvCount);
        Button btn = (Button) convertView.findViewById(R.id.Cart);
        final TextView tv = (TextView)convertView.findViewById(R.id.tvCount);


        btn.setOnClickListener(new View.OnClickListener() {
        @Override
            public void onClick(View v) {

                 count = Integer.parseInt((String)tv.getText());
                 count++;
                 tv.setText("" + count);


        }
        });
            //tv.setText(""+count);         
            convertView.setTag(holder);
    }else
    {
           holder = (ViewHolder) convertView.getTag();
    }

    holder.txtItemName.setText(itemList.get(idx).itemName);
    holder.txtPic.setImageResource(itemList.get(idx).pic);
    holder.txtPrice.setText(""+itemList.get(idx).price);
    holder.counter.setText(itemList.get(idx).count);

    return convertView;
}


class ViewHolder {
      TextView txtItemName;
      ImageView txtPic;
      TextView txtPrice;
      TextView counter;
     }

}
`
Here is the logCat.

03-20 16:16:38.853: D/AndroidRuntime(2759): Shutting down VM
03-20 16:16:38.853: W/dalvikvm(2759): threadid=1: thread exiting with uncaught exception (group=0x40015560)
03-20 16:16:38.883: E/AndroidRuntime(2759): FATAL EXCEPTION: main
03-20 16:16:38.883: E/AndroidRuntime(2759): java.lang.NullPointerException
03-20 16:16:38.883: E/AndroidRuntime(2759):     at uet.cs08.bll.itemBaseAdapter$1.onClick(itemBaseAdapter.java:61)
03-20 16:16:38.883: E/AndroidRuntime(2759):     at android.view.View.performClick(View.java:2485)
03-20 16:16:38.883: E/AndroidRuntime(2759):     at android.view.View$PerformClick.run(View.java:9080)
03-20 16:16:38.883: E/AndroidRuntime(2759):     at android.os.Handler.handleCallback(Handler.java:587)
03-20 16:16:38.883: E/AndroidRuntime(2759):     at android.os.Handler.dispatchMessage(Handler.java:92)
03-20 16:16:38.883: E/AndroidRuntime(2759):     at android.os.Looper.loop(Looper.java:123)
03-20 16:16:38.883: E/AndroidRuntime(2759):     at android.app.ActivityThread.main(ActivityThread.java:3683)
03-20 16:16:38.883: E/AndroidRuntime(2759):     at java.lang.reflect.Method.invokeNative(Native Method)
03-20 16:16:38.883: E/AndroidRuntime(2759):     at java.lang.reflect.Method.invoke(Method.java:507)
03-20 16:16:38.883: E/AndroidRuntime(2759):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-20 16:16:38.883: E/AndroidRuntime(2759):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-20 16:16:38.883: E/AndroidRuntime(2759):     at dalvik.system.NativeStart.main(Native Method)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T13:35:08+00:00Added an answer on May 31, 2026 at 1:35 pm

    Make sure you have reference to your TextView in xml.

    Connect it (using findViewById()) inside onCreate() of Activity only, and you may pass it to your Adapter (i.e making a class field and setter getter method), if Adapter is a separate class.

    and dont do this inside getView() of Adapter, remove this line.

    final TextView tv  = (TextView)convertView.findViewById(R.id.tvCount);
    

    you will pass your TextView from Activity to Adapter using constructor or setter getter method, will keep a reference using class Adapter's class level variable and the what you will do inside onClick() of Button is:

    btn.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
              count++;
             yourClassLevelTextView.setText(String.valueOf(count));
         }});
    

    EDIT:

    how you will pass TextView to Adapter

    public class YourAdapter extends BlahBlahBlah
    {
         private TextView yourClassLevelTextView;
         public void setTextView(TextView textViewFromActivity)
         {
             this.yourClassLevelTextView = textViewFromActivity;
         }
    }
    

    and in Activity do something like this:

    YourAdapter adapter = new YourAdapter(blah, blah, blah);
    adapter.setTextView(yourTextViewInActivity);
    listView.setAdapter(adapter);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been working on a shopping cart that the user can add/remove order
I'm working on an app that has a shopping cart. When I add a
Im working with a shopping cart plugin and that uses a basic form on
I'm working on a shopping cart in a website and I have my items(which
I'm working on a very basic shopping cart system. I have a table items
I have a shopping cart page where my 'Pay By Credit Card' button fires
I have a form that submit a shopping cart to Google Checkout. The form
I have a working shopping cart system in which items are added to the
For a site with shopping cart we have https working on the domain.com/shop-2/cart location.
I'm working on a simple custom shopping cart (yes, reinventing the wheel) in Rails.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.