开发者

android's ListView does not listen on single clicks

I'm developing an simple application on Android, where some items are shown on a list. The user may click on one, taking him to a 开发者_如何学Pythonfurther activity. Basics...

But my OnItemClickListener does not get called! I've found this very similar question, but the solution (disallow the list item view to get focus) does not work for me. However, a long click gets catched - my OnItemLongClickListener gets called. Please have a look at the following code and try it yourself. This is a simplified version of my code, showing the buggy behavior. Btw: I'm using Andriod SDK 2.0 with Eclipse 3.5.1.

package de.sacherkhoudari.listtest;

import java.util.LinkedList;
import java.util.List;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;

public class ListTest extends ListActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        final List<ListEntry> lEntries = new LinkedList<ListEntry>();
        for ( int ii = 0; ii < 10; ii++ )
            lEntries.add( new ListEntry( "Entry " + ii ) );

        setListAdapter( new ArrayAdapter<ListEntry>( this, R.layout.list_item, lEntries ) );

        ListView lv = getListView();
        lv.setTextFilterEnabled(true);

        lv.setOnItemClickListener( new OnItemClickListener() {
            public void onItemClick( AdapterView<?> parent, View view, int position, long id ) {
                Toast.makeText( ListTest.this, "ItemClick at item " + lEntries.get(position).toString(), Toast.LENGTH_LONG ).show();
            }
        });

        lv.setOnItemLongClickListener( new OnItemLongClickListener() {
            public boolean onItemLongClick( AdapterView<?> parent, View view, int position, long id ) {
                Toast.makeText( ListTest.this, "ItemLongClick at item " + lEntries.get(position).toString(), Toast.LENGTH_LONG ).show();
                return false;
            }
        });

        setContentView(lv);
    }
}

class ListEntry {
    private String name;

    public ListEntry( String s ) {
        name = s;
    }

    public String toString() {
        return name;
    }
}

So far the Java code... here comes the layout list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dp"
    android:textSize="16sp"
    android:focusable="false" >
</TextView>

Note: android:focusable="false" has no effect.

Thanks!

Sacher


When you add layouts with setContentView, views within that layouts get freshly instanciated. The ListActivity has a very simple Layout by default (read about it here), even if you don't add your own layout. So basically in your first example:

  • First you add a listener to the default ListView within the ListActivity
  • Then you throw that ListView away by using setContentView to instanciate a new layout with a new ListView
  • Then you never register a new listener to the new ListView from your new layout.

Whereas when you pull setContentView up in your code you only ever work with your own ListView and everything works as expected.


Move your setContentView(lv); right after retrieving the ListView

ListView lv = getListView();
setContentView(lv);
lv.setTextFilterEnabled(true);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜