android - 如何从 AlertDialog 中由光标填充的 ListView 获取选中的值?

标签 android listview android-alertdialog android-cursor

我创建了一个 AlertDialog,其中包含项目的 ListView 以及复选框。我希望能够收集调用 Activity 中选中的项目,以便我可以使用它们。

我的 Activity 中有一个按钮:

<Button android:id="@+id/my_button" />

我是这样的:

Button button = (Button)findViewById(R.id.my_button);

然后我设置了一个监听器:

button.setOnClickListener(my_on_click_listener);
View.OnClickListener my_on_click_listener = new View.OnClickListener(){
    @Override
    public void onClick(View v){
        my_method();
    }
};

调用一个方法:

public void my_method(){
    AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
    LayoutInflater layoutInflater = this.getLayoutInflater();
    View view = layoutInflater.inflate(R.layout.my_listview_layout, null);
    dialogBuilder.setView(view);

    final DatabaseHelper databaseHelper = new DatabaseHelper(this);
    String[] fromColumns = {"_id","value"};
    int[] toViews = {R.id.dropdown_id, R.id.dropdown_value};

    final ListView listView = (ListView)view.findViewById(R.id.my_listview);

    final Cursor cursor = databaseHelper.getData();
    SimpleCursorAdapter simpleCursorAdapter = new SimpleCursorAdapter(this, R.layout.my_layout, cursor, fromColumns, toViews, 0);
    listView.setAdapter(simpleCursorAdapter);

    // I tried this, but I'm not sure what to use for the isCheckedColumn and labelColumn
    dialogBuilder.setMultiChoiceItems(cursor, "", "", new DialogBuilder.OnMultiChoiceClickListener(){
        @Override
        public void onClick(DialogInterface dialog, int which, boolean isChecked){
            // do stuff here ...
        }
    });

    dialogBuilder.setPositiveButton("Save Selected", new DialogInterface.OnClickListener(){
        public void onClick(DialogInterface dialog, int which){
            // I tried this, but it only collects the elements within view
            for(int i=0; i<listView.getCount(); i++){
                LinearLayout ll = (LinearLayout)listView.getChildAt($i);
                if(null != ll){
                    CheckBox cb = (CheckBox)ll.findViewById(R.id.dropdown_checkbox);
                    TextView tv01 = (CheckBox)ll.findViewById(R.id.dropdown_id);
                    TextView tv02 = (CheckBox)ll.findViewById(R.id.dropdown_value);
                }
            }
        }
    });

    final AlertDialog alert = dialogBuilder.create();
    alert.setTitle("Select Choices");
    alert.show();
}

这是my_listview_layout.xml:

<ListView android:id="@+id/my_listview" />

这是my_layout.xml:

<LinearLayout>
    <CheckBox android:id="@+id/dropdown_checkbox" />
    <TextView android:id="@+id/dropdown_value" />
    <TextView android:id="@+id/dropdown_id" android:visibility="invidible"/>
</LinearLayout>

请参阅上面代码中我的两条评论。

那么,如何从 Android 中的 AlertDialog 中的光标填充的 ListView 中获取选中的值?

----

更新:

在我的代码中,我注释掉了这一行:

listView.setAdapter(simpleCursorAdapter);

并更改了这一行:

dialogBuilder.setMultiChoiceItems(cursor, "", "", new DialogBuilder.OnMultiChoiceClickListener();

对此:

dialogBuilder.setMultiChoiceItems(cursor, "_id", "value", new DialogBuilder.OnMultiChoiceClickListener()

现在我的对话框已正确呈现,我可以确定检查哪些项目。我猜想 [也许] ListViewsetMultiChoiceItems() 都在争夺对绘制选项列表的控制权。

但是,我有一个新问题。如果我检查列表顶部的某些项目,然后向下滚动并向上滚动,则我检查的项目已被取消选中。那,列表中的第一项默认情况下是选中的,我不知道如何取消选中它。

有什么想法吗???

再次感谢...。

最佳答案

你可以这样做:

// Fetch the list of items from any of your sources and store it as an array list.
// Here I'm considering itemList as the source it is a string array but you
   can use an array list rather.

    final ArrayList<String> selectedItemList = new ArrayList<>();
    final int itemListLength = itemList.length;
    final boolean[] selectionPreference = new boolean[itemListLength];

    for(int i=0; i < itemListLength; i++){
        Arrays.fill(selectionPreference, Boolean.FALSE);
    }

    AlertDialog.Builder builder =  new AlertDialog.Builder(context);

    builder.setTitle("Select one")
            .setMultiChoiceItems(itemList, selectionPreference, 
     new DialogInterface.OnMultiChoiceClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                    String item = itemList[which];
                    if(isChecked){
                        selectionPreference[which] = true;
                    } else {
                        selectionPreference[which] = false;
                    }
                }
            })
    .setPositiveButton("ok",new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            for (int i=0; i<itemListLength; i++) {
                if(selectionPreference[i]) {
                    selectedItemList.add(itemList[i]);
                }
            }
            Log.i(TAG, String.valueOf(selectedItemList));
        }
    }).show();

您使用的过程可能有点乏味。因此,使用这种方式可以轻松访问和保留值。希望对您有所帮助。

关于android - 如何从 AlertDialog 中由光标填充的 ListView 获取选中的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48120021/

相关文章:

c# - 滚动时 Xamarin.Android 中的 ListView 内存不足

android - 如何使用 Android 在不使用 toast(Alter 对话框)的情况下在设置错误消息中制作编辑文本框?

android - 在配置更改时更改 DialogFragment 中的布局和保存状态

java - 如何使用 Cursor 逐行返回所有数据库结果 - SQLite 数据库 *工作*

android - 使用 strings.xml 中的字符串数组填充 string[] 时出现问题

android - 我应该单独使用 notifyDataSetChanged 还是更新 ListView 项目?

c# - ObjectListView - 如何删除所有对象

android - 在android中将透明背景设置为alertdialog

java - 在 Android 中使用 DecimalFormat 格式化数字

java - 使用 webview 将 HTML 代码添加到 Assets 文件夹中的 html 文件