Skip to content

Instantly share code, notes, and snippets.

@futabooo
Last active August 29, 2015 14:20
Show Gist options
  • Save futabooo/1bbeb7642135024f6b30 to your computer and use it in GitHub Desktop.
Save futabooo/1bbeb7642135024f6b30 to your computer and use it in GitHub Desktop.

ListView And Adapter

@futabooo


What is a ListView?


It displays a list of scrollable items


ListView in Couples

fit, inlinefit, inline


What is an Adapter?


##“Man in the Middle” inline


Let's write our ListView


ListView Overview

  1. Declare our ListView in our layout.xml
  2. Create our Adapter class
  3. Fetch the items for our list
  4. Specify the layout that we want for our list items
  5. Plug our Adapter with our declared Listview

1. Declare our ListView in our layout.xml

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

2. Create our Adapter class

public class MyAdapter extends ArrayAdapter<String> {

    public MyAdapter(Context context, int resource, List<String> items) {
        super(context, resource, items);
    }

}

3. Fetch the items for our list

List<String> items = new ArrayList<String>();
items.add("Apple");
items.add("Banana");
items.add("Pineapple");

4. Specify the layout that we want for our list items

MyAdapter adapter = new MyAdapter(this, R.layout.simple_list_item_1, items);

5. Plug our Adapter with our declared Listview

ListView listView = (ListView) findViewById(R.id.my_list);
listview.setAdapter(adapter);

Done!


ListViewPractice

RecyclerView ← NEW!!

Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment