Aditya Chawla
Aditya Chawla

Reputation: 107

Want to have a list view in an alert dialog box with both onClick listener and onLongClickListener

To create an Alert Dialog box with a list view, I used the following piece of code:

                ArrayList<String> namesAL = dbHandler.getArrayListOFnames();
                final ListAdapter m_Adapter = new ArrayAdapter<String>(fragment_console.this,android.R.layout.simple_expandable_list_item_1, namesAL);


                builderSingle.setAdapter(
                        m_Adapter,
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {                                     
                                destloc = getLocLatLng(which);
                                destlat = destloc.latitude;
                                destlng = destloc.longitude;
                                gotoLocation(destlat, destlng, 14);
                                if (marker != null) {
                                    marker.remove();
                                }
                                if (circle != null){
                                    circle.remove();
                                    circle = null;
                                }

                                MarkerOptions options = new MarkerOptions()
                                        .title("Your destination")
                                        .position(destloc)
                                        .position(destloc)
                                        .icon(BitmapDescriptorFactory.fromResource(R.drawable.dest_marker));

                                marker = map.addMarker(options);
                                onDestinationChanged();
                                dialog.cancel();                                   }
                        });
                builderSingle.show();

But this restricts me to use only use OnClickListener, there is no option of Long click listener. I need a long click listener too so that the user can delete an entry from the list I provide (which is actually created by user only). How to do this?

Upvotes: 1

Views: 1499

Answers (4)

Gibolt
Gibolt

Reputation: 47297

Simple way to add item long press handler

AlertDialog dialog = new Builder(mContext)
    .setTitle(HISTORY_LIST_DIALOG_TITLE)
    .setAdapter(historyAdapter, (dialog, index) -> {
       // OnItemClickListener
    })
    .create();

// Long click listener must be added after builder creation
dialog.getListView().setOnItemLongClickListener((parent, view, index, id) -> {
  // Handle item long click here
  dialog.dismiss(); // Not required, but recommended
  return true;
});

dialog.show();

Upvotes: 0

user6547359
user6547359

Reputation: 204

After building the dialog, before showing it, you can do this:

alertDialog.getListView().setOnLongClickListener(...);

https://developer.android.com/reference/android/app/AlertDialog.html#getListView()

Edit: adding more code to clarify for the op

Here is the code to add the listener. In order to get the right object from the list you should add the object itself as a tag to the view.

//[your code from above here...]

        AlertDialog alertDialog = builderSingle.create();
        alertDialog.getListView().setOnLongClickListener(new OnLongClickListener() {

            @Override
            public boolean onLongClick(View v) {
                //your removeing code here
                YourObject yObj = (YourObject) v.getTag();
                yourList.renmove(yObj);
                return true;
            }
        });
        alertDialog.show();

Upvotes: 0

Android Surya
Android Surya

Reputation: 544

            // TODO Auto-generated method stub
            AlertDialog.Builder alertBuilder = new AlertDialog.Builder(
                    ListAlertDailog.this);
            alertBuilder.setIcon(R.drawable.ic_launcher);
            alertBuilder.setTitle("Select Mobile OS:-");
            final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
                    ListAlertDailog.this,
                    android.R.layout.select_dialog_item);
            arrayAdapter.add("Android");
            arrayAdapter.add("IOS");
            arrayAdapter.add("Windows");
            arrayAdapter.add("Bada");
            arrayAdapter.add("BlackBerry OS");
            arrayAdapter.add("Symbian OS");

            alertBuilder.setNegativeButton("Cancle",
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog,
                                int which) {
                            dialog.dismiss();
                        }
                    });

            alertBuilder.setAdapter(arrayAdapter,
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog,
                                int which) {
                            String strOS = arrayAdapter.getItem(which);
                            Toast.makeText(getApplicationContext(),
                                    "On click selected " + strOS, Toast.LENGTH_SHORT)
                                    .show();
                            dialog.dismiss();
                        }
                    });

            final AlertDialog alertDialog = alertBuilder.create();
            alertDialog.setOnShowListener(new OnShowListener() {

                @Override
                public void onShow(DialogInterface dialog) {
                    // TODO Auto-generated method stub
                    ListView listView = alertDialog.getListView(); 
                    listView.setOnItemLongClickListener(new OnItemLongClickListener() {

                        @Override
                        public boolean onItemLongClick(
                                AdapterView<?> parent, View view,
                                int position, long id) {
                            // TODO Auto-generated method stub
                            String strOS = arrayAdapter.getItem(position);
                            Toast.makeText(getApplicationContext(),
                                    "Long Press - Deleted Entry " + strOS,
                                    Toast.LENGTH_SHORT).show();
                            alertDialog.dismiss();
                            return true;
                        }
                    });
                }
            });

            alertDialog.show();

Upvotes: 1

stasbar
stasbar

Reputation: 105

You need to make your own DialogFragment with ListView, but the best will be RecyclerView.

Example:

public class MyDialogFragment extends DialogFragment {
    private RecyclerView mRecyclerView;
    private MyRecyclerAdapter adapter;
    // this method create view for your Dialog
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
          //inflate layout with recycler view
         View v = inflater.inflate(R.layout.fragment_dialog, container, false);
        mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        //setadapter
        CustomAdapter adapter = new MyRecyclerAdapter(context, customList);
            mRecyclerView.setAdapter(adapter);
         //get your recycler view and populate it.
         return v;
    }
}

here you can find how to create RecyclerView adapter, http://www.androidhive.info/2016/01/android-working-with-recycler-view/

You can show this fragment like so:

MyDialogFragment dialogFragment = new MyDialogFragment();
dialogFragment.show(getFragmentManager(), "dialogFragment");

Upvotes: 0

Related Questions