简体   繁体   English

无法通过ListActivity使用自己的布局选择项目

[英]Can't select item using own layout with ListActivity

I was trying out a ListActivity example from the net. 我正在从网上尝试一个ListActivity示例。 I am having problem using a custom list layout. 我在使用自定义列表布局时遇到问题。 Here is my code: 这是我的代码:

public class Test1Activity extends ListActivity {
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
            "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
            "Linux", "OS/2" };
    //ArrayAdapter<String> standard_adapter = new ArrayAdapter<String> (this,android.R.layout.simple_list_item_1, values);

    ArrayAdapter<String> my_adapter = new ArrayAdapter<String>(this, R.layout.mylistlayout, R.id.label,values);
    setListAdapter(my_adapter);
}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    String item = (String) getListAdapter().getItem(position);
    Toast.makeText(this, item + " selected", Toast.LENGTH_LONG).show();
}}

onListItemClick is working fine when I use the standard adapter. 当我使用标准适配器时, onListItemClick可以正常工作。 But it doesn't work when use my adapter. 但是使用我的适配器时不起作用。 In fact I don't even see any highlighting when I click on the list items. 实际上,当我单击列表项时,甚至看不到任何突出显示的内容。 Why is this happening. 为什么会这样呢? Am I missing some code? 我是否缺少一些代码?

mylistlayout is a simple LinearLayout with a TextView (id label ) and a Button (id button1 ). mylistlayout是一个简单LinearLayoutTextView (ID label )和一个Button (ID button1 )。

<?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" >


<TextView
    android:id="@+id/label"
    android:layout_width="287dp"
    android:layout_height="wrap_content"
    android:text="items" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="32dp"
    android:padding="0dp"
    android:text="X" />

Try to add: 尝试添加:

android:focusable="false"

to your Button in the xml layout. 到xml布局中的Button

尝试将此属性添加到mylistlayout的LinearLayout中:

android:descendantFocusability="blocksDescendants"

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

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