Android 使用自定义cursorAdapter

以下的MySimpleCursorAdapter扩展了SimpleCursorAdapter类,从而实现了从cursor项内容向自定义的视图容器的对应。

public class MySimpleCursorAdapter extends SimpleCursorAdapter { private Cursor m_cursor; private Context m_context; @Override public View newView(Context context, Cursor cursor, ViewGroup parent) { final View view = super.newView(context, cursor, parent); ViewHolder holder = new ViewHolder(); holder.titleView = (TextView) view.findViewById(R.id.title); holder.linkView = (TextView) view.findViewById(R.id.Link); holder.descriptionView = (TextView) view.findViewById(R.id.Description); holder.categoryView = (TextView) view.findViewById(R.id.Category); holder.authorView = (TextView) view.findViewById(R.id.Author); holder.pubDateView = (TextView) view.findViewById(R.id.PubDate); view.setTag(holder); return view; } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub return super.getView(position, convertView, parent); } public MSimpleCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to) { super(context, layout, c, from, to); m_cursor = c; m_context = context; } @Override public void bindView(View view, Context context, Cursor cursor) { ViewHolder holder = (ViewHolder) view.getTag(); setViewText(holder.titleView, cursor.getString(cursor .getColumnIndex(Items.TITLE))); setViewText(holder.linkView, cursor.getString(cursor .getColumnIndex(Items.LINK))); setViewText(holder.descriptionView, cursor.getString(cursor .getColumnIndex(Items.DESCRIPTION))); setViewText(holder.categoryView, cursor.getString(cursor .getColumnIndex(Items.CATEGORY))); setViewText(holder.authorView, cursor.getString(cursor .getColumnIndex(Items.AUTHOR))); setViewText(holder.pubDateView, cursor.getString(cursor .getColumnIndex(Items.PUBDATE))); super.bindView(view, context, cursor); } final static class ViewHolder { public TextView titleView; public TextView linkView; public TextView descriptionView; public TextView categoryView; public TextView authorView; public TextView pubDateView; } }

在使用时直接用MySimpleCursorAdapter来替代SimpleCursorAdapter即可,你可以修改上述程序中的ViewHolder中的项来达到自己想要的目的。

你可能感兴趣的:(c,android,String,layout,Class,扩展)