0

I've managed to create a custom listview dialog based on the answers of this Stackoverflow question.how-to-display-list-view-in-alert-dialog-in-android

Here's an image of my dialog custom listview. enter image description here

I'm having an issue of how do I retrieve the user input if the user selects "Edit Post" / "Delete Post" respectively?Is there an OnClickListener for Dialog and how do I go about implementing it?Thank you :)

Community
  • 1
  • 1
Arvind Dinivra
  • 357
  • 1
  • 3
  • 14

4 Answers4

0
mainListView.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {

            // Do things that use the position or 
            // the view content to understand user input

        }
    });
Eran Goldin
  • 980
  • 12
  • 21
  • Thank you for the help,I've managed to solve my issue via your answer,and upon successful click of any item from the CustomListView dialog,I've attempted to dismiss the dialog in which everything working perfectly fine now :D! – Arvind Dinivra Jun 08 '14 at 08:28
0

I don't think you have to define the dialog view as listview in your dialog. Just create a simple layout file as following:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_marginRight="100dp"
    android:layout_gravity="right">

<TextView
    android:id="@+id/edit"
    android:layout_width="wrap_content"
    android:paddingTop="10dp"
    android:paddingBottom="10dp"
    android:paddingRight="120dp"
    android:paddingLeft="10dp"
    android:gravity="center"
    android:clickable="true"
    android:layout_height="wrap_content"
    android:textColor="@color/white"
    android:background="@drawable/selector_option_menu"
    android:text="EditPost"/>

<View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="@color/white"
    />

<TextView
    android:id="@+id/delete"
    android:layout_width="wrap_content"
    android:paddingTop="10dp"
    android:paddingBottom="10dp"
    android:paddingRight="120dp"
    android:paddingLeft="10dp"
    android:gravity="center"
    android:clickable="true"
    android:textColor="@color/white"
    android:background="@drawable/selector_option_menu"
    android:layout_height="wrap_content"
    android:text="Delete Post"/>
</LinearLayout>

Create a custom dialog class that extends Dialog class as following and bind onclick listener in onCreate() mehtod:

public class MyDialog extends Dialog {

Context context;
public MyDialog(Context context) {
    super(context);
    // TODO Auto-generated constructor stub
    this.context = context;
}
public MyDialog(Context context, int theme){
    super(context, theme);
    this.context = context;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    this.setContentView(R.layout.dialog);

    findViewById(R.id.edit).setOnClickListener(...);
    findViewById(R.id.delte).setOnClickListener(...);
}
}

hope this will help.

Weibo
  • 1,042
  • 8
  • 18
0
 listview.setOnItemClickListener(new  OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                        long arg3) {

                     builder.setSingleChoiceItems(items,0,
                             new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog,int which) {
        // TODO Auto-generated method stub
            deger= which;
                                }
                  })
                   // Set the action buttons
                  .setPositiveButton("Editpost", new DialogInterface.OnClickListener() {
                      @Override
                      public void onClick(DialogInterface dialog, int id) {
                          //  Your code when user clicked on

                      }
                  })

                  .setNegativeButton("delet post", new DialogInterface.OnClickListener() {
                      @Override
                      public void onClick(DialogInterface dialog, int id) {
                         //    Your code when user clicked on

                      }
                  });

                }

            });
sakir
  • 3,391
  • 8
  • 34
  • 50
0

You could always use a context menu which does the same thing.

First of all register a listener on your ListView:

registerForContextMenu(myListView);

Set up your context menu options:

       @Override  
    public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {  
    super.onCreateContextMenu(menu, v, menuInfo);  
    if (v==myLIstView)
    {
        menu.setHeaderTitle("Optional title");
        menu.add(0, v.getId(), 0,"Edit post"); 
        menu.add(0, v.getId(), 0,"Delete post");
    }


    }

Capture and do something with the result:

    @Override
public boolean onContextItemSelected(MenuItem item) {
    // TODO Auto-generated method stub
    AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
    long num= info.id;
    if(item.getTitle()=="Edit post")
    {
        edit a post
    }
    else if(item.getTitle()=="Delete post"){
        delete a post
    }

    }
    return super.onContextItemSelected(item);
}
Ivan Wooll
  • 4,145
  • 3
  • 23
  • 34