INTRODUCING ADAPTERS

 

Adapters are bridging classes that bind data to Views (such as List Views) used in the user interface.

 

Some Native Adapters

ArrayAdapter The Array Adapter uses generics to bind an Adapter View to an array of
objects of the specified class.

 

SimpleCursorAdapter The Simple Cursor Adapter attaches Views specified within a lay-
out to the columns of Cursors returned from Content Provider queries.

 

Customizing the Array Adapter

public class MyArrayAdapter extends ArrayAdapter<MyClass> {
int resource;
public MyArrayAdapter(Context context,
int _resource,
List<MyClass> items) {
super(context, _resource, items);
resource = _resource;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LinearLayout newView;
MyClass classInstance = getItem(position);
// TODO Retrieve values to display from the
// classInstance variable.
// Inflate a new view if this is not an update.
if (convertView == null) {
newView = new LinearLayout(getContext());
String inflater = Context.LAYOUT_INFLATER_SERVICE;
LayoutInflater vi = (LayoutInflater)getContext().getSystemService(inflater);
vi.inflate(resource, newView, true);
} else {
newView = (LinearLayout)convertView;
}
// TODO Retrieve the Views to populate
// TODO Populate the Views with object property values.

return newView;
}
}

 

Using Adapters for DataBinding

ArrayList<String> myStringArray = new ArrayList<String>();
ArrayAdapter<String> myAdapterInstance;
int layoutID = android.R.layout.simple_list_item_1;
myAdapterInstance = new ArrayAdapter<String>(this, layoutID , myStringArray);
myListView.setAdapter(myAdapterInstance);

 

myAdapterInstance.notifyDataSetChanged();

 

Using the Simple Cursor Adapter

The SimpleCursorAdapter lets you bind a Cursor to a List View, using a custom layout definition to
define the layout of each row/item, which is populated by a row’s column values.

 

String uriString = "content://contacts/people/";
Cursor myCursor = managedQuery(Uri.parse(uriString), null, null, null);
String[] fromColumns = new String[] {People.NUMBER, People.NAME};
int[] toLayoutIDs = new int[] { R.id.nameTextView, R.id.numberTextView};
SimpleCursorAdapter myAdapter;
myAdapter = new SimpleCursorAdapter(this,
R.layout.simplecursorlayout,

myCursor,
fromColumns,
toLayoutIDs);

myListView.setAdapter(myAdapter);

 

 

阅读更多
个人分类: Android
想对作者说点什么? 我来说一句

Introducing.Python.2014.11.pdf

2015年03月05日 8.17MB 下载

Apress.Introducing.Spring.Framework.A.Primer.

2014年10月07日 10.16MB 下载

Introducing Go.pdf

2018年03月13日 2.97MB 下载

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭