在Alert对话框中使用Cursor和setMultiChoiceItems

时间:2010-08-30 08:26:56

标签: android

我想在警告对话框中显示多重列表。如果我使用数组来存储项目列表,那么它工作正常:

d.setMultiChoiceItems(R.array.items,
                    new boolean[]{false, true, false, true, false, false, false},
                    new DialogInterface.OnMultiChoiceClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton,
                                boolean isChecked) {

                            /* User clicked on a check box do some stuff */
                        }
                    })

但在我的情况下,项目列表是动态的,我正在从数据库中找到它。数据库不断更新其内容,因此列表也以固定的时间间隔进行更新。 因此,我不想使用数组,而是想在setMultiChoiceItems的参数中使用游标。 谁能告诉我怎么做......?

2 个答案:

答案 0 :(得分:3)

只需使用setMultiChoiceItems()的另一个版本,即Cursor作为参数的版本。您还可以在结果集中为其指定表示标签和布尔复选框设置的列的名称。

答案 1 :(得分:0)

我们在JohnBryce耶路撒冷,课程ID 5124通过如下实现它解决了这个问题:

final Map<Long,Boolean> selectedContactsMap = new HashMap<Long,Boolean>();

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) 
{
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Share link");
    View selectContactView = getLayoutInflater().inflate(R.layout.contact_select_layout, null);
    builder.setView(selectContactView);

    ListView contactsListView = (ListView)selectContactView.findViewById(R.id.selectContactListView);

    CursorAdapter adapter = new CursorAdapter(this, cursor, true)
    {

        @Override
        public void bindView(View view, Context arg1, Cursor cursor) 
        {
            final long id = cursor.getLong(cursor.getColumnIndex(Contacts._ID));
            final String displayName = cursor.getString(cursor.getColumnIndex(Contacts.DISPLAY_NAME));

            TextView displayNameTextView = (TextView)view.findViewById(R.id.displayNameTextView);
            final CheckBox selectContactCheckBox = (CheckBox)view.findViewById(R.id.selectContactCheckBox);

            displayNameTextView.setText(displayName);

            boolean isChecked = selectedContactsMap.containsKey(id)?
                        selectedContactsMap.get(id):false;

            selectContactCheckBox.setChecked(isChecked);

            selectContactCheckBox.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) 
                {
                    boolean isChecked = selectContactCheckBox.isChecked();
                    if(!isChecked && selectedContactsMap.containsKey(id))
                    {
                        Log.d(TAG, "Remove id: "+id+": "+displayName);
                        selectedContactsMap.remove(id);
                    }
                    else
                    {
                        Log.d(TAG, "Select id: "+id+": "+displayName+" : "+isChecked);
                        selectedContactsMap.put(id, isChecked);
                    }   
                }
            });

        }

        @Override
        public View newView(Context context, Cursor arg1, ViewGroup arg2) 
        {
            View selectContactItemView = getLayoutInflater().inflate(R.layout.contact_select_item_layout, null);
            return selectContactItemView;
        }

    };

    contactsListView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg0, View view, int position,
                long id) 
        {
            WebViewerFragment webViewerFrag = (WebViewerFragment)
                    getFragmentManager().findFragmentById(R.id.web_viewer_fragment);

            String currentLink = null;
            if (webViewerFrag != null) 
            {
                currentLink = webViewerFrag.getCurrentLink();
                long _id = (Long)view.getTag();
                share(_id, currentLink);

            }
            else
            {
                Toast.makeText(MainActivity.this,"No link is found", Toast.LENGTH_LONG).show();
            }

            shareDialog.dismiss();

        }
    });
    contactsListView.setAdapter(adapter);

    builder.setView(selectContactView);
    builder.setPositiveButton("Share!", new OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) 
        {

        }
    });
    builder.setNegativeButton("Cancel", new OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
             Toast.makeText(MainActivity.this,":-(",Toast.LENGTH_SHORT).show();
        }
    });

    shareDialog = builder.create();



}