Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在android对话框窗口中更新multi-select_Java_Android_Checkbox_Cursor_Multi Select - Fatal编程技术网

Java 如何在android对话框窗口中更新multi-select

Java 如何在android对话框窗口中更新multi-select,java,android,checkbox,cursor,multi-select,Java,Android,Checkbox,Cursor,Multi Select,我一直找不到一个使用光标进行多重选择的教程。到目前为止,我的逻辑正在以我想要的方式工作,但复选框不会正确更新。我忽略了什么 return new AlertDialog.Builder(this).setTitle("Items") .setMultiChoiceItems(cur, CHECK, EDATE, new DialogInterface.OnMultiChoiceClickListener() { @Override

我一直找不到一个使用光标进行多重选择的教程。到目前为止,我的逻辑正在以我想要的方式工作,但复选框不会正确更新。我忽略了什么

return new AlertDialog.Builder(this).setTitle("Items")
            .setMultiChoiceItems(cur, CHECK, EDATE, new DialogInterface.OnMultiChoiceClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int position, boolean checked)
                {
                    DBM.open();
                    AlertDialog AD = (AlertDialog) dialog;
                    ListView list = AD.getListView();
                    list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

                    itemCur = (Cursor) list.getItemAtPosition(position);

                    if (checked)
                    {
                        //update query
                        DBM.setChecked(checkCur.getInt(checkCur.getColumnIndex(ID)), itemId, userId, 1);
                        list.setItemChecked(1, true);
                    } else
                    {
                        DBM.setChecked(checkCur.getInt(checkCur.getColumnIndex(ID)), itemId, userId, 0);
                        list.setItemChecked(1, false);
                    }
                    DBM.close();
                }
            }).setPositiveButton("OK", new DialogButtonClickHandler()).create();

android上的对话框无法修改。如果查看源代码,您将看到dialogbuilder将所有演示文稿工作委托给某些组件,而您在创建后无权访问这些组件。因此,更改用于构建对话框的组件的状态不会在以后更新对话框组件

您可以看到这种机制:在警报控制器上调用onCreate之后,您没有访问访问控制器的权限


如果你想实现这一点,最好是重建一个新的活动并给它一个对话框主题。

android上的对话框无法修改。如果查看源代码,您将看到dialogbuilder将所有演示文稿工作委托给某些组件,而您在创建后无权访问这些组件。因此,更改用于构建对话框的组件的状态不会在以后更新对话框组件

您可以看到这种机制:在警报控制器上调用onCreate之后,您没有访问访问控制器的权限


如果您想实现这一点,最好是重建一个新的活动并为其指定一个对话框主题。

您可以使用AlertDialog的
setCursor()
方法。这很简单,所以你可能不需要教程


一个相关的SO问题是,它的文档是

您可以使用AlertDialog的
setCursor()
方法。这很简单,所以你可能不需要教程


一个相关的SO问题是,它的文档是

,所以在深入研究这个问题并经过几个不同的迭代之后,我终于找到了一个我非常满意的解决方案。在学校和工作的努力推动下,我几乎没有时间在外面做额外的项目,我一直在考虑这个解决方案,但现在无法发布

我的最后一个难题是找到changeCursor函数,这解决了旧数据不再匹配要加载的DB的问题。我目前面临的障碍是选中复选框所需的时间,从单击到更新有明显的滞后。我发现当点击一条时,多条记录会更新。我无法找到这些额外更新的有效原因

下面是我目前实现的使multi-select工作的代码。这只是一个对话框代码,对于一个正在运行的演示,我将在GitHub上发布一个项目,作为一个正在运行的原型。(现已公开)

我是一个相当新的安卓开发者,我的安卓知识大部分是自学的,通过在线资源的知识学习的。我当时正在做一个学校项目,希望在一个对话框中实现一个multiselect,用选定的选项更新主要活动。请就如何改进这一点提供任何建议

优点:
-加载时正确填充复选框。
-单击检查时更新数据库。
-在数据更改后保持显示更新

缺点:
-必须单击复选框以更新值。
-无法撤消在对话框中所做的更改。值保存onClick,在用户确认之前,我无法想出临时存储新值的方法。
-单击一次即可更新多个记录,有时当选项从屏幕上滚动时,值也会更新

@Override
protected Dialog onCreateDialog(int id)
{
    switch (id) {

    case 0:
        LayoutInflater factory = LayoutInflater.from(this);

        // Setup of the view for the dialog
        final View bindListDialog = factory.inflate(R.layout.multi_list_layout, null);
        multiListView = (ListView) bindListDialog.findViewById(R.id.multiList);

        // Because I do not know how to properly handle an undo in this situation
        // I make the dialog only close if the button is pressed and confirms the changes
        return new AlertDialog.Builder(MultiSelectDemoActivity.this).setTitle(R.string.multiSelectTitle)
                .setCancelable(false).setView(bindListDialog)
                .setPositiveButton(R.string.btnClose, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton)
                    {
                        updateItemList(); // In my implementation there is a list view
                                          // that shows what has been selected.
                    }
                }).create();
    default:
        return null;
    }
}

private static final boolean ONCREATE = true;
private static final boolean ONUPDATE = false;

private void setupMultiList(Boolean newList)
{
    demoDBM.open();
    multiCur = demoDBM.getList(userId); // Gets all items tied to the user.
    startManagingCursor(multiCur);
    // Uses the cursor to populate a List item with an invisible ID column,
    // a name column, and the checkbox
    demoDBM.close();

    if (newList)
    {
        // Creates a new adapter to populate the list view on the dialog
        multiAdapter = new SimpleCursorAdapter(this, R.layout.check_list_item, multiCur, new String[] { DemoDBM.ID,
                DemoDBM.NAME, DemoDBM.SEL }, new int[] { R.id.itemId, R.id.itemName, R.id.itemCheck });
        multiAdapter.setViewBinder(new MyViewBinder());
        multiListView.setAdapter(multiAdapter);
    } else
    {
        // updates the previously made adapter with the new cursor, without changing position
        multiAdapter.changeCursor(multiCur);
    }
}

@Override
protected void onPrepareDialog(final int id, final Dialog dialog, Bundle args)
{
    setupMultiList(ONCREATE);
}

public class MyViewBinder implements ViewBinder
{
    @Override
    public boolean setViewValue(View view, Cursor cursor, int columnIndex)
    {
        int checkId = cursor.getColumnIndex(DemoDBM.SEL);

        if (columnIndex == checkId)
        {
            CheckBox cb = (CheckBox) view;
            // Sets checkbox to the value in the cursor
            boolean bChecked = (cursor.getInt(checkId) != 0);
            cb.setChecked(bChecked); // Switches the visual checkbox.

            cb.setOnCheckedChangeListener(new MyOnCheckedChangeListener());
            return true;
        }
        return false;
    }
}

public class MyOnCheckedChangeListener implements OnCheckedChangeListener
{
    @Override
    public void onCheckedChanged(CompoundButton checkBox, boolean newVal)
    {
        View item = (View) checkBox.getParent(); // Gets the plain_list_item(Parent) of the Check Box

        // Gets the DB _id value of the row clicked and updates the Database appropriately.
        int itemId = Integer.valueOf(((TextView) item.findViewById(R.id.itemId)).getText().toString());
        demoDBM.open();
        demoDBM.setChecked(itemId, userId, newVal);
        demoDBM.close();

        setupMultiList(ONUPDATE);
    }
}

因此,在深入研究这个问题并经过几个不同的迭代之后,我终于找到了一个我非常满意的解决方案。在学校和工作的努力推动下,我几乎没有时间在外面做额外的项目,我一直在考虑这个解决方案,但现在无法发布

我的最后一个难题是找到changeCursor函数,这解决了旧数据不再匹配要加载的DB的问题。我目前面临的障碍是选中复选框所需的时间,从单击到更新有明显的滞后。我发现当点击一条时,多条记录会更新。我无法找到这些额外更新的有效原因

下面是我目前实现的使multi-select工作的代码。这只是一个对话框代码,对于一个正在运行的演示,我将在GitHub上发布一个项目,作为一个正在运行的原型。(现已公开)

我是一个相当新的安卓开发者,我的安卓知识大部分是自学的,通过在线资源的知识学习的。我当时正在做一个学校项目,希望在一个对话框中实现一个multiselect,用选定的选项更新主要活动。请就如何改进这一点提供任何建议

优点:
-加载时正确填充复选框。
-单击检查时更新数据库。
-在数据更改后保持显示更新

缺点:
-必须单击复选框以更新值。
-无法撤消在对话框中所做的更改。值保存onClick,在用户确认之前,我无法想出临时存储新值的方法。
-单击一次即可更新多个记录,有时当选项从屏幕上滚动时,值也会更新

@Override
protected Dialog onCreateDialog(int id)
{
    switch (id) {

    case 0:
        LayoutInflater factory = LayoutInflater.from(this);

        // Setup of the view for the dialog
        final View bindListDialog = factory.inflate(R.layout.multi_list_layout, null);
        multiListView = (ListView) bindListDialog.findViewById(R.id.multiList);

        // Because I do not know how to properly handle an undo in this situation
        // I make the dialog only close if the button is pressed and confirms the changes
        return new AlertDialog.Builder(MultiSelectDemoActivity.this).setTitle(R.string.multiSelectTitle)
                .setCancelable(false).setView(bindListDialog)
                .setPositiveButton(R.string.btnClose, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton)
                    {
                        updateItemList(); // In my implementation there is a list view
                                          // that shows what has been selected.
                    }
                }).create();
    default:
        return null;
    }
}

private static final boolean ONCREATE = true;
private static final boolean ONUPDATE = false;

private void setupMultiList(Boolean newList)
{
    demoDBM.open();
    multiCur = demoDBM.getList(userId); // Gets all items tied to the user.
    startManagingCursor(multiCur);
    // Uses the cursor to populate a List item with an invisible ID column,
    // a name column, and the checkbox
    demoDBM.close();

    if (newList)
    {
        // Creates a new adapter to populate the list view on the dialog
        multiAdapter = new SimpleCursorAdapter(this, R.layout.check_list_item, multiCur, new String[] { DemoDBM.ID,
                DemoDBM.NAME, DemoDBM.SEL }, new int[] { R.id.itemId, R.id.itemName, R.id.itemCheck });
        multiAdapter.setViewBinder(new MyViewBinder());
        multiListView.setAdapter(multiAdapter);
    } else
    {
        // updates the previously made adapter with the new cursor, without changing position
        multiAdapter.changeCursor(multiCur);
    }
}

@Override
protected void onPrepareDialog(final int id, final Dialog dialog, Bundle args)
{
    setupMultiList(ONCREATE);
}

public class MyViewBinder implements ViewBinder
{
    @Override
    public boolean setViewValue(View view, Cursor cursor, int columnIndex)
    {
        int checkId = cursor.getColumnIndex(DemoDBM.SEL);

        if (columnIndex == checkId)
        {
            CheckBox cb = (CheckBox) view;
            // Sets checkbox to the value in the cursor
            boolean bChecked = (cursor.getInt(checkId) != 0);
            cb.setChecked(bChecked); // Switches the visual checkbox.

            cb.setOnCheckedChangeListener(new MyOnCheckedChangeListener());
            return true;
        }
        return false;
    }
}

public class MyOnCheckedChangeListener implements OnCheckedChangeListener
{
    @Override
    public void onCheckedChanged(CompoundButton checkBox, boolean newVal)
    {
        View item = (View) checkBox.getParent(); // Gets the plain_list_item(Parent) of the Check Box

        // Gets the DB _id value of the row clicked and updates the Database appropriately.
        int itemId = Integer.valueOf(((TextView) item.findViewById(R.id.itemId)).getText().toString());
        demoDBM.open();
        demoDBM.setChecked(itemId, userId, newVal);
        demoDBM.close();

        setupMultiList(ONUPDATE);
    }
}

我希望有一个比自己对话更简单的方法。为什么可以使用填充对话框的两个字符串数组而不是包含光标信息的字符串数组进行编辑。对我来说没有意义。我希望有一个比自己对话更简单的方法。为什么可以使用填充对话框的两个字符串数组而不是包含光标信息的字符串数组进行编辑。对我来说没有意义。