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);

}

TextView が 2 つしかないカスタム レイアウト (R.layout.showdata) を使用すると、値を確認できますが、選択範囲のどこかに触れるのではなく、表示されているテキストに触れて仕事をする必要があります。

だから私は 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

この質問は古いことは知っていますが、Google からここにたどり着きました。同様の問題があったので、答えがあると思います。

Androidのものではなく、配列でカスタムTextViewを参照しています。int[] to代わりに次を使用する必要があります。

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

どれでも使用できますが、一貫性がなければなりません。

私はXMLで使用android:id="@+id:text1"していました(ID「text1」で新しいTextViewを作成しています)がandroid.R.id.text1、Javaクラスで使用していたため、CursorAdaptor存在しないビューに情報を設定していたため、まさにその結果が得られました:すべての行が含まれていないリストビュー文章。

于 2016-01-16T09:42:13.097 に答える