0

我有的:

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

我想要的是:

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

我的问题:

我必须在哪里实施OnClickListener?如何处理转换视图(因为可能有超过 12 个 ListView 项目,并且我希望每个项目根据相应数据库列中的条目显示正确的图像)?ArrayAdapter在本教程之后,我用 and 和一个更简单的模型做了类似的事情。但它不是为与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;
}
}
4

2 回答 2

0

您必须实现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 

        }
    });

您正在使用的游标,您必须通过重新查询来更新该游标

于 2013-05-24T09:19:22.043 回答
0

恐怕我必须回答我自己的问题。我终于自己解决了。非常重要的是调用notifyDataSetChanged()onCLickListener 及其cursor.moveToPosition(_position)下方。在此之前,我在单击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;
    }
}
于 2013-05-24T16:53:49.007 回答