1

I am creating ListView with two elements in each row (one for open second for deletion of row). I have my customized ListView in main activity created like this:

ArrayList<String> names = Devices.getInstance().getNameList();

final StableArrayAdapter adapter = new StableArrayAdapter(this,listViewResourceId,names);
final ListView listview = (ListView) findViewById(R.id.listView1);
listview.setAdapter(adapter);

My own ArrayAdapter:

private class StableArrayAdapter extends ArrayAdapter<String> {

    HashMap<String, Integer> mIdMap = new HashMap<String, Integer>();
    private final Activity mContext;

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

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
      View view = null;
      if (convertView == null) {
        LayoutInflater inflator = mContext.getLayoutInflater();
        view = inflator.inflate(R.layout.row, null);
        final ViewHolder viewHolder = new ViewHolder();
        viewHolder.text = (TextView) view.findViewById(R.id.label);
        viewHolder.text.setText(getItem(position));
        viewHolder.button = (ImageButton) view.findViewById(R.id.del);
        viewHolder.button.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO dodanie zapytania o usuniecie
                StableArrayAdapter.this.mIdMap.remove(getItem(position));
                StableArrayAdapter.this.notifyDataSetChanged();
                //((BaseAdapter) ((ListView)findViewById(R.id.listView1)).getAdapter()).notifyDataSetChanged(); 
            }

        });
        view.setTag(viewHolder);
      }

      return view;
    }

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

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

  }

Problem is that when I'm trying to delete row by using deletion button (its On click listener is above) there is NullPointerException on notifyDataSetChanged() line.

I tried making non anonymous class (for OnClickListener), or switching local fields from OnCreate to class fields but nothing worked - I suppose that I'm making some stupid thing but for now I'm blind and can't see anything in this code.

Thanks in advance for help!!!

StackTrace:

07-02 10:24:26.604: E/AndroidRuntime(2109): FATAL EXCEPTION: main
07-02 10:24:26.604: E/AndroidRuntime(2109): java.lang.NullPointerException
07-02 10:24:26.604: E/AndroidRuntime(2109):     at android.widget.AbsListView.obtainView(AbsListView.java:2253)
07-02 10:24:26.604: E/AndroidRuntime(2109):     at android.widget.ListView.makeAndAddView(ListView.java:1769)
07-02 10:24:26.604: E/AndroidRuntime(2109):     at android.widget.ListView.fillSpecific(ListView.java:1318)
07-02 10:24:26.604: E/AndroidRuntime(2109):     at android.widget.ListView.layoutChildren(ListView.java:1600)
07-02 10:24:26.604: E/AndroidRuntime(2109):     at android.widget.AbsListView.onLayout(AbsListView.java:2102)
07-02 10:24:26.604: E/AndroidRuntime(2109):     at android.view.View.layout(View.java:13754)
07-02 10:24:26.604: E/AndroidRuntime(2109):     at android.view.ViewGroup.layout(ViewGroup.java:4362)
07-02 10:24:26.604: E/AndroidRuntime(2109):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1649)
07-02 10:24:26.604: E/AndroidRuntime(2109):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1507)
07-02 10:24:26.604: E/AndroidRuntime(2109):     at android.widget.LinearLayout.onLayout(LinearLayout.java:1420)
07-02 10:24:26.604: E/AndroidRuntime(2109):     at android.view.View.layout(View.java:13754)
07-02 10:24:26.604: E/AndroidRuntime(2109):     at android.view.ViewGroup.layout(ViewGroup.java:4362)
07-02 10:24:26.604: E/AndroidRuntime(2109):     at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
07-02 10:24:26.604: E/AndroidRuntime(2109):     at android.view.View.layout(View.java:13754)
07-02 10:24:26.604: E/AndroidRuntime(2109):     at android.view.ViewGroup.layout(ViewGroup.java:4362)
07-02 10:24:26.604: E/AndroidRuntime(2109):     at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
07-02 10:24:26.604: E/AndroidRuntime(2109):     at android.view.View.layout(View.java:13754)
07-02 10:24:26.604: E/AndroidRuntime(2109):     at android.view.ViewGroup.layout(ViewGroup.java:4362)
07-02 10:24:26.604: E/AndroidRuntime(2109):     at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
07-02 10:24:26.604: E/AndroidRuntime(2109):     at android.view.View.layout(View.java:13754)
07-02 10:24:26.604: E/AndroidRuntime(2109):     at android.view.ViewGroup.layout(ViewGroup.java:4362)
07-02 10:24:26.604: E/AndroidRuntime(2109):     at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
07-02 10:24:26.604: E/AndroidRuntime(2109):     at android.view.View.layout(View.java:13754)
07-02 10:24:26.604: E/AndroidRuntime(2109):     at android.view.ViewGroup.layout(ViewGroup.java:4362)
07-02 10:24:26.604: E/AndroidRuntime(2109):     at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1866)
07-02 10:24:26.604: E/AndroidRuntime(2109):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1687)
07-02 10:24:26.604: E/AndroidRuntime(2109):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:998)
07-02 10:24:26.604: E/AndroidRuntime(2109):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4212)
07-02 10:24:26.604: E/AndroidRuntime(2109):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
07-02 10:24:26.604: E/AndroidRuntime(2109):     at android.view.Choreographer.doCallbacks(Choreographer.java:555)
07-02 10:24:26.604: E/AndroidRuntime(2109):     at android.view.Choreographer.doFrame(Choreographer.java:525)
07-02 10:24:26.604: E/AndroidRuntime(2109):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
07-02 10:24:26.604: E/AndroidRuntime(2109):     at android.os.Handler.handleCallback(Handler.java:615)
07-02 10:24:26.604: E/AndroidRuntime(2109):     at android.os.Handler.dispatchMessage(Handler.java:92)
07-02 10:24:26.604: E/AndroidRuntime(2109):     at android.os.Looper.loop(Looper.java:137)
07-02 10:24:26.604: E/AndroidRuntime(2109):     at android.app.ActivityThread.main(ActivityThread.java:4745)
07-02 10:24:26.604: E/AndroidRuntime(2109):     at java.lang.reflect.Method.invokeNative(Native Method)
07-02 10:24:26.604: E/AndroidRuntime(2109):     at java.lang.reflect.Method.invoke(Method.java:511)
07-02 10:24:26.604: E/AndroidRuntime(2109):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
07-02 10:24:26.604: E/AndroidRuntime(2109):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
07-02 10:24:26.604: E/AndroidRuntime(2109):     at dalvik.system.NativeStart.main(Native Method)   
4

3 回答 3

1

您正在if (convertView == null) {}块内做所有事情。仅使用它来膨胀和存储视图到持有者。设置文本并单击此块之外的侦听器,以便它们适用于每个视图,无论是否重新循环。

例子:

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

    //--init if not re-cycled--
    if (convertView == null) {
        convertView = LayoutInflater.from(getContext()).inflate(R.layout.row, parent, false);
        convertView.setTag(new ViewHolder(
                (TextView) convertView.findViewById(R.id.label),
                (ImageButton) convertView.findViewById(R.id.del)
        ));
    }

    //--data to set--
    String item = getItem(position);

    View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            StableArrayAdapter.this.mIdMap.remove(getItem(position));
            StableArrayAdapter.this.notifyDataSetChanged();
        }
    };

    //--set data--
    ViewHolder holder = (ViewHolder) convertView.getTag();
    holder.text.setText(item);
    holder.button.setOnClickListener(listener);
    return convertView;
}

private static class ViewHolder{
    public final TextView text;
    public final Button button;

    private ViewHolder(TextView text, Button button) {
        this.text = text;
        this.button = button;
    }
}
于 2013-07-02T10:57:23.810 回答
1

尝试这个,

@Override
    public View getView(final int position, View convertView, ViewGroup parent) {
//      View view = null;         remove this line
      if (convertView == null) {
        LayoutInflater inflator = mContext.getLayoutInflater();
        view = inflator.inflate(R.layout.row, null);
      }
    final ViewHolder viewHolder = new ViewHolder();
    viewHolder.text = (TextView) convertView.findViewById(R.id.label);
    viewHolder.text.setText(getItem(position));
    viewHolder.button = (ImageButton) convertView.findViewById(R.id.del);
    viewHolder.button.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO dodanie zapytania o usuniecie
            StableArrayAdapter.this.mIdMap.remove(getItem(position));
            StableArrayAdapter.this.notifyDataSetChanged();
            //((BaseAdapter) ((ListView)findViewById(R.id.listView1)).getAdapter()).notifyDataSetChanged(); 
        }

    });
    view.setTag(viewHolder);
  }

  return view;
}
于 2013-07-02T11:51:10.577 回答
0

问题当然是愚蠢的,我尝试通过仅从容器中删除行来删除行,而没有正确通知整个类 StableArrayAdapter,而不是这个(在 OnClickListener 中):

StableArrayAdapter.this.mIdMap.remove(getItem(position));

我应该使用这个:

StableArrayAdapter.this.remove(getItem(position));
于 2013-07-02T13:29:36.170 回答