0

From MainActivity:

public class MainActivity extends ListActivity implements AdapterView.OnItemLongClickListener {
    private DataSourceSql mDataSourceSql;
    protected ArrayList<String> mProfileNames;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mDataSourceSql = new DataSourceSql(MainActivity.this);
        mNames = new ArrayList<String>();

        //this part here
        ListView lv = (ListView) findViewById(android.R.id.list);
        lv.setLongClickable(true);
    }

The id of the ListView in this case is set in the XML as android:id="@android:id/list".

And then later on in the same activity:

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    Toast.makeText(this, mNames.get(position), Toast.LENGTH_SHORT).show();
}

@Override
public boolean onItemLongClick(AdapterView<?> l, View v, final int position, long id) {
    Toast.makeText(this, "long clicked: " + mNames.get(position), Toast.LENGTH_LONG).show();
    return true;
}

But when I long-press an item in the list, the only thing that triggers is onListItemClick. I never get the message with the long click.

3 Answers3

1

You shouldn't implement an interface like AdapterView.OnItemLongClickListener directly in your onCreate().. Just use your old way (Your class implement this interface) and with each method override, you have to write like this:

lv.setOnItemLongClickListener(this);
lv.setonListItemLongClickListener(this);
Bui Quang Huy
  • 1,784
  • 2
  • 17
  • 50
0

I think I figured it out, but someone please correct me if I'm missing something.

I removed the "implements..." thing and then added the following to the onCreate method:

lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener
                () {
            @Override
            public boolean onItemLongClick(AdapterView<?> av, View v, int
                    pos, long id) {
                onListItemLongClick(v, pos, id);
                return false;
            }
        });

Although I am not sure if I should be returning true or false there.

I also changed the long click function down below to

public boolean onListItemLongClick(View v, final int position, long id) {
    Toast.makeText(this, "long clicked: " + mNames.get(position), Toast.LENGTH_LONG).show();
    return true;
}

Edit:

Quicker approach is to just add lv.setOnItemLongClickListener(this); to my onCreate method in the OP.

0

Use This. Your Problem will Solve.

 listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(MainActivity.this, "Click", Toast.LENGTH_SHORT).show();
            }
        });
        listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {

                Toast.makeText(MainActivity.this, "Long Click", Toast.LENGTH_SHORT).show();
                return true;
            }
        });
Mehul Santoki
  • 1,208
  • 1
  • 12
  • 25