Sha*_*awn 4 android list recycle android-listview
我一直在搜索示例和教程,但我似乎无法理解如何在子类SimpleCursorAdapter中处理回收.我知道对于常规ArrayAdapter,你可以检查convertView for null和inflate,如果null是xml,如果不是null,则回收,但是我在查看SimpleCursorAdapter子类中from和to数组的工作方式时遇到了一些麻烦.我试图通过Commonsware的"繁忙的Android开发编码器指南"来解决这个问题,但是没有成功.如果有人知道任何提示,示例或教程,我将很高兴看到它们.
我无法找到任何优秀的例子来简单地使用SimpleCursorAdapter来使用convertView循环方法,所以我最终继承了CursorAdapter的子类.这实际上对我的实现非常有效,并且随着内存使用量的急剧下降而快速发展.回收意见确实有效,我推荐它!
以下是我的实施示例:
private static class MyNiftyAdapter extends CursorAdapter
{
private LayoutInflater mInflater;
private Cursor cur;
public MyNiftyAdapter(Context context, Cursor c) {
super(context,c);
this.mInflater = LayoutInflater.from(context);
this.cur = c;
}
public MyNiftyAdapter(Context context, Cursor c, boolean autoRequery)
{
super(context, c, autoRequery);
this.mInflater = LayoutInflater.from(context);
this.cur = c;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
ViewHolder viewHolder;
if(convertView == null)
{
convertView = this.mInflater.inflate(R.layout.chamber_item, null);
viewHolder = new ViewHolder();
viewHolder.name = (TextView)convertView.findViewById(R.id.Name);
viewHolder.city = (TextView)convertView.findViewById(R.id.city);
viewHolder.state = (TextView)convertView.findViewById(R.id.state);
viewHolder.country = (TextView)convertView.findViewById(R.id.country);
convertView.setTag(viewHolder);
}else
{
viewHolder = (ViewHolder)convertView.getTag();
}
this.cur.moveToPosition(position);
viewHolder.name.setText(this.cur.getString(this.cur.getColumnIndex(MyDBHelper.NAME)));
viewHolder.city.setText(this.cur.getString(this.cur.getColumnIndex(MyDBHelper.CITY)));
viewHolder.state.setText(this.cur.getString(this.cur.getColumnIndex(MyDBHelper.STATE)));
viewHolder.country.setText(this.cur.getString(this.cur.getColumnIndex(MyDBHelper.COUNTRY)));
return convertView;
}
/* (non-Javadoc)
* @see android.widget.CursorAdapter#bindView(android.view.View, android.content.Context, android.database.Cursor)
*/
@Override
public void bindView(View view, Context context, Cursor cursor) {
// Dont need to do anything here
}
/* (non-Javadoc)
* @see android.widget.CursorAdapter#newView(android.content.Context, android.database.Cursor, android.view.ViewGroup)
*/
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
// Dont need to do anything here either
return null;
}
static class ViewHolder
{
TextView name;
TextView city;
TextView state;
TextView country;
}
}
Run Code Online (Sandbox Code Playgroud)
我列表中的每一行现在都按照我的要求显示名称,城市,州和国家/地区.希望这可以帮助.
| 归档时间: |
|
| 查看次数: |
5294 次 |
| 最近记录: |