Custom listview on AlertDialog multichoice

2019-05-26 09:02发布

I would like to create AlertDialog same as on the picture: enter image description here

My code:

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>

MainActivity

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(); 

and class in 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;
    }
}  

But in that look as on the picture it isn't displayed. However if to note as the comment of the line builder.setMultiChoiceItems - that everything it is displayed, except a multiple choice.

How to correct a code so that was as on the image. Multiple choice and title and the message in one item?

1条回答
别忘想泡老子
2楼-- · 2019-05-26 09:22

If you must have to display two TextView in listing then you have to use custom adapter and you already using that, so you no need to use builder.setMultiChoiceItems, just customize your layout put checkbox in your layout and manage..

If you don't need two TextView then remove custom adapter.

Check this

查看更多
登录 后发表回答