2012-01-05 8 views
3

Hy、 ListViewの1つの項目にアイコンと2行を追加したいとします。最初の値になり、2番目はvalues2ListView Androidのアイテムごとの2行とアイコン

String[] values = new String[] { "Android", "iPhone", "WindowsMobile", 
      "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X", 
      "Linux", "OS/2" }; 
String[] values2 = new String[] `enter code here`{ "Android2", "iPhone2",  "WindowsMobile2", 
      "Blackberry2", "WebOS2", "Ubuntu2", "Windows72", "Max OS X2", 
      "Linux2", "OS/22" }; 

リストのための私のxmlは次のようになります。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" > 

<ImageView 
    android:id="@+id/icon" 
    android:layout_width="22px" 
    android:layout_height="22px" 
    android:layout_marginLeft="4px" 
    android:layout_marginRight="10px" 
    android:layout_marginTop="4px" 
    android:src="@drawable/ic_launcher" > 
</ImageView> 

<TextView 
    android:id="@+id/label" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@+id/label" 
    android:textSize="20px" /> 

<TextView 
    android:id="@+id/label2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@+id/label" /> 

</LinearLayout> 

そして、私のコードは次のとおりです:各行について

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

<ListView 
    android:id="@+id/list" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" > 
</ListView> 

</LinearLayout> 

私のXMLがある

public class listaNoua 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" }; 
    String[] values2 = new String[] { "Android2", "iPhone2", "WindowsMobile2", 
      "Blackberry2", "WebOS2", "Ubuntu2", "Windows72", "Max OS X2", 
      "Linux2", "OS/22" }; 

    ArrayAdapter<String> adapter = new ArrayAdapter<String> (this,R.layout.rowlayout, R.id.label2, values2); 
    setListAdapter(adapter); 

} 

私が追加した場合:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.rowlayout, R.id.label2, values2 , R.id.label, values); 

コードが

誰が私を助けることができる動作しませんか?

+0

はそれが唯一のカーソル上で動作しますが、より複雑な、どこかmatrixcursorの使用を暗示することになる、((基本的にIDのリストと項目のリストを結合するためのsimplelistadapterを参照) – njzk2

答えて

5

リストカスタムアダプターを使用する必要があります。あなたは私はあなたがあなたの行のXMLでのLinearLayoutのためのオリエンテーションを忘れると思うここ

http://www.learn-android.com/2011/11/22/lots-of-lists-custom-adapter/

良いいくつかの例を見つけることができます。問題は、直線的なレイアウトがデフォルトで「水平」の向きになっていることです。行は、リスト内の3つすべての項目を表示するために、以下のようにする必要があります。あなたはこのためにSimpleAdapterを使用することができます

<ImageView 
    android:id="@+id/icon" 
    android:layout_width="22dp" 
    android:layout_height="22dp" 
    android:layout_marginLeft="4dp" 
    android:layout_marginRight="10dp" 
    android:layout_marginTop="4dp" 
    android:src="@drawable/ic_launcher" > 
</ImageView> 

<LinearLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/label" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@+id/label" 
     android:textSize="20sp" /> 

    <TextView 
     android:id="@+id/label2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@+id/label" /> 
</LinearLayout> 

+0

それは私のコンストラクタArrayAdapterをエラーが発生します(listaNoua、int、int、String []、int、String [])は定義されておらず、最初のテキストはfalseで2番目のテキストはそれが必要なものです(android2、iphone 2) – Razvan

+0

次に、この問題を解決するためのアダプタ – Arslan

+0

どうすればよいですか?すべての試みは失敗しました – Razvan

5

List<Map<String, ?>> data = new ArrayList<Map<String, ?>>(); 
    data.add(createRow("Android", "Android2")); 
    data.add(createRow("iPhone", "iPhone")); 

    String[] from = {"value1", "value2"}; 
    int[] to = {R.id.label, R.id.label2}; 

    SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.item_layout, from, to); 

と機能

private Map<String, ?> createRow(String value1, String value2) { 
    Map<String, String> row = new HashMap<String, String>(); 
    row.put("value1", value1); 
    row.put("value2", value2); 
    return row; 
} 
2

行を作成するためのAndroidは、すでにこのための事前に定義されたXMLレイアウトが付属していますがsimple_list_item_2と呼ばれるので、独自のアダプタを作成したり、独自のレイアウトファイルを記述する必要はありません。

あなただけgetViewをオーバーライドして、既存のsimple_list_item_2レイアウトですでに事前定義されたandroid.R.id.text1android.R.id.text2 TextViewsの値を設定する必要があります。

ArrayAdapter<MyObject> adapter = new ArrayAdapter<MyObject>(this, android.R.layout.simple_list_item_2, android.R.id.text1, result) { 
    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     View view = super.getView(position, convertView, parent); 
     TextView text1 = (TextView) view.findViewById(android.R.id.text1); 
     TextView text2 = (TextView) view.findViewById(android.R.id.text2); 

     text1.setText(result.get(position).OperatingSystem); 
     text2.setText(result.get(position).Platform); 
     return view; 
    } 
}; 
+0

ありがとう、それは私のためのトリックでした。'result'は型MyObject []'であり、追加するオブジェクトを保持しています。あるいは、このパラメータを完全に削除し、個々のオブジェクトを 'adapter.add()'または 'adapter.addAll()'で追加することもできます。 'getView()'の内部では、 'getItem(position)'でリストアイテムにアクセスできます。 – user149408

関連する問題