将Cursor数据绑定到非列表布局

时间:2012-02-14 18:36:31

标签: android cursor

我正在编写一个屏幕,显示来自数据库的一行信息。基本上它是一个细节片段,表示与表中一个'行'有关的信息。我想了解将数据从游标(表中的一个唯一行)绑定到textview,复选框等布局的最佳实践。

AdapterView是票吗?

@JoeMalin建议:

  

然后在游标和文本视图数组之间编写一个适配器。

这归结了我的问题。将一系列文本视图挂钩到游标的正确方法是什么?

3 个答案:

答案 0 :(得分:1)

如果要在将某些游标数据移动到文本视图之前对某些游标数据进行处理,那么您将超越适配器模式,该模式假定将数据结构的形式“重铸”到另一个数据结构没有任何中间处理。适配器的优点在于,对于由适配器链接的两个数据结构A和B,假设B在A发生变化时自动更改。

当然,您可以重新定义适配器插入自己的中间操作的想法,例如转换日期,,您可以将转换作为显示数据的视图的一个方面。我猜测“处理”实际上是格式化,你出于显示目的。这是文本视图的属性,而不是数据;写一些扩展文本视图并根据需要转换日期的内容。然后在游标和文本视图数组之间编写一个适配器。

答案 1 :(得分:1)

我最近实现了自己的数据适配器类,可能在球场。

public class NoteImageDataAdapter {

private final View mMainView;
private Cursor mCursor;
private ViewHolder holder;
private ContentObserver mContentObserver;

public static class ViewHolder {
    public TextView title;
    public TextView text;
    public ImageView image;
}

public NoteImageDataAdapter(View mainView, Cursor c) {
    if (mainView == null) {
        throw new IllegalArgumentException("View mainView cannot be null");
    }

    if (c == null) {
        throw new IllegalArgumentException("Cursor c cannot be null");
    }
    mMainView = mainView;
    mCursor = c;

    holder = new ViewHolder();
    holder.title = (TextView) mMainView.findViewById(R.id.title);
    holder.text = (TextView) mMainView.findViewById(R.id.text);
    holder.image = (ImageView) mMainView.findViewById(R.id.myImageView);

    mContentObserver = new ImageNoteContentObserver(new Handler());
    mCursor.registerContentObserver(mContentObserver);
    bindView();
}

class ImageNoteContentObserver extends ContentObserver {

    public ImageNoteContentObserver(Handler handler) {
        super(handler);
    }

    @Override
    public boolean deliverSelfNotifications() {
        return true;
    }

    @Override
    public void onChange(boolean selfChange) {
        Log.d("NoteImageDataAdapter", "ImageNoteContentObserver.onChange( "
                + selfChange + ")");
        super.onChange(selfChange);
        mCursor.requery();
        bindView();
    }
}

public void bindView() {
    Log.d("NoteImageDataAdapter", "bindView");
    mCursor.moveToFirst();

    holder.text.setText(Note.getText(mCursor));
    holder.title.setText(Note.getTitle(mCursor));

    Uri imageUri = Note.getImageUri(mCursor);
    if (imageUri != null) {
        assignImage(holder.image, imageUri);
    } else {
        Drawable d = Note.getImageThumbnail(mCursor);
        holder.image.setImageDrawable(d);
        holder.image.setVisibility(View.VISIBLE);
    }
}

private static final int MAX_IMAGE_PIXELS = 1024*512;

private void assignImage(ImageView imageView, Uri imageUri){
    if (imageView != null && imageUri != null){

        ContentResolver cr = imageView.getContext().getContentResolver();

        Display display = ((WindowManager) imageView.getContext()
                .getSystemService(Context.WINDOW_SERVICE))
                .getDefaultDisplay();
        int width = (int) (display.getWidth() * 0.9);
        int height = (int) (display.getHeight() * 0.9);

        int minSideLength = Math.min(height, width);

        Bitmap b = Util.makeBitmap(minSideLength, MAX_IMAGE_PIXELS, imageUri, cr, false);

        if (b == null){
            b = Util.makeBitmap(minSideLength, MAX_IMAGE_PIXELS/2, imageUri, cr, false);                
        }
        if (b != null){
            imageView.setImageBitmap(b);
            imageView.setAdjustViewBounds(true);
            imageView.setVisibility(View.VISIBLE);
        }   
    }   
}

}

和您的活动

private NoteImageDataAdapter mAdapter;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.note_image_view_layout);

    wireDataAdapter();       
}

private void wireDataAdapter() {
    final String[] COLUMNS = new String[] { 
            Note.Columns.TITLE,
            Note.Columns.TEXT,
            Note.Columns.IMAGE_URI, 
            Note.Columns.IMAGE_THUMBNAIL,
            Note.Columns._ID };

    // the uri for the note row
    Uri contentUri = getIntent().getData();
    Cursor cur = managedQuery(contentUri, COLUMNS, null, null, null);

    View mainLayout = this.findViewById(R.id.noteImageViewLayout);
    mAdapter = new NoteImageDataAdapter(mainLayout, cur);
}

答案 2 :(得分:-1)

从活动使用:

Adpater adapter = new Adapter(Activity.this or context , Cursor);
setListAdapter(adapter) in case of List Activity;

否则

listViewObj.setAdpater(adapter)   

公共类CustomCursorAdapter扩展了CursorAdapter {

 private LayoutInflater mInflater;
 private Context activityContext;

 private ViewHolder holder;

 public ContactsAdapter(Context aContext,Cursor cursor) {
        super(mContext, cursor);
        mInflater = LayoutInflater.from(mContext); 
        activityContext = aContext;

    }  

 public static class ViewHolder{
     public TextView textView1;
     // View Group on Row inflate lyaout that need to be used
     public ImageView imageView;
   }

 @Override
 public void bindView(View v, Context context, Cursor c) { 
  holder=(ViewHolder)v.getTag();

 }


 @Override
 public View newView(Context context, Cursor cursor, ViewGroup parent) {
  final LayoutInflater inflater = LayoutInflater.from(context);
        View v = inflater.inflate(R.layout.item_inflate_layout, parent, false);  

        holder = new ViewHolder();
     holder.textView1 = (TextView) v.findViewById(R.id.TEXTVIEW1);
  // Other Id that need to be used and are available on item_inflate_layout
     holder.imageView = (ImageView) v.findViewById(R.id.IMAGEVIEW);
     v.setTag(holder);     
  bindView(v, context, cursor);
        return v;  
 }
 }