如何在android sqlite数据库中更新已检查的自定义列表视图记录?

时间:2016-09-04 11:06:43

标签: android database sqlite listview checkbox

我有一个带有sqlite数据的自定义列表视图。 Listview包含3个文本视图和一个复选框。我想用listview中检查项的值更新我的sqlite数据库。我已经完成了几乎所有的步骤,但是当我选择2个项目并尝试更新时,它只更新了sqlite数据库中的一条记录。我想更新选定的记录。我的代码和Screen已经给出了。 我想在保存按钮点击时执行此操作。来自sqlite的所选listitems id存储在str1变量中。

btnSaveCancellation.setOnClickListener(new OnClickListener()
    {           
        @Override
        public void onClick(View v) 
        {                                           
            long itemCount = listVCancelCollection.getCount();

            for(i=0;i<=itemCount-1;i++)
            {
                if(chkConfirm.isChecked())
                {                   
                    final long str1 = list_collection.get(i).getId();       

                    AlertDialog.Builder alertDialogBuild=new AlertDialog.Builder(CancelCollection.this, AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);
                    alertDialogBuild.setTitle("Cancel Collection?");
                    alertDialogBuild.setCancelable(false).setPositiveButton
                    ("Yes", new DialogInterface.OnClickListener() 
                    {
                        @Override
                        public void onClick(DialogInterface dialog, int which)
                        {
                            dialog.cancel();

                            displayReasonPopup();                                           
                        }

                    private void displayReasonPopup() 
                    {
                        Button btnAlert;

                        LayoutInflater layoutInflater = LayoutInflater.from(CancelCollection.this);
                        View promptView = layoutInflater.inflate(R.layout.input_dialog_reason, null);

                        final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(CancelCollection.this);
                        alertDialogBuilder.setView(promptView);
                        alertDialogBuilder.setTitle("Reason");

                        final EditText editReason = (EditText) promptView.findViewById(R.id.edittext_reason);
                        editReason.setTypeface(ttf);    

                        alertDialogBuilder.setCancelable(false)
                        .setPositiveButton("Save", new DialogInterface.OnClickListener()
                        {
                            public void onClick(DialogInterface dialog, int id) 
                            {                                   
                                String edtString=editReason.getText().toString().trim();

                                String strUpdate="UPDATE Customer_Collection SET Collection_Status = '0' , "
                                            + "Cancel_Notes = '"+edtString+"'"
                                            + "WHERE Customer_ID = '"+strCustomerID+"'" 
                                            + " AND Customer_Collection_ID = '"+str1+"'";   

                                SQLiteStatement statement = db.compileStatement(strUpdate);
                                db.beginTransaction();
                                statement.execute();

                                db.setTransactionSuccessful();
                                db.endTransaction();  

                            }
                        })
                            .setNegativeButton("Cancel",
                                new DialogInterface.OnClickListener() 
                            {
                                    public void onClick(DialogInterface dialog, int id)
                                    {
                                        dialog.cancel();
                                    }
                            });

                        // create an alert dialog
                        final AlertDialog alertDD = alertDialogBuilder.create();
                        alertDD.show();

                        }

                        }).setNegativeButton("No", new DialogInterface.OnClickListener()

                        {                               
                            @Override
                            public void onClick(DialogInterface dialog, int which)
                            {
                                dialog.cancel();
                            }
                        });

                AlertDialog alertDialog = alertDialogBuild.create();

                alertDialog = alertDialogBuild.setMessage("Do you want to cancel the selected collections?").show();
            }

    }   
}
}); 

1 个答案:

答案 0 :(得分:0)

您需要检查if(chkConfirm.isChecked())是否属于您的列表视图项目