Android的ListView控件不单点击收听 [英] android's ListView does not listen on single clicks

查看:71
本文介绍了Android的ListView控件不单点击收听的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发在Android上,一些项目都显示一个列表中的简单应用程序。用户可以点击其中一个,带他到另外的活动。基本...

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 further activity. Basics...

但我OnItemClickListener不会被调用!我发现<一href="http://stackoverflow.com/questions/1121192/android-custom-listview-unable-to-click-on-items">this非常类似的问题,但解决的办法(禁止列表项,以获得焦点)不为我工作。然而,长按被逮住 - 我OnItemLongClickListener被调用。请看看下面的code和自己尝试一下。这是一个简化版本,我的code,显示错误行为。顺便说一句:我使用的是安卓2.0的SDK与Eclipse 3.5.1

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;
    }
}

到目前为止,Java的code ......来这里的布局list_item.xml

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>

注:机器人:可调焦=假不起作用

谢谢!
扎赫尔

Thanks!
Sacher

推荐答案

将你的的setContentView(LV); 检索ListView控件之后

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

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

这篇关于Android的ListView控件不单点击收听的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆