0

I have an activity_main layout with a ListView.
I need to fill my ListView in MainActivity.

MainActivity doesn't extend ListActivity, because I use not only a ListView in my layout, I use other elements.

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_top_level);
    ListView listView = (ListView) findViewById(R.id.mylist);
}

Item.items returns an array which I need to add to the ListView.
How Can I do that?

I know that I have to use an ArrayAdapter. But I can't do it properly.
It works for me, but I don't understand using android.R.layout.simple_list_item_1.

ArrayAdapter<Item> itemsAdapter = 
    new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, Item.items);
listView.setAdapter(itemsAdapter);
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
jisecayeyo
  • 250
  • 1
  • 9

1 Answers1

0

Regarding your question, you have two options.

1) Something really simple that'll toString() your Item objects in each row.

ArrayAdapter<Item> adapter = new ArrayAdapter<Item>(
    MainActivity.this, // Context
    android.R.layout.simple_list_item_1, // An XML file for your items
    Item.items         // The Item[] array
);
listView.setAdapter(adapter);

2) Define your own adapter class with your own XML


If you know how to use ListActivity, you can still show other elements, by the way.

Read the documentation.

if you desire, you can customize the screen layout by setting your own view layout with setContentView() in onCreate(). To do this, your own view MUST contain a ListView object with the id "@android:id/list"

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245