简体   繁体   English

单击Android ListActivity中的项目不起作用

[英]Click on item in android ListActivity not working

I have ListActivity with custom ArrayAdapter and following layout for list items: 我有带有自定义ArrayAdapter的ListActivity和列表项的以下布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" 
android:paddingTop="5dip"
android:paddingBottom="5dip" >

<ImageView
    android:id="@+id/image"
    android:layout_width="48dip"
    android:layout_height="48dip" />

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/text1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="@color/white"
        android:textSize="24.5sp" />

    <TextView
        android:id="@+id/text2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="@color/white" 
        android:textSize="12sp"/>
</LinearLayout>

My problem is that, I am unable to click any of the list items. 我的问题是,我无法单击任何列表项。 What I've tried so far is setting focusable , clickable to false (which I found might help) to my ImageView but, unfortunately, problem remains. 到目前为止,我尝试将ImageView的focusableclickable设置为false (这对我发现可能有所帮助),但是不幸的是,问题仍然存在。 Please help, this has been bugging me all day. 请帮助,这整天困扰着我。 Thank you. 谢谢。

EDIT: OK, so William's answer helped my out with this one, only problem now is that click on item doesn't highlight it, when I change background colour to black (by setting new theme in manifest for this list activity). 编辑:好的,所以William的回答帮助我解决了这一问题,现在唯一的问题是,当我将背景颜色更改为黑色时(通过在此清单活动的清单中设置新主题),单击该项目并不会突出显示该项目。 This is my custom style for activity theme <style name="ContentsListTheme"> <item name="android:background">@color/black</item> </style> from res/values/styles/ folder. 这是我针对活动主题<style name="ContentsListTheme"> <item name="android:background">@color/black</item> </style>自定义<style name="ContentsListTheme"> <item name="android:background">@color/black</item> </style>来自res/values/styles/文件夹。

To make it clear, once again, default ListActivity background colour is white. 为了清楚起见,默认的ListActivity默认背景颜色是白色。 I want it black. 我要黑色。 When I apply the style from above, list activity item highlighting when clicked is disabled. 当我从上方应用样式时,将禁用单击时突出显示列表活动项。 How can I enable it again, to the default highlighting colour? 如何再次将其启用为默认突出显示颜色?

In your oncreate try this 在您的oncreate尝试一下

ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
          public void onItemClick(AdapterView<?> parent, View view,
    int position, long id) {
});

OR you can write this function which will handle the clicking on the list items 或者,您可以编写此函数来处理列表项的单击

protected void onListItemClick(ListView l, View v, int position, long id) {}

I have this working code, and I think in your Java code is the problem. 我有此工作代码,并且我认为您的Java代码中存在问题。

public class NewsListActivity extends Activity implements OnItemClickListener

... ...

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list);
        listView = (ListView) findViewById(R.id.listView);
        listView.setAdapter(new NewsDataArrayAdapter(NewsListActivity.this, headlines));
        listView.setOnItemClickListener(this);

... ...

public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
        if (v == null) {
            return;
        }

try this 尝试这个

ListView lv=(ListView)findViewById(yourlistview);
Lv.setonItemClickListener(this);

public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    paret.getItemAtPositon(position);
    //this returns you an object cast it into the same object you passed to your adapter cast to string if values passed were string
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM