1

アイテムのリストを作成したいのですが、各リスト アイテムには 4 つのテキストが必要です。そして、4 つすべてがそのリストの異なる 4 つのコーナーに配置されます。私はxmlファイルで行うことができます。しかし、アンドロイドのJavaからどうやってそれを行うことができるのかわかりません。データベースからそれらのリストを作成する必要があるためです。私の問題をよりよく理解するために、私のxmlコードを以下に示します。誰か助けてください..?

                <?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="match_parent"
android:orientation="vertical" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/tvb"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center_vertical|center_horizontal"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="left"
            android:text="Left Aligned"
            android:textSize="25dp"
            android:textStyle="bold" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="right"
            android:text="Right Aligned" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center_vertical|center_horizontal"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="left"
            android:text="Left Aligned" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="right"
            android:text="Right Aligned" />
    </LinearLayout>
</LinearLayout>

4

1 に答える 1

1

最初にこれを見てください: http://www.vogella.com/articles/AndroidListView/article.html

一般的な の包括的なガイドとListView、独自のカスタム を作成する方法Adapter

getViewこれを見たときに、カスタムのメソッドを見たいと思うかもしれません。Adapterこれがすべての楽しみがある場所だからです;-)

このgetViewメソッドはListView、リスト内の各アイテムに対して呼び出されます。

ここで、XML レイアウトを拡張し、レイアウト ファイルのテキストを設定します。

getViewメソッドがどのように見えるかの例は、次のようになります。

public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    if(convertView == null) {
        convertView = inflater.inflate(R.layout.myrowlayout, parent, false);
    }

    TextView textView1 = (TextView) rowView.findViewById(R.id.textview1);
    TextView textView2 = (TextView) rowView.findViewById(R.id.textview2);
    TextView textView3 = (TextView) rowView.findViewById(R.id.textview3);
    TextView textView4 = (TextView) rowView.findViewById(R.id.textview4);

    // Set the text of the textViews accordingly to where you are in the list.
    // An example
    if(position % 2 == 0) {
        // If even position in the list, set the first TextView
        textView1.setText("This text is only shown for even positions in your list");            
    }

    return rowView;
}

Collection最初に に提供した を見てAdapter、特定のレイアウトを適用したい特別なアイテムがあるかどうかを確認することもできます。

これは、たとえば特定の画面サイズにも適用できます。タブレットでは、たとえばのGridView代わりに を使用する必要がありListViewます (ほとんどの場合)。

トピック外: 私のコード例は、アイテムの読み込みには決して効率的ではありません。効率的なコードが必要な場合は、たとえばViewHolder パターンを確認する必要があります。

これが途中で役立つことを願っています:-)

于 2013-10-03T14:34:34.733 に答える