0

私が持っているもの:

私は を持ってActivityListViewます。アイテムのはカスタムで、と複数の が含まれlayoutています。標準を使用してからデータを入力します(アダプターでのカスタマイズはまだありません)。各項目は、属性が 1 つのデータベース行に保存されている特定のオブジェクトを表します。対応するデータベース列からのデータがすべて正しく入力されますが、問題はありません。ListViewImageViewTextViewsListViewSQLiteDatabaseSimpleCursorAdapterListViewTextViews

私が欲しいもの:

をクリックして、データベース内のオブジェクトの AND と特定の属性ImageViewを変更したいと考えています。これは、アイテムの を変更し、対応する列でデータベースの 0 と 1 を切り替えるimageことによるオン/オフのようなものです。imageListView

私の質問:

どこに実装する必要がありOnClickListenerますか? 変換ビューを処理するにはどうすればよいですか (12 以上の ListView アイテムが存在する可能性があり、データベース列のエントリに応じて各アイテムに適切な画像を表示する必要があるため)。このチュートリアルhereに従って、 と で同様のことを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;
}
}
4

2 に答える 2

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 に答える