5

がありListView、リストのサブアイテムを表示できません...どちらか一方のみが表示され、両方は表示されません。下から番号を削除すると、文字列(タイトル)が表示されます。

アイテムとサブアイテムの両方を表示したい。

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    this.loadData();

    String []titles_str = titles.toArray(new String[titles.toArray().length]);
    this.setListAdapter(new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_2, android.R.id.text1, titles_str));

    Number []subtitles_str = lengths.toArray(new Number[lengths.toArray().length]);
    this.setListAdapter(new ArrayAdapter<Number>(this,
            android.R.layout.simple_list_item_2, android.R.id.text2, subtitles_str));
}
4

1 に答える 1

5

「this.setListAdapter」を 2 回使用している場合、2 回目の「this.setListAdapter」のみが反映されます。

あなたの要件のために、使用してください

1)custom arrayadapter<customObject> 
      or 
2)simpleadapter.

1) カスタム arrayadapter の使用http://devtut.wordpress.com/2011/06/09/custom-arrayadapter-for-a-listview-android/を確認してください

2) SimpleAdapter の使用:

List<Map<String, String>> data = new ArrayList<Map<String, String>>();
for (int i=0; i<titles_str.length; i++) {
    Map<String, String> datum = new HashMap<String, String>(2);
    datum.put("title", titles_str[i]);
    datum.put("subtitle", String.valueof(subtitles_str[i]));
    data.add(datum);
}
SimpleAdapter adapter = new SimpleAdapter(this, data,
                                      android.R.layout.simple_list_item_2,
                                      new String[] {"title", "subtitle"},
                                      new int[] {android.R.id.text1,
                                                 android.R.id.text2});
this.setListAdapter(adapter);
于 2013-05-09T10:04:26.317 に答える