Android-приложение не будет работать с bindView в собственном простом курсоре - PullRequest
0 голосов
/ 10 июня 2011

Я немного растерялся из-за того, что мой пользовательский курсор не использует метод переопределения 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 Ответ

3 голосов
/ 10 июня 2011

Стандартная реализация CursorAdapter вызывает newView или bindView из getView.

Если вы переопределите getView, ваш адаптер больше не будет вызывать newView / bindView.

Для ваших записей: если вы расширяете SimpleCursorAdapter, вы должны переопределить getView или newView / bindView ... Это нормально переопределить все эти методы, но вы должны вызвать newView / bindView из getView.

...