2

Is it possible to create a ListView and an adapter, add items to it programmatically without XML? If so, how?

CJBS
  • 13,354
  • 5
  • 76
  • 124
Fcps
  • 185
  • 3
  • 18
  • 2
    Yes, there is a way to do it. – Cruceo Mar 20 '14 at 19:11
  • @Guardanis You know how I can do ? – Fcps Mar 20 '14 at 19:23
  • ListView myListView = new ListView(context); Aside from that, this site is to help you fix problems, not program an entire thing for you. If you have a specific question or problem, show us what you've done and we can help you fix it. We can't just magically predict what you've done or build the whole thing for you. But here's a .0005 second Google search for you: http://stackoverflow.com/a/12795518/1426565 – Cruceo Mar 20 '14 at 19:51

2 Answers2

3

First, you need to initialize the ListView, then add it to the layout of your activity. You can read about doing this here: How to create a RelativeLayout programmatically with two buttons one on top of the other?

You would create a Layout, then create a ListView and add it to that layout.

Next, you need to create a custom adapter. You can read more about that here: http://developer.android.com/reference/android/widget/ListAdapter.html

For example, you could use an ArrayAdapter. http://developer.android.com/reference/android/widget/ArrayAdapter.html

An ArrayAdapter has a method

public View getView (int pos, View convertView, ViewGroup parent)

This method returns a view to display at position pos in the list. In this method, you would first see if you already have a view for this position (by checking convertView). If you don't, you would generate a new View however you want. You could do this in code, or better yet, you could use an XML file and inflate a view.

After you get your adapter setup, you call setAdapter on your ListView.

So for example, it might look something like this:

@Override
public void onCreate(Bundle savedInstanceState){
    LinearLayout ll = new LinearLayout(this);
    ListView lv = new ListView(this);
    YourAdapter adapter = new YourAdapter(<parameters here>);
    lv.setAdapter(adapter);

    ll.addView(lv, LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
    setContentView(ll);
}
Community
  • 1
  • 1
Jon Nale
  • 206
  • 1
  • 2
  • 4
    Before I leave this page, I must say: That has absolutely -NOTHING- to do with the question he asked... – Cruceo Mar 20 '14 at 19:52
  • @Guardanis - His question starts with "I need to create a listview programmatically with her adapter and without xml". Part of doing this would be to create an adapter and specify the View that it returns. Sure, this doesn't actually create the ListView, but it doesn't have "NOTHING" to do with the question. It specifically answers how to create list items programatically. You need an adapter, and you can programmatically create a view and return it in the getView method. – Jon Nale Mar 20 '14 at 19:55
  • 1
    Yeah, it starts with "I need to create a listview programmatically" and you didn't answer anything related to that... It doesn't ask for how to create an Adapter. – Cruceo Mar 20 '14 at 19:58
  • "...with her adapter and without xml" I gave him a basic walkthrough of _part_ of his question. Which goes back to my point: this has SOMETHING to do with the question, it just isn't the complete answer. There are plenty of resources online that will tell you how to add a View, such as a ListView, to a layout programatically. – Jon Nale Mar 20 '14 at 20:00
  • I added more for clarification. – Jon Nale Mar 20 '14 at 20:06
0

Assuming you have knowledge about Fragments and ArrayAdapters, the simplest way is to extend ListFragment and add your adapter using setListAdapter(ArrayAdaper/some custom adapter object) convenience method. If you are not using the convenience method, you should use ListFragment.setListAdapter(...) Not ListView.setListAdapter(...).

user3388324
  • 552
  • 5
  • 17