1

我想通过滚动功能向用户显示我的数据库的一些内容。

所以我扩展了 ListActivity 并使用了 SimpleCursorAdapter。问题是我看不到被取代的价值观。

private void fillData() {
 // Get all of the rows from the database and create the item list
ListData = data.findAllDbValues();
startManagingCursor(ListData);

// Create an array to specify the fields we want to display in the list
String[] from = new String[]{Data.ID, Data.DESC};

// and an array of the fields we want to bind those fields to 
int[] to = new int[]{R.id.text1, R.id.text2};

// Now create a simple cursor adapter and set it to display 
ListAdapter la = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_2, ListData, from, to);//R.layout.showdata
setListAdapter(la);

}

如果我使用自定义布局(R.layout.showdata),它只有两个 TextView,我可以看到这些值,但我必须触摸显示的文本才能完成我的工作,而不是仅仅触摸选择的任何地方。

所以我想使用一个 simple_list_item_2 这样我就可以拥有这个能力。有什么想法为什么我看不到这些值,或者我怎样才能使我的自定义布局产生与 simple_list 相同的能力?

4

2 回答 2

1

这对我有用:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android_orientation="horizontal" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:paddingLeft="4dip"
android:paddingRight="5dp">
<TextView android:id="@+id/productRow"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content" android:layout_height="wrap_content" />
</LinearLayout>

诀窍是将TextView行包装在一个LinearLayout. 这让我可以单击列表元素上的任意位置来选择它。

于 2011-04-29T14:38:50.223 回答
0

我知道这个问题很老,但我是通过谷歌来到这里的,我想我有答案,因为我遇到了类似的问题。

TextView您在数组中引用自定义int[] to,而不是 Android 数组。您必须改用:

// and an array of the fields we want to bind those fields to 
int[] to = new int[]{android.R.id.text1, android.R.id.text2};

只需检查 XML 中的 id 和您的类中的 id 是否相同。当您使用自定义视图时,您的类中的引用是 R.id.text1和您的布局中android:id="@+id:text1"的引用,但是当您使用 Android 资源时,您的类中的引用是android.R.id.text1和您的布局中的引用android:id="@android:id/text1"

您可以使用其中任何一个,但必须保持一致。

android:id="@+id:text1"在我的 XML 中使用(创建一个 ID 为“text1”的新 TextView),但android.R.id.text1在我的 Java 类中使用,所以将CursorAdaptor信息设置在一个不存在的视图中,我得到的结果正是:一个列表视图,所有行都没有文本。

于 2016-01-16T09:42:13.097 回答