如何在Android中删除项目时取消选中复选框?

时间:2012-05-19 06:11:14

标签: android list listview

在我的应用程序中,单击“删除”按钮,“订购”页面中的项目将被删除..但是,也应该在ListView中取消选中已删除的项目..请帮我取消选中已删除的项目..

我试过,这是我的代码:

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


      final  ViewHolder holder;

        if (convertView == null) 
        {
            convertView = inflater.inflate(R.layout.selecteditemlistview, null);
            holder = new ViewHolder();

            holder.textViewSelectedText = (TextView)convertView.findViewById(R.id.selectedtext);
            holder.price=(TextView)convertView.findViewById(R.id.selectitemprice);
            holder.image=(ImageView)convertView.findViewById(R.id.selectitemimage);
            holder.qty=(EditText)convertView.findViewById(R.id.selectqty);
            holder.total=(TextView)convertView.findViewById(R.id.price);               
            holder.delete = (Button) convertView.findViewById(R.id.delete);

            holder.uncheck = (CheckBox)convertView.findViewById(R.id.bcheck);

            convertView.setTag(holder);
        }
        else 
        {
            holder = (ViewHolder) convertView.getTag();
        }
        String amount=holder.qty.getText().toString();
        final Double price1=Double.parseDouble(itemprice.get(position));
        int qut=Integer.parseInt(holder.qty.getText().toString());
        Double total=(price1*qut);
        holder.textViewSelectedText.setText(arr1.get(position));
        holder.price.setText(itemprice.get(position));
        holder.image.setImageBitmap(itemimage.get(position));
        holder.total.setText(String.valueOf(total));


        holder.delete.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                 arr1.remove(position);
                 itemimage.remove(position);
                 itemprice.remove(position);
                 notifyDataSetChanged();
                 holder.uncheck.setChecked(false);
            }

        });

先谢谢。

这是我的Logcat:

      05-19 12:10:17.355: D/AndroidRuntime(721): Shutting down VM
      05-19 12:10:17.365: W/dalvikvm(721): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
      05-19 12:10:17.375: E/AndroidRuntime(721): FATAL EXCEPTION: main
      05-19 12:10:17.375: E/AndroidRuntime(721): java.lang.NullPointerException
      05-19 12:10:17.375: E/AndroidRuntime(721):    at com.customer.CustomAdapter$1.onClick(CustomAdapter.java:114)
      05-19 12:10:17.375: E/AndroidRuntime(721):    at android.view.View.performClick(View.java:2408)
      05-19 12:10:17.375: E/AndroidRuntime(721):    at android.view.View$PerformClick.run(View.java:8816)
      05-19 12:10:17.375: E/AndroidRuntime(721):    at android.os.Handler.handleCallback(Handler.java:587)
      05-19 12:10:17.375: E/AndroidRuntime(721):    at android.os.Handler.dispatchMessage(Handler.java:92)
      05-19 12:10:17.375: E/AndroidRuntime(721):    at android.os.Looper.loop(Looper.java:123)
      05-19 12:10:17.375: E/AndroidRuntime(721):    at android.app.ActivityThread.main(ActivityThread.java:4627)
      05-19 12:10:17.375: E/AndroidRuntime(721):    at java.lang.reflect.Method.invokeNative(Native Method)
      05-19 12:10:17.375: E/AndroidRuntime(721):    at java.lang.reflect.Method.invoke(Method.java:521)
      05-19 12:10:17.375: E/AndroidRuntime(721):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
      05-19 12:10:17.375: E/AndroidRuntime(721):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
      05-19 12:10:17.375: E/AndroidRuntime(721):    at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:2)

更改以下行的顺序:

             notifyDataSetChanged();
             holder.uncheck.setChecked(false);

             holder.uncheck.setChecked(false);
             notifyDataSetChanged();

答案 1 :(得分:0)

试试这个

holder.uncheck.setChecked(false);
             notifyDataSetChanged();