无法在警报对话框中获取自定义列表视图

时间:2016-03-03 05:53:31

标签: java android android-alertdialog custom-lists

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

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

出了什么问题?

警告对话框:

   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>

2 个答案:

答案 0 :(得分:0)

您应该使用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();

答案 1 :(得分:0)

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

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