Creating My Very Own ArrayAdapter

If you are new to Java in Android Studio, you may have tried to drag a button into your layout file. When you try to run your program the button doesn’t do anything. That makes sense, you didn’t tell the button to do anything ‘onclick’. The same thing is going to happen when you add a ListView to your project. You know, the thing that lets you see the same thing over and over again with different data inside?

Today, I’m going to talk about my experiences using ArrayAdapters. As software developers know, there are many different ways to write code. So, this is the variant that I chose this time around. My experiences are going to be done in Android Studio developing for an Android Application.

Creating a basic non-Custom ArrayAdapter

When creating an ArrayAdapter inside of an Activity it is very simple. By this I mean that the there are very few lines of code and there is limited functionality. For example, displaying a checkbox and a TextView requires a custom adapter.

Create the array of items.

There are many ways to create/gather an array of data but this is the way that I have chosen.

String[] items = {"Milk", "Bread", "Sandwich meat", "Sandwich cheese","Pickles", "Mustard", "Mayo", "Water"};

In this example, I have created and array of strings called items.

Call Your Method

This is super simple. In my example, I have chosen to call my method populateListView(); Call this method inside of the onCreate Method to call it when the Activity starts.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_item_list);

    populateListView();
}

Initialize the Adapter

Next, we need to initialize the adapter. In other words we need to make a new adapter.

ArrayAdapter<String> itemsAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items);

 Attach the ListView to the Adapter and Set the Adapter

Now, we need to tie the ListView to the Adapter so that we know what element to populate.

ListView listView = (ListView) findViewById(R.id.listViewMain);
listView.setAdapter(itemsAdapter);

Now there are only two files that we need to create/edit to finalize our work. These are the layout files. The first one is going to contain a ListView inside of a Linear/Relative Layout. The second is going to be a TextView  without a Linear/Relative Layout. Make sure the layouts are referenced correctly when initializing the adapters.

Creating a Custom ArrayAdapter

Now we are going to try to create a Custom ArrayAdapter. This is going to allow us to customize the item layout in any way we want. For the sake of this example, I have added a checkbox to our item layout.

I am going to talk about what is different from the Basic ArrayAdapter that way we do not duplicate too much code. Things that are going to remain the same are the first layout xml file, the way we create our array, and the way in which we call our method. The second layout file is going to be slightly different as well. We are going to add the checkbox and make sure the entire file is now inside of a Linear/Relative Layout much like the first layout.

There are going to be one new file that we need to create. In our situation I have called it ItemAdapter.java.

public class ItemAdapter extends ArrayAdapter<String> {

    public ItemAdapter(Context context) {
        super(context, 0, new ArrayList<String>());
    }

    public void setItems(List items) {
        addAll(items);
    }

    @Override
    public View getView(int position, View view, ViewGroup viewGroup) {
      if (view == null) {
        view = LayoutInflater.from(getContext()).inflate(R.layout.item, viewGroup, false);
      }

      TextView textView1 = (TextView) view.findViewById(R.id.textViewMain);
      CheckBox cb = (CheckBox) view.findViewById(R.id.checkBox1);
      textView1.setText(getItem(position));

      return view;
    }

}

We are going to use this file to populate the items in the ListView. We are going to tell it what data we want it to use (the array that we created) and then we are going to tell it what layout to talk to. In this situation we are going to talk to the layout item.xml. In specific we are going to talk to the TextView and the CheckBox with their respected ID’s inside of this file. We do not need to do anything with the checkbox yet but we are going to setText for the TextViews based on their position.

private void populateListView() {
    String[] items = {"Milk", "Bread", "Sandwich meat", "Sandwich cheese", "Pickles", "Mustard", "Mayo", "Water", "Paper Plates", "Paper Cups", "Paper Towels", "Cleaning Spray", "Plastic Forks", "Plastic Spoons", "Yogurt", "Pop Tarts", "Coca Cola", "Chips", "Coffee", "Coffee Filters", "Cereal", "Crackers"};

 List<String> arrayOfItems = Arrays.asList(items);
 ItemAdapter adapter = new ItemAdapter(this);
 ListView listView = (ListView) findViewById(R.id.listViewMain);
 listView.setAdapter(adapter);
 adapter.setItems(arrayOfItems);
}

As shown above there are some changes to the Activity. We are now going to reference the array we have created, create the adapter, find the view by its ID, tie the adapter and the view together and setItems. These items are going to be Strings in this situation. If you would like to, you can make them an Object. For this example this is what works best.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s