Android中要填充一些控件(如ListView)经常须要用到Adapter来实现,经常使用的有ArrayAdapter,SimpleAdapter,CursorAdapter,BaseAdapter,前几个都是继承自BaseAdapter的。我平时经常使用的就是ArrayAdapter,再就是BaseAdapter了,SimpleAdapter本身事实上也不算简单。所以能用SimpleAdapter的时候我一般都用了BaseAdapter。
至于CuesorAdapter我就差点儿没用过。今天事实上是有点忘了BaseAdapter怎么用了,在这记一下,以备后用,也就主要是讲须要注意的东西。
1.BaseAdapter is generally used to override the constructor method.
In fact, Baseadapter is generally better used, mainly to achieve four abstract methods. But then suddenly can't remember how to pass the data passed, naturally think of the construction method. Look at the code once, I know it. So here's a conclusion: to use Baseadapter, you need to rewrite the construction method.
2. There must be a context in the baseadapter.
Because the baseadapter to fill its own write view. So it is necessary to use a Layoutinflater object, and to get Layoutinflater object need to use Layoutinflater.from (context); So it seems. Also remember a small conclusion: to use baseadapter you need to define a context.
To use this context object, or to pass it, then get a conclusion: to use Baseadapter in the construction method to pass at least one of the context object parameters.
3. To use the Viewholder class, include the controls in your own definition layout.
We know that the ListView and the like need optimization. Otherwise always fill the memory, the extreme when the memory may occur when the situation causes the program FC, so you have to customize an internal class viewholder to optimize. So again we get a conclusion: to use Baseadapter to define a Viewholder class on its own. Instantiate a Viewholder object when it is used.
When convertview== null. By convertView = inflater.inflate(R.layout.list_item, null); initializing the layout, then initializing the controls in the layout, and then calling the
convertView.setTag(viewHolder);else{ viewHolder= (ViewHolder) convertView.getTag(); }
is to get the components again. Instead of initializing again.
Baseadapter using base points in Android