简体   繁体   中英

How to update checked custom listview record in android sqlite database?

I have a custom listview with sqlite data. Listview contains 3 textviews and one checkbox. I want to update my sqlite database with a value for the checked items in listview. I have done almost steps but when I selected 2 items and tried to update, it updates only one record in sqlite database. I want to update the selected records. My code and Screen is given. I want to perform this on save button click. The selected listitems id from sqlite is storing in str1 variable.

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();
            }

    }   
}
}); 

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM