Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Please explain Array Adapters and their purpose. Even better [closed]

Tags:

android

Better yet can someone explain this program point by point?

package com.paad.todolist;

import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;

public class ToDoList extends Activity {

  /** Called when the activity is first created. */
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Inflate your view
    setContentView(R.layout.main);

    // Get references to UI widgets
    ListView myListView = (ListView)findViewById(R.id.myListView);
    final EditText myEditText = (EditText)findViewById(R.id.myEditText);

    // Create the array list of to do items
    final ArrayList<String> todoItems = new ArrayList<String>();
    // Create the array adapter to bind the array to the listview
    final ArrayAdapter<String> aa;
    aa = new ArrayAdapter<String>(this,
                                  android.R.layout.simple_list_item_1,
                                  todoItems);
    // Bind the array adapter to the listview.
    myListView.setAdapter(aa);

    myEditText.setOnKeyListener(new OnKeyListener() {
        public boolean onKey(View v, int keyCode, KeyEvent event) {
          if (event.getAction() == KeyEvent.ACTION_DOWN)
            if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER)
            {
              todoItems.add(0, myEditText.getText().toString());
              aa.notifyDataSetChanged();
              myEditText.setText("");
              return true;
            }
          return false;
        }
      });
  }
}
like image 986
jojo10 Avatar asked Nov 29 '10 03:11

jojo10


1 Answers

ToDoList is the activity that defines the UI elements of the screen that user sees.

UI elements of the screen are defined in the layout /res/layout/main.xml

One of the UI element in the layout main.xml is a ListView whose ID is myListView

ListView can be something that acts as a container for list items. So all list view needs to know is, how many items are in the list, and how the each list item looks like?

An adapter is something that knows about the list items and how to represent or draw each list item on the screen.

Above example makes use of ArrayAdapter and its constructor takes 3 parameters having information about the list items

final ArrayAdapter<String> aa;
aa = new ArrayAdapter<String>(this,
                              android.R.layout.simple_list_item_1,
                              todoItems);

First argument is Context to access system services and resources ( you need layout inflater to create list item view )

Second argument defines the layout of the list that defines how the list item appears in listview. Here layout android.R.layout.simple_list_item_1 which is defined by framework is used.

Third argument is the information about the list item, typically this information is used to create view for the list item.

Finally created Adapter is given to the ListView

myListView.setAdapter(aa);

Now ListView calls the functions of Adapter to get the views of list item and populates in the container.

If the list items are changed ( here todo list ) Adapter can let the ListView know about it by calling notifyDataSetChanged.

aa.notifyDataSetChanged();

You can have a look at the implementation of ArrayAdapter to get more clarity.

Hope this helps you!

like image 131
Suresh Avatar answered Sep 20 '22 18:09

Suresh