Custom listview on AlertDialog multichoice

亡梦爱人 提交于 2020-01-02 11:15:05

问题


I would like to create AlertDialog same as on the picture:

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:


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



来源:https://stackoverflow.com/questions/20626625/custom-listview-on-alertdialog-multichoice

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!