Note that there are some explanatory texts on larger screens.

plurals
  1. POResetting value of NumberPicker inside ListView item
    primarykey
    data
    text
    <p>I have a <code>ListView</code> activity that has a <code>NumberPicker</code> 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 <code>ListView</code> activity the values are still at the chosen values when I would like them to be set to 0. </p> <p>Is there any way I can do this? So far I have done the following inside my <code>MenuItemArrayAdapter</code> class I add a method to reset the number picker. </p> <pre><code>public class MenuItemArrayAdapter extends ArrayAdapter&lt;MenuItem&gt;{ private List&lt;MenuItem&gt; menuItems; private List&lt;MenuItem&gt; order; private static NumberPicker np; private TextView price; private double amount = 0.0; private double total = 0.0; //Constructor public MenuItemArrayAdapter(Context context, List&lt;MenuItem&gt; menuItems, TextView price) { super(context, R.layout.menuitem_row, menuItems); this.menuItems = menuItems; this.price = price; order = new ArrayList&lt;MenuItem&gt;(); } 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 &gt; 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 &lt; 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&lt;MenuItem&gt; getOrder() { return order; } public void resetNumberPicker(){ np.setValue(0); } </code></pre> <p>}</p> <p>I then call this method in the ListView activity:</p> <pre><code> 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); } } </code></pre> <p>but i get this error:</p> <p><strong>FATAL EXCEPTION: main java.lang.NullPointerException at com.example.waitron5.MenuItemArrayAdapter.resetNumberPicker(MenuItemArrayAdapter.java:104)</strong></p> <p>this is the logcat:</p> <pre><code>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) </code></pre> <p>The error is occuring at this line 104:</p> <pre><code>np.setValue(0); </code></pre> <p>I realise that it is perhaps because I am not getting a reference to the <code>NumberPicker</code> np. But How can I fix my code so that this will not return an NPE? </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload