Custom ListView in a dialog(对话框中的自定义 ListView)
本文介绍了对话框中的自定义 ListView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个列表视图,在这个列表中,当点击其中的一个项目时,它会在对话框中显示一个自定义列表
i have a list view , inside this list when click on an item of it ,it will show a custom list inside a dialog box
对话框显示,但它只显示对话框的标题
the dialog shows up but its only shows the title of the dialog
这是我的代码
case R.id.CommentNumOfLike:
dbobj = new DataBaseHandler(getContext());
Items1 = dbobj.select_HowComment();
dbobj.CloseDataBase();
Collections.reverse(Items1);
x = (Integer) view.getTag();
Log.v("value", x + "");
target = Items1.get(x).getCommentId().toString();
CommentEvents r = new CommentEvents();
r.execute(user_id, secret_id, table, target);
final Dialog dialog = new Dialog(getContext());
dialog.setContentView(R.layout.likers_list);
dialog.setTitle("List of likers");
dialog.setCanceledOnTouchOutside(true);
dbobj = new DataBaseHandler(getContext());
likeItems=dbobj.select_HowlikeComment();
dbobj.CloseDataBase();
ListView list = (ListView) dialog.findViewById(R.id.ListLikersList);
LikersCustomeAdapter adapter= new LikersCustomeAdapter(getContext(), R.layout.likerscustomelist, likeItems);
list.setAdapter(adapter);
dialog.show();
break;
这是我的适配器
public class LikersCustomeAdapter extends ArrayAdapter<LikersComment>{
Context context;
List<LikersComment> items;
Bitmap bitmap;
String imageUser = "";
String filePath_Image = "/Pictures/jehad/joj/";
public LikersCustomeAdapter(Context context, int textViewResourceId,
List<LikersComment> objects) {
super(context, textViewResourceId, objects);
// TODO Auto-generated constructor stub
this.context=context;
}
public View getView(final int position, View convertView, ViewGroup parent) {
Log.d("View likers List", "View>>>>>>>>");
likerscommentholder h = null;
View v = convertView;
Log.v("items getName", items.get(position).getName());
LayoutInflater vi = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (v == null) {
v = vi.inflate(R.layout.likerscustomelist,null);
h = new likerscommentholder();
h.likersimg=(ImageView)v.findViewById(R.id.likersImg);
h.likersname=(TextView)v.findViewById(R.id.likersName);
v.setTag(h);
} else {
h = (likerscommentholder) v.getTag();
}
imageUser = items.get(position).getId() + items.get(position).getRand();
boolean flag_ImagePath = Methods.checkIfImage_DirExists(filePath_Image
+ imageUser);
if (flag_ImagePath == true) {
Log.v("flag_ImagePath", String.valueOf(flag_ImagePath));
bitmap = Methods.displayBitmapImage(imageUser);
h.likersimg.setImageBitmap(bitmap);
}
h.likersname.setText(items.get(position).getName());
return v;
}
}
class likerscommentholder {
ImageView likersimg;
TextView likersname;
}
这是列表视图
likers_list.xml
likers_list.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/ListLikersList"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
这是自定义布局
likerscustomelist.xml
likerscustomelist.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/likersImg"
android:layout_width="65dp"
android:layout_height="65dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/likersName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="16dp"
android:layout_marginTop="19dp"
android:layout_toRightOf="@+id/likersImg"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
</LinearLayout>
推荐答案
需要使用AlertDialog.Builder
-
参考这个- 文档
编辑 -
new AlertDialog.Builder(MyActivity.this)
.setAdapter(yourListAdapter, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//TODO - Code when list item is clicked (int which - is param that gives you the index of clicked item)
}
})
.setPositiveButton("Okay", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.setTitle("Dialog Title")
.setCancelable(false)
.show();
这篇关于对话框中的自定义 ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:对话框中的自定义 ListView
基础教程推荐
猜你喜欢
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01