【问题标题】:How to update checked custom listview record in android sqlite database?如何更新 android sqlite 数据库中检查的自定义列表视图记录?
【发布时间】:2017-01-11 23:26:41
【问题描述】:

我有一个带有 sqlite 数据的自定义列表视图。 Listview 包含 3 个文本视图和一个复选框。我想用列表视图中选中项目的值更新我的 sqlite 数据库。我已经完成了几乎所有步骤,但是当我选择了 2 个项目并尝试更新时,它只更新了 sqlite 数据库中的一条记录。我想更新选定的记录。给出了我的代码和Screen。 我想在单击保存按钮时执行此操作。从 sqlite 中选择的列表项 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();
            }

    }   
}
}); 

【问题讨论】:

    标签: android database sqlite listview checkbox


    【解决方案1】:

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

    【讨论】:

    • 我已经尝试了很多但不能这样做属于我的列表视图项。请帮助我应该对我的代码进行哪些更改?
    猜你喜欢
    • 1970-01-01
    • 2018-09-06
    • 1970-01-01
    • 2019-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多