不执行的ListView setOnItemClickListener [英] ListView setOnItemClickListener not executing

查看:142
本文介绍了不执行的ListView setOnItemClickListener的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的Andr​​oid和,虽然我得到这个更简单的项目之前的工作,我不能为我的生命弄清楚到底是怎么回事。

I am new to android and although I got this working before on a simpler project, I can not for the life of me figure out what is going on.

下面是code为我ListActivity类:

Here is the code for my ListActivity class:

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

        // Get rid of the default title bar
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);

        Bundle extras = this.getIntent().getExtras();
        if (extras != null)
        {
            String[] results = extras.getStringArray("results_to_display");
            this.setListAdapter(new ArrayAdapter<String>(this, R.layout.result_list_item, results));

            ListView listView = this.getListView();
            listView.setTextFilterEnabled(true);
            Toast.makeText(getApplicationContext(), "hello there", Toast.LENGTH_LONG).show();

            listView.setOnItemClickListener(new OnItemClickListener() {
                public void onItemClick(AdapterView<?> parent, View view, int position, long id)
                {
                    Log.d(ACTIVITY_SERVICE, "here now");
                    Toast.makeText(getApplicationContext(), ((TextView) view).getText(), Toast.LENGTH_SHORT).show();
                }
            });
        }

        this.setContentView(R.layout.result_list);
    }
}

我能够获取列表以显示我想要的内容,但是,在单击某个项目时,它看起来像onItemClick没有得到执行。我已经把一个断点在那里。

I am able to get the list to display the content that I want, however, when clicking on an item it looks like onItemClick does not get executed. I have put a breakpoint in there as well.

下面是我的布局文件(不知道这是否有差别)

Here is my layout file (not sure if it makes a difference)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/contentContainer"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFFFFF">
    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"/> 
</LinearLayout>

任何帮助将是AP preciated!

Any help would be appreciated!

推荐答案

在一个ListActivity您可以覆盖onListItemClick:

In a ListActivity you can override onListItemClick:

public class ResultsActivity extends ListActivity {
    protected void onListItemClick (ListView l, View v, int position, long id){

    }
}

修改

我发现这个问题。您设置的onCreate年底布局(带的setContentView)。您设置的OnItemClickListener ListView的是不是你所看到的ListView控件。

I found the problem. You set the layout (with setContentView) at the end of onCreate. The ListView you are setting the OnItemClickListener on is not the ListView you are seeing.

在ListActivity创建自己的内容查看,如果你不自己设置的。所以你添加的OnClickListener的默认ListView和在用一个新的,因为的setContentView被称为默认的被替换的onCreate的结束。

The ListActivity creates its own contentView if you do not set one yourself. So you added the OnClickListener on the "default" ListView and at the end of onCreate the default one was replaced with a new one because setContentView was called.

其实你可以创建一个ListActivity不设置内容查看。

Actually one can create a ListActivity without setting the contentView.

要解决这个问题举的setContentView在的onCreate的顶部。

To solve the problem move setContentView at the top of onCreate.

这篇关于不执行的ListView setOnItemClickListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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