从android listview中删除项目时出现异常

时间:2013-07-25 09:13:50

标签: android listview exception

我创建了一个包含内容的列表视图,并为每一行使用了删除按钮。在这里,我尝试使用它的删除按钮从列表视图中删除项目。删除列表视图的最后一个元素时,抛出ArrayIndexOutOfBound异常。

我用谷歌搜索并找到了一些解决方案,如:

@Override
    public int getCount() {

        return lsit.size();
    }

Adapter.notifyDataSetChanged();

但没有一个解决方案不会解决我的问题。

这是我的代码:

public View getView( final int position, View convertView,final ViewGroup parent) {
        final ViewHolder holder;
        System.out.println("saved homes list..");
        System.out.println("Position: "+m_itemClickedPosition_savedHomes);
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.nearbyhomestext, null);
            holder = new ViewHolder();

            holder.homeTitle = (TextView) convertView.findViewById(R.id.text_title);
            holder.homePlan = (TextView) convertView.findViewById(R.id.text_plan);
            holder.deleteButton = (Button) convertView.findViewById(R.id.delete_btn);


            holder.activeDrawable = getActivity().getResources().getDrawable(R.drawable.gradient_bg_hover);
            holder.inActiveDrawable = getActivity().getResources().getDrawable(R.drawable.gradient_bg);
            holder.deleteButton.setTag(position);
            //holder.deleteButton.setVisibility(View.INVISIBLE);
            holder.deleteButton.setOnClickListener(
                    new Button.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            System.out.println("Clicked position:"+position);
                            System.out.println("ArraySize:"+m_savedHomesList.size());
                          //Integer index = (Integer) v.getTag();
                              m_savedHomesList.remove(position); 
                              m_savedHomesAdapter.notifyDataSetChanged();
                        }
                    }
                );
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }   


        return convertView;
    }

LogCatError:

    07-25 14:25:56.187: E/AndroidRuntime(5152): FATAL EXCEPTION: main
07-25 14:25:56.187: E/AndroidRuntime(5152): java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1
07-25 14:25:56.187: E/AndroidRuntime(5152):     at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:257)
07-25 14:25:56.187: E/AndroidRuntime(5152):     at java.util.ArrayList.remove(ArrayList.java:406)
07-25 14:25:56.187: E/AndroidRuntime(5152):     at com.cpt.realtor.tablet.fragments.HomeListFragment$SavedHomeBaseAdapter$1.onClick(HomeListFragment.java:399)
07-25 14:25:56.187: E/AndroidRuntime(5152):     at android.view.View.performClick(View.java:2485)
07-25 14:25:56.187: E/AndroidRuntime(5152):     at android.view.View$PerformClick.run(View.java:9080)
07-25 14:25:56.187: E/AndroidRuntime(5152):     at android.os.Handler.handleCallback(Handler.java:587)
07-25 14:25:56.187: E/AndroidRuntime(5152):     at android.os.Handler.dispatchMessage(Handler.java:92)
07-25 14:25:56.187: E/AndroidRuntime(5152):     at android.os.Looper.loop(Looper.java:123)
07-25 14:25:56.187: E/AndroidRuntime(5152):     at android.app.ActivityThread.main(ActivityThread.java:3687)
07-25 14:25:56.187: E/AndroidRuntime(5152):     at java.lang.reflect.Method.invokeNative(Native Method)
07-25 14:25:56.187: E/AndroidRuntime(5152):     at java.lang.reflect.Method.invoke(Method.java:507)
07-25 14:25:56.187: E/AndroidRuntime(5152):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
07-25 14:25:56.187: E/AndroidRuntime(5152):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
07-25 14:25:56.187: E/AndroidRuntime(5152):     at dalvik.system.NativeStart.main(Native Method)

需要帮助解决这个问题..

提前致谢..

3 个答案:

答案 0 :(得分:0)

尝试

public int getCount() {
    return m_savedHomesList.size();
}

您要从getView(..)函数中的m_savedHomesList中删除。所以适配器应该基于m_savedHomesList。 同样在getView方法中,只需使用

notifyDataSetChanged();

而不是m_savedHomesAdapter.notifyDataSetChanged();

答案 1 :(得分:0)

ArrayIndexOutOfBounds答案是将听众放在return convertView;& notifyDataSetChanged();你必须创建一个处理程序:

public View getView( final int position, View convertView,final ViewGroup parent) {
    final ViewHolder holder;
    System.out.println("saved homes list..");
    System.out.println("Position: "+m_itemClickedPosition_savedHomes);
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.nearbyhomestext, null);
        holder = new ViewHolder();

        holder.homeTitle = (TextView) convertView.findViewById(R.id.text_title);
        holder.homePlan = (TextView) convertView.findViewById(R.id.text_plan);
        holder.deleteButton = (Button) convertView.findViewById(R.id.delete_btn);


        holder.activeDrawable = getActivity().getResources().getDrawable(R.drawable.gradient_bg_hover);
        holder.inActiveDrawable = getActivity().getResources().getDrawable(R.drawable.gradient_bg);
        holder.deleteButton.setTag(position);
        //holder.deleteButton.setVisibility(View.INVISIBLE);

        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }   
    // Your listener here
    holder.deleteButton.setOnClickListener(
                new Button.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        System.out.println("Clicked position:"+position);
                        System.out.println("ArraySize:"+m_savedHomesList.size());
                      //Integer index = (Integer) v.getTag();
                          m_savedHomesList.remove(position); 
                          mHandler.setEmptyMessage(0);
                    }
                }
            );

    mHandler =  new Handler() { // Don't forget to declare your handler
     public void handleMessage(Message msg) {
        notifyDataSetChanged();
       }
    };

    return convertView;
}

答案 2 :(得分:-1)

很高兴知道你克服了问题并解决了问题

无论如何,我建议你使用不同的方法,因为它会更正确(只是建议考虑一下)

  • 在适配器中设置由活动实现的接口。
  • 当使用要删除的项目的位置单击删除btn时,将回调发送到活动。
  • 在活动中收到的回调(也在那里收到位置)更新传递给适配器的arrayList并调用notifyDataSetChanged()