Android - 从ArrayList中删除项目

时间:2014-03-04 05:32:16

标签: java android android-listview arraylist android-arrayadapter

我有一个ArrayList,里面有6个值。当我单击一个按钮时,我删除了其中一个值,然后将新值添加到ArrayList。一切都很完美但是当我到达ArrayList中的最后一个原始项目并尝试删除它时,应用程序崩溃了。这是一个例子,

我的ArrayList包含这些变量"1", "2", "3","4", "5", "6"当我按下一个按钮时,删除了ArrayList的第一项,然后添加了另一个项。所以,它看起来像这样,"2", "3","4", "5", "6", "New Value"我一直这样做,直到我到达ArrayList中的最后一个原始项目。它看起来像这样,"6", "New Value","New Value", "New Value", "New Value", "New Value"当我点击按钮删除项目"6"时,应用程序崩溃了。

这是我的ArrayList代码,

String[] values = new String[] { "1", "2", "3",
                "4", "5", "6" };  
        final ArrayList<String> list = new ArrayList<String>();
        for (int i = 0; i < values.length; ++i) {
          list.add(values[i]);
        }
        final StableArrayAdapter adapter = new StableArrayAdapter(this,
            R.layout.main_activity_list_item, list);
        listview.setAdapter(adapter);

这是我用来删除和添加新ArrayList项的代码,

Button.setOnTouchListener(new OnTouchListener(){

            public boolean onTouch(View v, MotionEvent event) {
                switch(event.getAction())
                {
                case MotionEvent.ACTION_DOWN :

                    break;
                case MotionEvent.ACTION_UP :
                    list.add("New Value");
                    list.remove(0);
                    adapter.notifyDataSetChanged();
                    break;
                }
                return false;
            }

        });

这是StableArrayAdapter,

private class StableArrayAdapter extends ArrayAdapter<String> {

        HashMap<String, Integer> mIdMap = new HashMap<String, Integer>();

        public StableArrayAdapter(Context context, int textViewResourceId,
            List<String> objects) {
          super(context, textViewResourceId, objects);
          for (int i = 0; i < objects.size(); ++i) {
            mIdMap.put(objects.get(i), i);
          }
        }

        @Override
        public long getItemId(int position) {
          String item = getItem(position);
          return mIdMap.get(item);
        }

        @Override
        public boolean hasStableIds() {
          return true;
        }

      }

这就是CatLog,

03-04 00:28:01.586: E/AndroidRuntime(17775): java.lang.NullPointerException
03-04 00:28:01.586: E/AndroidRuntime(17775):    at com.example.LoginActivity$StableArrayAdapter.getItemId(LoginActivity.java:183)
03-04 00:28:01.586: E/AndroidRuntime(17775):    at android.widget.AdapterView.rememberSyncState(AdapterView.java:1169)
03-04 00:28:01.586: E/AndroidRuntime(17775):    at android.widget.AdapterView$AdapterDataSetObserver.onChanged(AdapterView.java:798)
03-04 00:28:01.586: E/AndroidRuntime(17775):    at android.widget.AbsListView$AdapterDataSetObserver.onChanged(AbsListView.java:5630)
03-04 00:28:01.586: E/AndroidRuntime(17775):    at android.database.DataSetObservable.notifyChanged(DataSetObservable.java:35)
03-04 00:28:01.586: E/AndroidRuntime(17775):    at android.widget.BaseAdapter.notifyDataSetChanged(BaseAdapter.java:50)
03-04 00:28:01.586: E/AndroidRuntime(17775):    at android.widget.ArrayAdapter.notifyDataSetChanged(ArrayAdapter.java:286)
03-04 00:28:01.586: E/AndroidRuntime(17775):    at com.example.LoginActivity$2.onTouch(LoginActivity.java:157)
03-04 00:28:01.586: E/AndroidRuntime(17775):    at android.view.View.dispatchTouchEvent(View.java:5574)
03-04 00:28:01.586: E/AndroidRuntime(17775):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1965)
03-04 00:28:01.586: E/AndroidRuntime(17775):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1734)
03-04 00:28:01.586: E/AndroidRuntime(17775):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1965)
03-04 00:28:01.586: E/AndroidRuntime(17775):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1734)
03-04 00:28:01.586: E/AndroidRuntime(17775):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1965)
03-04 00:28:01.586: E/AndroidRuntime(17775):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1734)
03-04 00:28:01.586: E/AndroidRuntime(17775):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1965)
03-04 00:28:01.586: E/AndroidRuntime(17775):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1734)
03-04 00:28:01.586: E/AndroidRuntime(17775):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1965)
03-04 00:28:01.586: E/AndroidRuntime(17775):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1734)
03-04 00:28:01.586: E/AndroidRuntime(17775):    at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1982)
03-04 00:28:01.586: E/AndroidRuntime(17775):    at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1441)
03-04 00:28:01.586: E/AndroidRuntime(17775):    at android.app.Activity.dispatchTouchEvent(Activity.java:2421)
03-04 00:28:01.586: E/AndroidRuntime(17775):    at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1930)
03-04 00:28:01.586: E/AndroidRuntime(17775):    at android.view.View.dispatchPointerEvent(View.java:5759)
03-04 00:28:01.586: E/AndroidRuntime(17775):    at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:2970)
03-04 00:28:01.586: E/AndroidRuntime(17775):    at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2529)
03-04 00:28:01.586: E/AndroidRuntime(17775):    at android.view.ViewRootImpl.processInputEvents(ViewRootImpl.java:869)
03-04 00:28:01.586: E/AndroidRuntime(17775):    at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2538)
03-04 00:28:01.586: E/AndroidRuntime(17775):    at android.os.Handler.dispatchMessage(Handler.java:99)
03-04 00:28:01.586: E/AndroidRuntime(17775):    at android.os.Looper.loop(Looper.java:154)
03-04 00:28:01.586: E/AndroidRuntime(17775):    at android.app.ActivityThread.main(ActivityThread.java:4945)
03-04 00:28:01.586: E/AndroidRuntime(17775):    at java.lang.reflect.Method.invokeNative(Native Method)
03-04 00:28:01.586: E/AndroidRuntime(17775):    at java.lang.reflect.Method.invoke(Method.java:511)
03-04 00:28:01.586: E/AndroidRuntime(17775):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
03-04 00:28:01.586: E/AndroidRuntime(17775):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
03-04 00:28:01.586: E/AndroidRuntime(17775):    at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:1)

您的代码是..

    @Override
    public long getItemId(int position) {
      String item = getItem(position);
      return mIdMap.get(item);
    }

你会做这样的改变..

    @Override
    public long getItemId(int position) {       
      return position;
    }

获取项目ID意味着它将返回一个位置这就是你将使用这样的自定义适配器它对你有用....

public class CustomAdapter extends ArrayAdapter<String> {

public ArrayList<String> mlist;
public Context context;
public LayoutInflater inflater;

public CustomAdapter(Context context, int resource, ArrayList<String> mlist) {
    super(context, resource);
    this.mlist = mlist;
    this.context = context;
    inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getPosition(Sample item) {
    return super.getPosition(item);
}

@Override
public Sample getItem(int position) {
    return mlist.get(position);
}

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

@Override
public long getItemId(int position) {
    return super.getItemId(position);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    convertView = inflater.inflate(R.layout.listitem, null);
    TextView text1 = (TextView) convertView.findViewById(R.id.item1);       
    text1.setText(mlist.get(position).getListitem1());      
    return convertView;
}

}

答案 1 :(得分:0)

您还需要覆盖getItem适配器中的StableArrayAdapter

@Override
public String getItem(int position) {
    return objects.get(position);
}

因为默认情况下getItem方法返回null

编辑:

要在地图中添加元素,请预先增加i而不是发布为:

  for (int i = 0; i < objects.size(); i++) {
    mIdMap.put(objects.get(i), i);
  }