简体   繁体   English

当尝试在Adapter类中单击ListView项时,我试图显示一个AlertDialog,但是黑屏覆盖了该对话框

[英]I am trying to show an AlertDialog when a ListView item is clicked within the Adapter class, but a black screen is covering the dialog

I have an adapter class that is loading an ArrayList of items into a ListView. 我有一个适配器类,它将项目的ArrayList加载到ListView中。 It does this fine. 这样做很好。 I am also trying to create an AlertDialog popup of an item in the ListView whenever a hyperlinked text is clicked in a ListView item, referencing a specific ListView item. 每当在ListView项目中单击超链接的文本时,我也会尝试在ListView中创建项目的AlertDialog弹出窗口,并引用特定的ListView项目。 However, when I show the AlertDialog the dialog has a black screen on top of it covering everything. 但是,当我显示AlertDialog时,对话框顶部有一个黑屏,覆盖了所有内容。

From what I've read so far on other posts the issue is possibly being caused because the ListView item is already inflated in the ListView and I'm trying to reinflate it in the AlertDialog. 根据我到目前为止在其他帖子上所读的内容,可能是由于ListView项已经在ListView中膨胀,并且我试图在AlertDialog中对其进行膨胀而引起的。 If that is the issue then, then how can I get a reference to the already inflated ListView item, show it in the AlertDialog, all from the adapter class. 如果那是问题所在,那么我如何获得对已经膨胀的ListView项的引用,将其显示在AlertDialog中,全部来自适配器类。

Here's a picture of what's happening. 这是正在发生的事情的图片。

在此处输入图片说明

Here's the following code for my adapter class we well. 这是我的适配器类的以下代码,我们很好。

public View getView(int position, View convertView, ViewGroup parent) {

    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);


    listView = inflater.inflate(R.layout.list_item_reply, null);

    TextView textView_name = (TextView) listView.findViewById(R.id.textViewName);
    TextView textView_time = (TextView) listView.findViewById(R.id.textViewTime);
    TextView textView_title = (TextView) listView.findViewById(R.id.textViewThreadTitle);

    textView_name.setText(this.reply_list.get(position).getName());
    textView_time.setText(this.reply_list.get(position).getTime_diff() + " ago");
    textView_title.setText(Html.fromHtml(reply_list.get(position).getTitle()));

    Spannable clickableMessage = (Spannable) textView_message.getText();
    clickableMessage = Find_String_Matches(clickableMessage);
    textView_message.setText(clickableMessage, TextView.BufferType.SPANNABLE);

    return listView;
}

private Spannable Find_String_Matches(Spannable text){
    Pattern pattern = Pattern.compile(thread_regex);
    Matcher matcher = pattern.matcher(text.toString());

    while (matcher.find()) {
        Log.d("start", "Start index: " + matcher.start());
        Log.d("end", " End index: " + matcher.end());
        Log.d("found", " Found: " + matcher.group());

        text = addClickablePart(text, matcher.start(), matcher.end());
    }

    return text;
};

public void Popup_Thread(final int pos){
    AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context);
    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View listReplyView = getView(pos, null, null);
    dialogBuilder.setView(listReplyView);

    AlertDialog alertDialog = dialogBuilder.create();
    alertDialog.show();
};

This should work. 这应该工作。 There is no need to inflate the layout 无需增加布局

AlertDialog.Builder builder = new AlertDialog.Builder(context);
                builder.setCancelable(true);
                builder.setMessage("Your message");
                View listReplyView = getView(pos, null, null);
                dialogBuilder.setView(listReplyView);
                // Yes option
                builder.setPositiveButton(R.string.positive_option,
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                              //smth
                            }
                        });

                // Cancel option
                builder.setNegativeButton(R.string.negative_option,
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss(); // If cancel is pressed, dialog is closed
                            }
                        });

                // Show the dialog window
                AlertDialog dialog = builder.create();
                dialog.show();

Try to inflate your layout item : 尝试为您的布局项目充气:

public void Popup_Thread(final int pos){
    AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
    LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View dialogView = inflater.inflate(R.layout. list_item_reply, null);
    dialogBuilder.setView(dialogView);

// set value into textview
TextView textView_name = (TextView) dialogView.findViewById(R.id.textViewName);
TextView textView_time = (TextView) 
dialogView.findViewById(R.id.textViewTime);
TextView textView_title = (TextView) 
dialogView.findViewById(R.id.textViewThreadTitle);

textView_name.setText(this.reply_list.get(pos).getName());
textView_time.setText(this.reply_list.get(pos).getTime_diff() + " ago");
textView_title.setText(Html.fromHtml(reply_list.get(pos).getTitle()));

 AlertDialog alertDialog = dialogBuilder.create();
    alertDialog.show();
};

i have a good magic for you 我对你有很好的魔力

Create a layout same as custom ListView item Layout and when you show() your dialog box get current item and put in same layout and inflate it in dialog box 创建一个与自定义ListView项布局相同的布局,并在show()时,对话框获取当前项并放入相同的布局,然后在对话框中对其进行充气

Please provide transperent theme for dialog. 请提供对话的透明主题。

add following style in values\\styles.xml 在values \\ styles.xml中添加以下样式

 <?xml version="1.0" encoding="utf-8"?> <resources> <style name="Theme.Transparent" parent="android:Theme"> <item name="android:windowIsTranslucent">true</item> <item name="android:windowBackground">@android:color/transparent</item> <item name="android:windowContentOverlay">@null</item> <item name="android:windowNoTitle">true</item> <item name="android:windowIsFloating">true</item> <item name="android:backgroundDimEnabled">false</item> </style> </resources> 

and provide it like ...... 并提供......

 listView = inflater.inflate(R.layout.list_item_reply, R.styles.Theme.Transparent);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM