Please explain Array Adapters and their purpose. Even better [closed]
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this questionBetter 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 firs开发者_StackOverflow社区t 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;
}
});
}
}
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!
In simple terms, an Adapter is a collection handler that returns each item in the collection as a view. ArrayAdapter is one of many adapters available in Android. Others include, ListAdapter, GalleryAdapter, CursorAdapter, etc. Have a look at Android references
An Adapter
object acts as a bridge between an AdapterView
and the underlying data for that view. The Adapter
provides access to the data items. The Adapter
is also responsible for making a View for each item in the data set. Adapter
精彩评论