【问题标题】:Unable to get custom list view in alert dialog无法在警报对话框中获取自定义列表视图
【发布时间】:2016-06-16 07:26:26
【问题描述】:

我正在警报对话框中实现自定义列表视图。它没有显示任何视图。

我创建了一个查询来从表中获取标题列表。我想在警报对话框中显示此列表。

怎么了?

警报对话框:

   selectTable.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                int selected = 0;

                TimeTableHelper th = new TimeTableHelper(getApplicationContext());
                final List<String> tables = new ArrayList<String>(th.getTitle());


            AlertDialog.Builder alertDialog = new AlertDialog.Builder(AddEventActivity.this);
            LayoutInflater inflater = getLayoutInflater();
            View convertView = (View) inflater.inflate(R.layout.tablelist, null ,false);
            alertDialog.setView(convertView);
            ListView lv = (ListView) convertView.findViewById(R.id.list);

            CustomAlertAdapter adapter = new CustomAlertAdapter(AddEventActivity.this, (ArrayList<String>)tables);
            lv.setAdapter(adapter);


                alertDialog.setSingleChoiceItems(adapter,selected,
                        new DialogInterface.OnClickListener() {

                            public void onClick(DialogInterface dialog, int item) {

                                ListView lw = ((AlertDialog)dialog).getListView();
                                Object checkedItem = lw.getAdapter().getItem(lw.getCheckedItemPosition());

                                txtTable.setText(String.valueOf(checkedItem));
                                dialog.dismiss();
                            }
                        });
                alertDialog.show();

            }
        });

customAlertAdapter

public class CustomAlertAdapter extends BaseAdapter{

    Context ctx=null;
    ArrayList<String> listarray=null;
    private LayoutInflater mInflater=null;
    public CustomAlertAdapter(Activity activty,ArrayList<String> list)
    {
        this.ctx=activty;
        mInflater = activty.getLayoutInflater();
        this.listarray=list;
    }
    @Override
    public int getCount() {

        return listarray.size();
    }

    @Override
    public Object getItem(int arg0) {
        return null;
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup arg2) {
        final ViewHolder holder;
        if (convertView == null ) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.alertlistrow, null);
            holder.titlename = (TextView) convertView.findViewById(R.id.tableTitle);
            convertView.setTag(holder);
        }
        else {
            holder = (ViewHolder) convertView.getTag();
        }

        String datavalue=listarray.get(position);
        holder.titlename.setText(datavalue);

        return convertView;
    }

    private static class ViewHolder {
        TextView titlename;
    }

alertlistrow 布局:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="1"
    android:orientation="vertical"
    android:measureWithLargestChild="false">


        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/white">
            <Button
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:layout_gravity="center|right"
                android:background="@drawable/circle_shape"
                android:id="@+id/selectColor"
                android:layout_alignTop="@+id/switch2"
                android:layout_alignParentStart="true"
                android:layout_marginLeft="20dp" />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="@android:style/TextAppearance.Medium"
                android:id="@+id/tableTitle"
                android:layout_alignParentLeft="false"
                android:layout_alignParentStart="false"
                android:layout_centerVertical="true"
                android:layout_toRightOf="@+id/selectColor"
                android:layout_marginLeft="10dp" />


        </RelativeLayout>


</LinearLayout>

【问题讨论】:

    标签: java android android-alertdialog custom-lists


    【解决方案1】:

    您应该使用Dialog 代替AlertDialog,如下所示:-

        final Dialog dialog = new Dialog(YOUR_CONTEXT.this);
        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
        dialog.setContentView(R.layout.CREATE_YOUR_LAYOUT_HERE);
    
        pop_up_list = (ListView) dialog.findViewById(R.id.tablelist);
    
        CustomAlertAdapter adapter = new CustomAlertAdapter(AddEventActivity.this, (ArrayList<String>)tables);
    
        pop_up_list.setAdapter(adapter);
    
        dialog.setTitle(null);
        dialog.setCancelable(true); // AS YOUR REQUIREMENT , YOU CAN SET IT FALSE... TO NOT DISMISS THE DIALOG
        dialog.show();
    

    【讨论】:

    • 我应该在哪里添加列表视图?在哪种布局中?
    • 您必须在布局文件中为您的对话框创建布局,然后使用上面的代码
    • 我已经创建了包含 listview 的 alertlistrow 布局和 tablelist 布局。
    • 比,有什么问题兄弟?? ...在我上面的代码中,将这些 CREATE_YOUR_LAYOUT_HERE 替换为您的布局
    • 这就是我所拥有的。请检查警报对话框的编辑问题代码。它显示空白对话框。
    【解决方案2】:

    根据需要更改选择模式并自定义您的适配器。不要设置任何布局,只需设置您的适配器。

    public static AlertDialog dialog;
    
    
    public static void showMultiChoiceDialog(Context context, String title, ArrayAdapter arrayAdapter) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context,style.Theme_Holo_Light_Dialog);
        builder.setTitle(title);
        builder.setAdapter(arrayAdapter,null);
        builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                dialog.dismiss();
            }
        });
        if (dialog == null || !dialog.isShowing()) {
            dialog = builder.create();
            dialog.getListView().setItemsCanFocus(false);
            dialog.getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
            dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
            dialog.show();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-12
      相关资源
      最近更新 更多