2010-12-14 11 views
2

私はアンドロイドの開発に新しく、太字のヘッダー
と各項目の小さい説明を持つリストを作成しようとしています。
http://i.stack.imgur.com/UVqzq.png
これは私がして起動する必要がXMLである:(申し訳ありませんまだ画像を投稿することはできません)ここに示したような twoLineListItemの適切な使用方法

<LinearLayout android:layout_width="fill_parent" 
android:layout_height="fill_parent"> 
<TwoLineListItem android:layout_width="fill_parent" 
android:layout_height="wrap_content" android:mode="twoLine"> 
<TextView android:textAppearance="?android:attr/textAppearanceLarge" 
android:id="@android:id/text1" android:layout_width="fill_parent" 
android:layout_height="wrap_content"/> 
<TextView android:layout_below="@android:id/text1" 
android:textAppearance="?android:attr/textAppearanceSmall" 
android:layout_alignLeft="@android:id/text1" android:layout_width="fill_parent" 
android:layout_height="wrap_content" android:id="@android:id/text2" /> 
</TwoLineListItem> 
</LinearLayout> 

は、配列からリストを移入する方法はあります別のXMLファイルで?そうでない場合、Javaコードからこのようなリストをどのように埋め込むのでしょうか?私が言ったように、私は開発には新しいので、おそらく私は離れています。入力をありがとう!

答えて

1

は、私は同様の実装を持っているが、代わりに2行のリストビューの、私は3つのラインを持っているリストビューhttp://developer.android.com/reference/android/widget/ListView.html

+0

私はアプリケーションの別のセクションでリストビューを持っていましたが、リスト内のアイテムごとに1つのタイプのテキストしか表示できないという印象を受けました。入力いただきありがとうございます、私はそれをさらに調べます。 – JP2014

+0

それはインスタンス化する方法によって異なります。 BaseAdapterクラスをサブクラス化して、リストビューに必要なビューをフィードできます。オンラインでカスタムリストビューの例が何百もあります。 – Falmarri

0

を使用しています。

これはXMLである:

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    android:orientation="vertical" > 
    <TextView 
     android:id="@+id/text1" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:textColor="#111111" 
     android:textSize="17sp" 
     android:textStyle="bold" 
     android:text="PROJECT NAME" 
     android:typeface="monospace" 
     android:paddingBottom="2dp" 
     android:paddingTop="2dp" 
     android:paddingLeft="5dp" 
     android:paddingRight="5dp" 
     android:gravity="left|center" > 
    </TextView> 
    <TextView 
     android:id="@+id/text2" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:textColor="#111111" 
     android:text="Selected Type" 
     android:textSize="16sp" 
     android:paddingBottom="1dp" 
     android:paddingTop="1dp" 
     android:paddingLeft="5dp" 
     android:paddingRight="5dp" > 
    </TextView> 
    <TextView 
     android:id="@+id/text3" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:textColor="#111111" 
     android:textSize="16sp" 
     android:paddingTop="1dp" 
     android:paddingBottom="1dp" 
     android:paddingLeft="5dp" 
     android:paddingRight="5dp" 
     android:text="Project Description" > 
    </TextView> 
</LinearLayout> 

これはDBから返されたデータを用いて行を充填するためのJavaコードです。このコードは、のonCreate()メソッドにあります。オブジェクトの配列の場合は

String[] fields = new String[] { db.TABLE_PRJ_NAME, db.TABLE_PRJ_TYPE, db.TABLE_PRJ_DESC }; 
    int[] views = new int[] { R.id.text1, R.id.text2, R.id.text3 }; 

    c = db.getAllProjects(); 
    startManagingCursor(c); 

    // Set the ListView 
    SimpleCursorAdapter prjName = new SimpleCursorAdapter(
      this, 
      R.layout.project_list_rows, 
      //android.R.layout.simple_list_item_1, 
      c, fields, views); 
    setListAdapter(prjName); 
+0

c、setListAdapterメソッドを定義していません。この解決策は不完全です。 – coolcool1994

1

、あなただけの独自のアダプタを実装する必要があります。 TwoLineListItemやその他のカスタムレイアウトでも動作します。例えば

、私は以下のタイプの項目のリストを持っている場合:次に

public class ProfileItem { 
    public String host, name, tag; 

    public ProfileItem(String host, String name, String tag) { 
     this.host = host; 
     this.name = name; 
     this.tag = tag; 
    } 

    @Override 
    public String toString() { 
     return name+"#"+tag; 
    } 
} 

I以下のアダプタを作成:

public class ProfileItemAdapter extends ArrayAdapter<ProfileItem> { 

private Context context; 
private int layoutResourceId; 
private List<ProfileItem> objects = null; 

public ProfileItemAdapter(Context context, int layoutResourceId, List<ProfileItem> objects) { 
    super(context, layoutResourceId, objects); 
    this.context = context; 
    this.layoutResourceId = layoutResourceId; 
    this.objects = objects; 
} 

public View getView(int position, View convertView, ViewGroup parent) { 
    View v = convertView; 
    if(v == null) 
    { 
     LayoutInflater inflater = ((Activity)context).getLayoutInflater(); 
     v = inflater.inflate(layoutResourceId, parent, false); 
    } 

    ProfileItem item = objects.get(position); 
    TextView titletext = (TextView)v.findViewById(android.R.id.text1); 
    titletext.setText(item.toString()); 
    TextView mainsmalltext = (TextView)v.findViewById(android.R.id.text2); 
    mainsmalltext.setText(item.host); 
    return v; 
} 

}

その後、すべての場所であるが、私の活動(または断片)で、私はちょうどonCreate方法でこのアダプタを設定する必要があります:

setListAdapter(new ProfileItemAdapter(getActivity(), 
      android.R.layout.two_line_list_item, // or my own custom layout 
      ProfileListContent.ITEMS)); 
+0

いい例、ありがとう! – Mgamerz