![](/img/trans.png)
[英]How do I read certain data from sqlite database and show it in a dialog when a listview item is clicked?
[英]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
我有一个适配器类,它将项目的ArrayList加载到ListView中。 这样做很好。 每当在ListView项目中单击超链接的文本时,我也会尝试在ListView中创建项目的AlertDialog弹出窗口,并引用特定的ListView项目。 但是,当我显示AlertDialog时,对话框顶部有一个黑屏,覆盖了所有内容。
根据我到目前为止在其他帖子上所读的内容,可能是由于ListView项已经在ListView中膨胀,并且我试图在AlertDialog中对其进行膨胀而引起的。 如果那是问题所在,那么我如何获得对已经膨胀的ListView项的引用,将其显示在AlertDialog中,全部来自适配器类。
这是正在发生的事情的图片。
这是我的适配器类的以下代码,我们很好。
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();
};
这应该工作。 无需增加布局
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();
尝试为您的布局项目充气:
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();
};
我对你有很好的魔力
创建一个与自定义ListView项布局相同的布局,并在show()时,对话框获取当前项并放入相同的布局,然后在对话框中对其进行充气
请提供对话的透明主题。
在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>
并提供......
listView = inflater.inflate(R.layout.list_item_reply, R.styles.Theme.Transparent);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.