android - AlertDialog 多选上的自定义 ListView

标签 android android-alertdialog listview-adapter

我想创建与图片相同的AlertDialog: enter image description here

我的代码:

adapter_book_desc.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:gravity="center_vertical"
    android:paddingLeft="15dip"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <CheckedTextView
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <TextView
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="?android:attr/textColorSecondary" 
        android:id="@+id/text2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</LinearLayout>

主要 Activity

final CharSequence[] items = arr_book_title.toArray(new CharSequence[arr_book_title.size()]);
                final ArrayList<Integer> seletedItems = new ArrayList<Integer>();
                AlertDialog.Builder builder = new AlertDialog.Builder(_context);
                builder.setAdapter(new adapterBookDesc(), null);
                builder.setTitle(_context.getString(R.string.alert_selectbook_message));

                builder.setMultiChoiceItems(items, null, new DialogInterface.OnMultiChoiceClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int indexSelected, boolean isChecked) {
                        if (isChecked) {
                            seletedItems.add(indexSelected);
                        }else if(seletedItems.contains(indexSelected)){
                            seletedItems.remove(Integer.valueOf(indexSelected));
                        }
                    }
                }).setPositiveButton(R.string.button_ok, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int id) {
                        String rr = ""; 
                        for (Object s : seletedItems){
                            rr += s.toString() + ";";
                        }
                        if(!rr.equals("")){
                            new saveBookInAutor().executeOnExecutor(AsyncTask.SERIAL_EXECUTOR, rr, selGroupParam);
                        }else{
                            Toast.makeText(_context, R.string.toast_select_newbookautor, Toast.LENGTH_LONG).show();
                        }
                    }
                }).setNegativeButton(R.string.button_cancel, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int id) {
                    }
                });

                builder.create().show(); 

和 BaseAdapter 中的类

class adapterBookDesc extends BaseAdapter
{
    @Override
    public int getCount() 
    {
        return arr_book_title.size();
    }

    @Override
    public Object getItem(int position) 
    {
        return arr_book_title.get(position);
    }

    @Override
    public long getItemId(int position) 
    {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) 
    {
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) _context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.adapter_book_desc, null);
        }

        ((TextView)convertView.findViewById(R.id.text1)).setText(arr_book_title.get(position));
        ((TextView)convertView.findViewById(R.id.text2)).setText(arr_book_param.get(position));

        return convertView;
    }
}  

但是在图片中它没有显示。但是,如果要注意 builder.setMultiChoiceItems 行的注释 - 它显示的所有内容,除了多项选择。

如何更正代码,使其如图所示。多项选择和标题以及消息在一项中?

最佳答案

如果您必须在列表中显示两个 TextView,那么您必须使用自定义适配器,并且您已经在使用它,因此您无需使用 builder.setMultiChoiceItems,只需自定义您的布局,将复选框放入您的布局中并进行管理..

如果您不需要两个 TextView,则删除自定义适配器。

Check this

关于android - AlertDialog 多选上的自定义 ListView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20626625/

相关文章:

android - AlertDialog setOnDismissListener 不工作

android - 从另一个类调用对话框

android - 如何将 OnClickListener 添加到 ListView 适配器内的按钮?

android - 如何将值从 ListView 适配器传递到 fragment ?

android - pom.xml 中的 Maven 依赖错误

android - Android 上的 Libgdx 并正确处理

android - 我可以从我的 android 应用程序中识别新邮件的 gmail 通知吗

java - Android 定时器/时钟

android GPS 在按钮点击时启用

Android - 我的简单 ListView/Toast 应用程序在启动时崩溃......有什么想法吗?