Mike
Mike

Reputation: 415

ListView onClick works in emulator but not on touch phone

can anyone tell me why this does not work on my LG400f touch phone but works fine with a mouse click in the emulator?

Code

ListView listView = (ListView) findViewById(R.id.listContactsList);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_contacts,  R.id.listContactsView, values);
listView.setAdapter(adapter);
listView.setClickable(true);
listView.setFocusable(true);
listView.setFocusableInTouchMode(true);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

@Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    Object selection = (Object) parent.getItemAtPosition(position);
    Toast.makeText(getApplicationContext(), selection.toString(), Toast.LENGTH_LONG).show();
    // Toast is triggered with mouse click in emulator but not touch in phone

XML

<ListView
    android:id="@+id/listContactsList"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    tools:context=".ListContacts" />

Upvotes: 0

Views: 632

Answers (2)

Samuel Quiroz
Samuel Quiroz

Reputation: 351

On the Listview in the xml check the touchscreenBlocksFocus = true

Upvotes: 1

Mike
Mike

Reputation: 415

Thank you to the people who responded to this question.

I have not exactly re-solved this issue directly but have coded a variation that works.

Firstly my previous class was defined like so: ListContacts extends Activity and the code was as I displayed it in the original question.

I now have defined my class ListContacts extends ListActivity and changed the code (which works) like this:

First delete setContentView(R.layout.xxxx) from onCreate() and the textview in the XML file.

Code:

this.setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, values));
ListView listView = getListView();
listView.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Object selection = (Object) parent.getItemAtPosition(position);
        Toast.makeText(getApplicationContext(), selection.toString(), Toast.LENGTH_LONG).show();

Upvotes: 0

Related Questions