自定义ListView在对话框中

时间:2013-09-14 08:17:56

标签: android listview 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

    <?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

      <?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>

2 个答案:

答案 0 :(得分:9)

您需要使用AlertDialog.Builder -

请参阅 - Docs

编辑 -

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

答案 1 :(得分:2)

@Vishal是对的,如果你需要在对话框中膨胀视图,你需要使用AlertDialog。这是示例代码。试过下面的代码。

public void showDialog(){
    AlertDialog.Builder dialog = new AlertDialog.Builder(getContext());     
    dialog.setTitle("List of likers");
    dialog.setCancelable(true);

    View view = ((Activity)getContext()).getLayoutInflater().inflate(R.layout.likers_list, null); 

    dbobj = new DataBaseHandler(getContext());
    likeItems=dbobj.select_HowlikeComment();
    dbobj.CloseDataBase();


    ListView list = (ListView) view.findViewById(R.id.ListLikersList);
    LikersCustomeAdapter adapter= new LikersCustomeAdapter(getContext(), R.layout.likerscustomelist, likeItems);

    list.setAdapter(adapter);

    dialog.setView(view);
    dialog.show();
}