Question

This is my code...

TextView tvItem = (TextView) findViewById(R.id.tvItem);

ListView list = (ListView) findViewById(R.id.list);

tvItem.setLongClickable(true);

tvItem.setClickable(true);

list.setOnItemLongClickListener(new OnItemLongClickListener(){


    @Override
    public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2,
            long arg3) {
        AlertDialog.Builder alert=new AlertDialog.Builder(MainActivity.this);
        alert.setMessage("What do you want to do?");
        alert.setPositiveButton("Rename", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub

            }
        });
        alert.setNegativeButton("Delete", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
            dialog.cancel();
            }
        });
        alert.setNeutralButton("Cancel", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub

            }
        });
        AlertDialog ale=alert.create();
        ale.show();
        return false;
    }

As I said, OnLongClickListener does not work.

Was it helpful?

Solution

try it within Adapter something like this:

public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
 if (view == null) {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.list_view_inspection, null);
        }

 view.setOnLongClickListener(new OnLongClickListener() {    
                    @Override
                    public boolean onLongClick(View v) {
                        Dialog(param 1, param2); //your alert box method or something
                        return true;
                    }
                });
}

OTHER TIPS

You should set setLongClickable(true); on list like: list.setLongClickable(true);

You have to enable the longclickable

list.setLongClickable(true);

you called this method on TextView Object.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top