0

我正在尝试ListActivity网上的一个例子。我在使用自定义列表布局时遇到问题。这是我的代码:

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当我使用标准适配器时工作正常。但是使用我的适配器时它不起作用。事实上,当我单击列表项时,我什至看不到任何突出显示。为什么会这样。我错过了一些代码吗?

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

4

2 回答 2

0

尝试添加:

android:focusable="false"

到您Button的 xml 布局中。

于 2012-04-29T18:09:59.987 回答
0

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

android:descendantFocusability="blocksDescendants"

于 2012-04-29T18:24:59.493 回答