Android App不会在自定义的simplecursoradapter中命中bindView

时间:2011-06-10 07:50:00

标签: android android-listview android-cursor

我很遗憾为什么我的自定义光标没有点击bindView覆盖方法。有什么想法吗?

public class ContactListSqlCursorAdapter extends SimpleCursorAdapter implements
    OnClickListener {

private Context currentContext;

public static final String TABLE_NAME = "...";

private DatabaseHelper dbHelper;
private Cursor currentCursor;
private int layout;

public ContactListSqlCursorAdapter(Context context, int layout, Cursor c,
        String[] from, int[] to, DatabaseHelper dbHelper) {

    super(context, layout, c, from, to);
    this.currentCursor = c;
    this.currentContext = context;
    this.dbHelper = dbHelper;
    this.layout = layout;

}

public View getView(int pos, View inView, ViewGroup parent) {
    View v = inView;
    if (v == null) {
        LayoutInflater inflater = (LayoutInflater) currentContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = inflater.inflate(this.layout, null);
    }

    this.currentCursor.moveToPosition(pos);
    v.setTag(Integer.parseInt(this.currentCursor
            .getString(this.currentCursor
                .getColumnIndex(DatabaseHelper.COLUMN_ID))));

    TextView txtTitle = (TextView) v.findViewById(R.id.txtTitle);
    txtTitle.setText(this.currentCursor.getString(this.currentCursor
            .getColumnIndex("ZFIRSTNAME")));

    return (v);
}

public void ClearSelections() {
    // this.dbHelper.clearSelections();
    this.currentCursor.requery();

}

@Override
public void onClick(View v) {

    CheckBox cBox = (CheckBox) v;
    Integer _id = (Integer) cBox.getTag();

    ContentValues values = new ContentValues();
    values.put(" ZHAS", cBox.isChecked() ? 1 : 0);
    this.dbHelper.shermanDB.update(this.TABLE_NAME, values, "_id='"
            + Integer.toString(_id) + "'", null);
}

@Override
public void bindView(View view, Context context, Cursor cursor) {
    ImageView imageView = (ImageView) view
            .findViewById(R.id.img_contact_photo);

    int id = currentCursor.getColumnIndex(ContactsContract.Contacts._ID);
    Uri uri = ContentUris.withAppendedId(
            ContactsContract.Contacts.CONTENT_URI, currentCursor
                    .getLong(id));

    ContentResolver cr = currentContext.getContentResolver();
    InputStream is = ContactsContract.Contacts.openContactPhotoInputStream(
            cr, uri);

    Bitmap photo = null;
     }
     }
    if (is != null) {
        photo = BitmapFactory.decodeStream(is);
    }

    if (photo != null) {
        imageView.setImageBitmap(photo);
    }

    super.bindView(view, context, cursor);
}

的活动:

            ContactsActivity.this.adapter = new ContactListSqlCursorAdapter(
                    ContactsActivity.this, R.layout.contact_list_item,
                    ContactsActivity.this.currentCursor, dbColumns,
                    listFields, ContactsActivity.this.dbHelper);
            ContactsActivity.this.lv
                    .setAdapter(ContactsActivity.this.adapter);

1 个答案:

答案 0 :(得分:3)

标准CursorAdapter implementation从getView调用newView或bindView。

如果覆盖getView,则适配器将不再调用newView / bindView。

对于你的记录:如果你扩展SimpleCursorAdapter你应该覆盖getView或newView / bindView ...它可以覆盖所有这些方法,但你应该从getView调用newView / bindView。