重置ListView项目中NumberPicker的值

时间:2013-01-21 18:07:20

标签: android nullpointerexception android-arrayadapter numberpicker

我有ListView个活动,对列表中的每个项目都有一个NumberPicker视图。用户可以使用此选择器添加/删除项目。然而,我的问题是,当我恢复ListView活动时,当我希望将它们设置为0时,值仍然是所选的值。

我有什么方法可以做到这一点? 到目前为止,我在MenuItemArrayAdapter类中完成了以下操作,我添加了一个方法来重置数字选择器。

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);
}

}

然后我在ListView活动中调用此方法:

    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);              
        }
    }

但是我收到了这个错误:

FATAL EXCEPTION:com.example.waitron5.MenuItemArrayAdapter.resetNumberPicker上的主要java.lang.NullPointerException(MenuItemArrayAdapter.java:104)

这是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)

错误发生在第104行:

np.setValue(0);

我意识到这可能是因为我没有得到NumberPicker np的引用。但是,我如何修复我的代码,以便这不会返回NPE?

1 个答案:

答案 0 :(得分:0)

您不能(并且不应该)连接到getView()方法中创建的行视图(您也不需要适配器中的静态NumberPicker字段) 。由于您始终在NumberPicker方法中将0的值设置为getView(),因此您可以尝试在notifyDataSetChanged()方法中调用resetNumberPicker()