Android:如何改变一个ImageView在ListView项目onClick对应的sqliteddatabase条



我有什么:

有一个Activity和一个ListViewListView项的layout是自定义的,包含一个ImageView和几个TextViews。我使用标准SimpleCursorAdapterSQLiteDatabase的数据填充到ListView中(适配器中还没有定制)。每个ListView项代表一个对象,其属性保存在一个数据库行中。所有TextViews都正确地填充了来自相应数据库列的数据,这没有问题。

我想要的:

通过点击ImageView,我想改变image和我的对象在数据库中的某个属性。这就像一个开关,在我的ListView项目中改变image,并在我的数据库中根据列在0和1之间切换。

我的问题:

我必须在哪里实现OnClickListener ?我如何处理转换视图(因为可以有超过12个ListView项目,我希望每个项目显示正确的图像取决于在数据库列中的条目)?我用ArrayAdapter做了类似的事情,并按照本教程做了一个更简单的模型。但它不是为与SQLiteDatabase相互作用而制造的。如有任何建议,我都很感激。

编辑:

下面是我的适配器代码(现在有一些定制):

public class IOIOSensorCursorAdapter extends SimpleCursorAdapter
{
static class ViewHolder
{
ImageView iv;
}
private Context ctx;
private Cursor cursor;
private IodDatabaseManager dbm;
public IOIOSensorCursorAdapter(Context _context, int _layout,
    Cursor _cursor, String[] _from, int[] _to, int _flags)
{
super(_context, _layout, _cursor, _from, _to, _flags);
ctx = _context;
cursor = _cursor;
dbm = new IodDatabaseManager(_context);
}
@Override
public View getView(final int _position, View _convertView,
    ViewGroup _parent)
{
ViewHolder holder = null;
LayoutInflater inflater = (LayoutInflater) ctx
    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// There is no view at this position, we create a new one. In this case
// by inflating an xml layout.
if (_convertView == null)
{
    _convertView = inflater
        .inflate(R.layout.listview_item_sensor, null);
    holder = new ViewHolder();
    holder.iv = (ImageView) _convertView
        .findViewById(R.id.stateImageView);
    _convertView.setTag(holder);
}
// We recycle a View that already exists.
else
{
    holder = (ViewHolder) _convertView.getTag();
}
holder.iv.setOnClickListener(new OnClickListener()
{
    @Override
    public void onClick(View _view)
    {
    // Here I should react on the click and change the database
    // entry and the image
    cursor.moveToPosition(_position);
    Log.d("onClick: position", "" + _position);
    int sensorID = cursor.getInt(cursor
        .getColumnIndex(IOIOSensorSchema.SENSOR_ID));
    Log.d("onClick: sensorID", "" + sensorID);
    int state = cursor.getInt(cursor
        .getColumnIndex(IOIOSensorSchema.STATE));
    Log.d("onClick: state", "" + state);
    if (state == 0)
    {
        dbm.updateSensorState(sensorID, 1);
    }
    else
    {
        dbm.updateSensorState(sensorID, 0);
    }
    cursor = dbm.getIOIOSensorsCursor();
    }
});
int state = cursor.getInt(cursor
    .getColumnIndex(IOIOSensorSchema.STATE));
if (state == 0)
{
    holder.iv.setImageResource(R.drawable.av_play_over_video);
}
else
{
    holder.iv.setImageResource(R.drawable.av_pause_over_video);
}
return _convertView;
}
}

您必须在getView中实现自定义适配器。

holder.imageView.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {
           cursor.moveToPosition(position);
           //change image according to you
           //get ID and update values 
           //recreate your cursor 
        }
    });

你正在使用的游标,你必须通过查询

更新游标

恐怕我得回答我自己的问题。我终于自己解决了。非常重要的是在onCLickListener和cursor.moveToPosition(_position)下面调用notifyDataSetChanged()。在那之前,我在点击ImageView后得到了一些奇怪的行为。现在一切都正常工作了。希望这能对你们中的一些人有所帮助。

下面是我的代码:
public class IOIOSensorCursorAdapter extends SimpleCursorAdapter
{
    static class ViewHolder
    {
    ImageView iv;
    }
    private Context ctx;
    private Cursor cursor;
    private IodDatabaseManager dbm;
    public IOIOSensorCursorAdapter(Context _context, int _layout,
        Cursor _cursor, String[] _from, int[] _to, int _flags)
    {
    super(_context, _layout, _cursor, _from, _to, _flags);
    ctx = _context;
    cursor = _cursor;
    dbm = new IodDatabaseManager(_context);
    }
    @Override
    public View getView(final int _position, View _convertView,
        ViewGroup _parent)
    {
    ViewHolder holder = null;
    LayoutInflater inflater = (LayoutInflater) ctx
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    // There is no view at this position, we create a new one. In this case
    // by inflating an xml layout.
    if (_convertView == null)
    {
        _convertView = inflater
            .inflate(R.layout.listview_item_sensor, null);
        holder = new ViewHolder();
        holder.iv = (ImageView) _convertView
            .findViewById(R.id.stateImageView);
        _convertView.setTag(holder);
    }
    // We recycle a View that already exists.
    else
    {
        holder = (ViewHolder) _convertView.getTag();
    }
    holder.iv.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View _view)
        {
        // Here I should react on the click and change the database
        // entry and the image
        cursor.moveToPosition(_position);
        Log.d("onClick: position", "" + _position);
        int sensorID = cursor.getInt(cursor
            .getColumnIndex(IOIOSensorSchema.SENSOR_ID));
        Log.d("onClick: sensorID", "" + sensorID);
        int state = cursor.getInt(cursor
            .getColumnIndex(IOIOSensorSchema.STATE));
        Log.d("onClick: state", "" + state);
        if (state == 0)
        {
            dbm.updateSensorState(sensorID, 1);
        }
        else
        {
            dbm.updateSensorState(sensorID, 0);
        }
        cursor = dbm.getIOIOSensorsCursor();
        notifyDataSetChanged();
        }
    });
    cursor.moveToPosition(_position);
    int state = cursor.getInt(cursor
        .getColumnIndex(IOIOSensorSchema.STATE));
    if (state == 0)
    {
        holder.iv.setImageResource(R.drawable.av_play_over_video);
    }
    else
    {
        holder.iv.setImageResource(R.drawable.av_pause_over_video);
    }
    return _convertView;
    }
}

最新更新