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

  • Home
  • SEARCH
  • 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 9167617
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:26:05+00:00 2026-06-17T15:26:05+00:00

I have a ListView activity that has a NumberPicker view on each item in

  • 0

I have a ListView activity that has a NumberPicker view on each item in the list. The user can use this picker to add/remove items. It is working however the issue I have is that when I resume the ListView activity the values are still at the chosen values when I would like them to be set to 0.

Is there any way I can do this?
So far I have done the following inside my MenuItemArrayAdapter class I add a method to reset the number picker.

public class MenuItemArrayAdapter extends ArrayAdapter<MenuItem>{

private List<MenuItem> menuItems;
private List<MenuItem> order;
private static NumberPicker np;
private TextView price;
private double amount = 0.0;
private double total = 0.0;

//Constructor
public MenuItemArrayAdapter(Context context, List<MenuItem> menuItems, TextView price) {
    super(context, R.layout.menuitem_row, menuItems);
    this.menuItems = menuItems;
    this.price = price;
    order = new ArrayList<MenuItem>();

}

public MenuItemArrayAdapter(Context context){
    super(context, R.layout.menuitem_row);
}

//get views
public View getView(final int position, View convertView, ViewGroup parent){
    View v = convertView;


    if(v == null){
        LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = vi.inflate(R.layout.menuitem_row, null);
        v.setFocusable(true);
    }

    //assign values to view
    final MenuItem item = this.menuItems.get(position);

    TextView nameView = (TextView) v.findViewById(R.id.item_name);
    final TextView priceView = (TextView) v.findViewById(R.id.item_price);

    nameView.setText(item.getName() + " ");
    priceView.setText("€"+ String.valueOf(item.getPrice()));

    //number picker
    np = (NumberPicker)v.findViewById(R.id.numpick);
    np.setMaxValue(20);
    np.setMinValue(0);
    np.setValue(0);
    np.setFocusable(false);
    //calculation occurs when values are changed...
    np.setOnValueChangedListener( new OnValueChangeListener() {
          public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
                Toast.makeText(picker.getContext(), "dish: " + item.getName() + " amount: " + picker.getValue(), Toast.LENGTH_SHORT).show();
                Toast.makeText(picker.getContext(), "new Value: " + newVal + " old Value: " + oldVal, Toast.LENGTH_SHORT).show();

               // amount += item.getPrice() * picker.getValue();

                if(newVal > oldVal){
                    total = (item.getPrice() * newVal) - item.getPrice() * oldVal;
                    //add to order
                    order.add(item);
                    amount += total;
                    Toast.makeText(picker.getContext(), "ADDED TO ORDER: " + item.getName(), Toast.LENGTH_SHORT).show();

                }

                if(newVal < oldVal){
                    total = (item.getPrice() * oldVal) - item.getPrice() * newVal;
                    //remove from order
                    order.remove(item);
                    amount -= total;
                    Toast.makeText(picker.getContext(), "REMOVED FROM ORDER: " + item.getName(), Toast.LENGTH_SHORT).show();

                }

                price.setText("€" + String.valueOf(amount));
            }
          });

    return v;

}

public List<MenuItem> getOrder() {
    return order;
}

public void resetNumberPicker(){
    np.setValue(0);
}

}

I then call this method in the ListView activity:

    protected void onPostExecute(Boolean result) {

        super.onPostExecute(result);
        if(result){
            pDialog.dismiss();
            adapter = new MenuItemArrayAdapter(StartersActivity.this, starters, price);
            adapter.resetNumberPicker();
            StartersActivity.this.setListAdapter(adapter);              
        }
    }

but i get this error:

FATAL EXCEPTION: main java.lang.NullPointerException at com.example.waitron5.MenuItemArrayAdapter.resetNumberPicker(MenuItemArrayAdapter.java:104)

this is the logcat:

FATAL EXCEPTION: main
01-21 18:04:40.053: E/AndroidRuntime(18291): java.lang.NullPointerException
01-21 18:04:40.053: E/AndroidRuntime(18291):    at com.example.waitron5.MenuItemArrayAdapter.resetNumberPicker(MenuItemArrayAdapter.java:104)
01-21 18:04:40.053: E/AndroidRuntime(18291):    at com.example.waitron5.StartersActivity$HTTPTask.onPostExecute(StartersActivity.java:215)
01-21 18:04:40.053: E/AndroidRuntime(18291):    at com.example.waitron5.StartersActivity$HTTPTask.onPostExecute(StartersActivity.java:1)
01-21 18:04:40.053: E/AndroidRuntime(18291):    at android.os.AsyncTask.finish(AsyncTask.java:631)
01-21 18:04:40.053: E/AndroidRuntime(18291):    at android.os.AsyncTask.access$600(AsyncTask.java:177)
01-21 18:04:40.053: E/AndroidRuntime(18291):    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
01-21 18:04:40.053: E/AndroidRuntime(18291):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-21 18:04:40.053: E/AndroidRuntime(18291):    at android.os.Looper.loop(Looper.java:137)
01-21 18:04:40.053: E/AndroidRuntime(18291):    at android.app.ActivityThread.main(ActivityThread.java:5039)
01-21 18:04:40.053: E/AndroidRuntime(18291):    at java.lang.reflect.Method.invokeNative(Native Method)
01-21 18:04:40.053: E/AndroidRuntime(18291):    at java.lang.reflect.Method.invoke(Method.java:511)
01-21 18:04:40.053: E/AndroidRuntime(18291):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-21 18:04:40.053: E/AndroidRuntime(18291):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-21 18:04:40.053: E/AndroidRuntime(18291):    at dalvik.system.NativeStart.main(Native Method)

The error is occuring at this line 104:

np.setValue(0);

I realise that it is perhaps because I am not getting a reference to the NumberPicker np. But How can I fix my code so that this will not return an NPE?

  • 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-06-17T15:26:06+00:00Added an answer on June 17, 2026 at 3:26 pm

    You can’t(and you shouldn’t) connect to the row views created in the getView() method like you did(you also don’t need that static NumberPicker field in the adapter). As you always set the value of the NumberPicker to 0 in the getView() method you could try simply calling notifyDataSetChanged() in the resetNumberPicker() method.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an activity that displays a list of restaurant dishes. Each view of
I have a ListView in which each List item has a Spinner . I
I have a Tab activity that has 3 tabs. Each tab is a ListView.
I have one activity A, that has one button and one list view which
I have a ListView in my activity. Each row includes three TextViews in this
I have an activity that has a checkbox, then under it is a list
I have an activity with ListView that has: android:theme=@android:style/Theme.Dialog in Manifest. When I open
I have a listview that has datasource getting from Network ( quite big list).
I have a ListView with items that will navigate on click. The list item
How can a have a ListView that has different amount of rows? Instead of

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.