Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/217.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android:删除数据库行后,如何重新查询光标以刷新ListView?_Android_Listview_Cursor_Android Sqlite_Simplecursoradapter - Fatal编程技术网

Android:删除数据库行后,如何重新查询光标以刷新ListView?

Android:删除数据库行后,如何重新查询光标以刷新ListView?,android,listview,cursor,android-sqlite,simplecursoradapter,Android,Listview,Cursor,Android Sqlite,Simplecursoradapter,这可能是一个noob问题,但我对所有这些SQLite数据库游标适配器ListView都是新手 我所拥有的: public class IOIOSensorCursorAdapterCam extends SimpleCursorAdapter { static class ViewHolder { ImageView stateIV, removeIV; TextView nameTV, pinNumberTV, feedIDTV, freqTV; } private Context ctx;

这可能是一个noob问题,但我对所有这些SQLite数据库游标适配器ListView都是新手

我所拥有的:

public class IOIOSensorCursorAdapterCam extends SimpleCursorAdapter
{
static class ViewHolder
{
ImageView stateIV, removeIV;
TextView nameTV, pinNumberTV, feedIDTV, freqTV;
}

private Context ctx;
private Cursor cursor;
private IodDatabaseManager dbm;

public IOIOSensorCursorAdapterCam(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)
{
    // Inflate a layout
    _convertView = inflater.inflate(R.layout.listview_item_sensor_cam,
        null);

    holder = new ViewHolder();
    holder.stateIV = (ImageView) _convertView
        .findViewById(R.id.stateImageView);
    holder.nameTV = (TextView) _convertView
        .findViewById(R.id.sensorNameTextView);
    holder.pinNumberTV = (TextView) _convertView
        .findViewById(R.id.sensorPinNumberTextView);
    holder.feedIDTV = (TextView) _convertView
        .findViewById(R.id.sensorFeedIDTextView);
    holder.freqTV = (TextView) _convertView
        .findViewById(R.id.sensorFrequencyTextView);
    holder.removeIV = (ImageView) _convertView
        .findViewById(R.id.removeImageView);
    _convertView.setTag(holder);
}
// We recycle a View that already exists.
else
{
    holder = (ViewHolder) _convertView.getTag();
}

// Set an OnClickListener to the "Delete Icon"
holder.removeIV.setOnClickListener(new OnClickListener()
{
    @SuppressWarnings("deprecation")
    @Override
    public void onClick(View _view)
    {
    cursor.moveToPosition(_position);

    // Delete sensor from database here
    int sensorID = cursor.getInt(cursor
        .getColumnIndex(IOIOSensorSchema.SENSOR_ID));
    dbm.deleteIOIOSensor(sensorID);

    // This leads to a "CursorIndexOutOfBoundsException" and cannot
    // be used to refresh the ListView
//      cursor = dbm.getIOIOSensorsCursor();

    // Refresh ListView
    cursor.requery();
    notifyDataSetChanged();
    }
});

cursor.moveToPosition(_position);

if (cursor.getCount() > 0)
{
    int state = cursor.getInt(cursor
        .getColumnIndex(IOIOSensorSchema.STATE));

    if (state == 0)
    {
    holder.stateIV.setImageResource(R.drawable.av_play_over_video);
    holder.stateIV.setColorFilter(ctx.getResources().getColor(
        R.color.hint_lighter_gray));
    // _convertView.setAlpha((float) 0.5);
    holder.nameTV.setTextColor(ctx.getResources().getColor(
        R.color.hint_darker_gray));
    }
    else
    {
    holder.stateIV.setImageResource(R.drawable.av_pause_over_video);
    holder.stateIV.setColorFilter(ctx.getResources().getColor(
        android.R.color.holo_green_light));
    // _convertView.setAlpha((float) 1);
    holder.nameTV.setTextColor(ctx.getResources().getColor(
        android.R.color.black));
    }

    // Set the sensor's name to the according TextView
    String sensorName = cursor.getString(cursor
        .getColumnIndex(IOIOSensorSchema.NAME));
    holder.nameTV.setText(sensorName);

    // Set the sensor's pin number to the according TextView
    int pinNumber = cursor.getInt(cursor
        .getColumnIndex(IOIOSensorSchema.PIN_NUMBER));
    holder.pinNumberTV.setText("" + pinNumber);

    // Set the sensor's feed ID to the according TextView
    int feedID = cursor.getInt(cursor
        .getColumnIndex(IOIOSensorSchema.FEED_ID));
    holder.feedIDTV.setText("" + feedID);

    // Set the sensor's frequency to the according TextView
    int frequency = cursor.getInt(cursor
        .getColumnIndex(IOIOSensorSchema.FREQUENCY));
    int timeUnit = cursor.getInt(cursor
        .getColumnIndex(IOIOSensorSchema.TIME_UNIT));
    String frequencyTextViewText = "";
    switch (timeUnit)
    {
    case IodIOIOSensor.TIME_UNIT_MINUTES:
    frequencyTextViewText = frequency + " min";
    break;
    case IodIOIOSensor.TIME_UNIT_HOURS:
    frequencyTextViewText = frequency + " h";
    break;
    default:
    frequencyTextViewText = frequency + " sec";
    break;
    }
    holder.freqTV.setText(frequencyTextViewText);
}
return _convertView;
}
}
在我的
main活动中
有一个
列表视图
。我使用
SQLite数据库
并使用扩展
SimpleCursorAdapter
的自定义适配器填充
ListView
。通过单击我的
操作栏
中的项目,我激活了
上下文操作模式
。到目前为止一切正常

我想要什么:

public class IOIOSensorCursorAdapterCam extends SimpleCursorAdapter
{
static class ViewHolder
{
ImageView stateIV, removeIV;
TextView nameTV, pinNumberTV, feedIDTV, freqTV;
}

private Context ctx;
private Cursor cursor;
private IodDatabaseManager dbm;

public IOIOSensorCursorAdapterCam(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)
{
    // Inflate a layout
    _convertView = inflater.inflate(R.layout.listview_item_sensor_cam,
        null);

    holder = new ViewHolder();
    holder.stateIV = (ImageView) _convertView
        .findViewById(R.id.stateImageView);
    holder.nameTV = (TextView) _convertView
        .findViewById(R.id.sensorNameTextView);
    holder.pinNumberTV = (TextView) _convertView
        .findViewById(R.id.sensorPinNumberTextView);
    holder.feedIDTV = (TextView) _convertView
        .findViewById(R.id.sensorFeedIDTextView);
    holder.freqTV = (TextView) _convertView
        .findViewById(R.id.sensorFrequencyTextView);
    holder.removeIV = (ImageView) _convertView
        .findViewById(R.id.removeImageView);
    _convertView.setTag(holder);
}
// We recycle a View that already exists.
else
{
    holder = (ViewHolder) _convertView.getTag();
}

// Set an OnClickListener to the "Delete Icon"
holder.removeIV.setOnClickListener(new OnClickListener()
{
    @SuppressWarnings("deprecation")
    @Override
    public void onClick(View _view)
    {
    cursor.moveToPosition(_position);

    // Delete sensor from database here
    int sensorID = cursor.getInt(cursor
        .getColumnIndex(IOIOSensorSchema.SENSOR_ID));
    dbm.deleteIOIOSensor(sensorID);

    // This leads to a "CursorIndexOutOfBoundsException" and cannot
    // be used to refresh the ListView
//      cursor = dbm.getIOIOSensorsCursor();

    // Refresh ListView
    cursor.requery();
    notifyDataSetChanged();
    }
});

cursor.moveToPosition(_position);

if (cursor.getCount() > 0)
{
    int state = cursor.getInt(cursor
        .getColumnIndex(IOIOSensorSchema.STATE));

    if (state == 0)
    {
    holder.stateIV.setImageResource(R.drawable.av_play_over_video);
    holder.stateIV.setColorFilter(ctx.getResources().getColor(
        R.color.hint_lighter_gray));
    // _convertView.setAlpha((float) 0.5);
    holder.nameTV.setTextColor(ctx.getResources().getColor(
        R.color.hint_darker_gray));
    }
    else
    {
    holder.stateIV.setImageResource(R.drawable.av_pause_over_video);
    holder.stateIV.setColorFilter(ctx.getResources().getColor(
        android.R.color.holo_green_light));
    // _convertView.setAlpha((float) 1);
    holder.nameTV.setTextColor(ctx.getResources().getColor(
        android.R.color.black));
    }

    // Set the sensor's name to the according TextView
    String sensorName = cursor.getString(cursor
        .getColumnIndex(IOIOSensorSchema.NAME));
    holder.nameTV.setText(sensorName);

    // Set the sensor's pin number to the according TextView
    int pinNumber = cursor.getInt(cursor
        .getColumnIndex(IOIOSensorSchema.PIN_NUMBER));
    holder.pinNumberTV.setText("" + pinNumber);

    // Set the sensor's feed ID to the according TextView
    int feedID = cursor.getInt(cursor
        .getColumnIndex(IOIOSensorSchema.FEED_ID));
    holder.feedIDTV.setText("" + feedID);

    // Set the sensor's frequency to the according TextView
    int frequency = cursor.getInt(cursor
        .getColumnIndex(IOIOSensorSchema.FREQUENCY));
    int timeUnit = cursor.getInt(cursor
        .getColumnIndex(IOIOSensorSchema.TIME_UNIT));
    String frequencyTextViewText = "";
    switch (timeUnit)
    {
    case IodIOIOSensor.TIME_UNIT_MINUTES:
    frequencyTextViewText = frequency + " min";
    break;
    case IodIOIOSensor.TIME_UNIT_HOURS:
    frequencyTextViewText = frequency + " h";
    break;
    default:
    frequencyTextViewText = frequency + " sec";
    break;
    }
    holder.freqTV.setText(frequencyTextViewText);
}
return _convertView;
}
}
通过单击my
ListView项目中的某个图标
应删除相应的数据库行,并且应刷新
ListView

我的问题:

public class IOIOSensorCursorAdapterCam extends SimpleCursorAdapter
{
static class ViewHolder
{
ImageView stateIV, removeIV;
TextView nameTV, pinNumberTV, feedIDTV, freqTV;
}

private Context ctx;
private Cursor cursor;
private IodDatabaseManager dbm;

public IOIOSensorCursorAdapterCam(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)
{
    // Inflate a layout
    _convertView = inflater.inflate(R.layout.listview_item_sensor_cam,
        null);

    holder = new ViewHolder();
    holder.stateIV = (ImageView) _convertView
        .findViewById(R.id.stateImageView);
    holder.nameTV = (TextView) _convertView
        .findViewById(R.id.sensorNameTextView);
    holder.pinNumberTV = (TextView) _convertView
        .findViewById(R.id.sensorPinNumberTextView);
    holder.feedIDTV = (TextView) _convertView
        .findViewById(R.id.sensorFeedIDTextView);
    holder.freqTV = (TextView) _convertView
        .findViewById(R.id.sensorFrequencyTextView);
    holder.removeIV = (ImageView) _convertView
        .findViewById(R.id.removeImageView);
    _convertView.setTag(holder);
}
// We recycle a View that already exists.
else
{
    holder = (ViewHolder) _convertView.getTag();
}

// Set an OnClickListener to the "Delete Icon"
holder.removeIV.setOnClickListener(new OnClickListener()
{
    @SuppressWarnings("deprecation")
    @Override
    public void onClick(View _view)
    {
    cursor.moveToPosition(_position);

    // Delete sensor from database here
    int sensorID = cursor.getInt(cursor
        .getColumnIndex(IOIOSensorSchema.SENSOR_ID));
    dbm.deleteIOIOSensor(sensorID);

    // This leads to a "CursorIndexOutOfBoundsException" and cannot
    // be used to refresh the ListView
//      cursor = dbm.getIOIOSensorsCursor();

    // Refresh ListView
    cursor.requery();
    notifyDataSetChanged();
    }
});

cursor.moveToPosition(_position);

if (cursor.getCount() > 0)
{
    int state = cursor.getInt(cursor
        .getColumnIndex(IOIOSensorSchema.STATE));

    if (state == 0)
    {
    holder.stateIV.setImageResource(R.drawable.av_play_over_video);
    holder.stateIV.setColorFilter(ctx.getResources().getColor(
        R.color.hint_lighter_gray));
    // _convertView.setAlpha((float) 0.5);
    holder.nameTV.setTextColor(ctx.getResources().getColor(
        R.color.hint_darker_gray));
    }
    else
    {
    holder.stateIV.setImageResource(R.drawable.av_pause_over_video);
    holder.stateIV.setColorFilter(ctx.getResources().getColor(
        android.R.color.holo_green_light));
    // _convertView.setAlpha((float) 1);
    holder.nameTV.setTextColor(ctx.getResources().getColor(
        android.R.color.black));
    }

    // Set the sensor's name to the according TextView
    String sensorName = cursor.getString(cursor
        .getColumnIndex(IOIOSensorSchema.NAME));
    holder.nameTV.setText(sensorName);

    // Set the sensor's pin number to the according TextView
    int pinNumber = cursor.getInt(cursor
        .getColumnIndex(IOIOSensorSchema.PIN_NUMBER));
    holder.pinNumberTV.setText("" + pinNumber);

    // Set the sensor's feed ID to the according TextView
    int feedID = cursor.getInt(cursor
        .getColumnIndex(IOIOSensorSchema.FEED_ID));
    holder.feedIDTV.setText("" + feedID);

    // Set the sensor's frequency to the according TextView
    int frequency = cursor.getInt(cursor
        .getColumnIndex(IOIOSensorSchema.FREQUENCY));
    int timeUnit = cursor.getInt(cursor
        .getColumnIndex(IOIOSensorSchema.TIME_UNIT));
    String frequencyTextViewText = "";
    switch (timeUnit)
    {
    case IodIOIOSensor.TIME_UNIT_MINUTES:
    frequencyTextViewText = frequency + " min";
    break;
    case IodIOIOSensor.TIME_UNIT_HOURS:
    frequencyTextViewText = frequency + " h";
    break;
    default:
    frequencyTextViewText = frequency + " sec";
    break;
    }
    holder.freqTV.setText(frequencyTextViewText);
}
return _convertView;
}
}
如何正确刷新
光标
列表视图
?当我在我的
OnClickListener
中不使用
cursor.requery()
,而是使用
cursor=dbm.getIOIOSensorsCursors()
时,我会在下面的几行中得到一个
CursorIndexOutOfBoundsException

int state = cursor.getInt(cursor.getColumnIndex(IOIOSensorSchema.STATE));
我的应用程序崩溃,但在重新加载后,数据库已被删除,相应的
ListView项也不见了

我猜崩溃一定与get
getView
方法中的
\u位置
有关,因为
\u位置
最终的
。但是,当我使用cursor.requery()
时,一切都正常工作

但是这个方法已经被弃用了,它的文档上写着“不要使用这个…”。我是一个正确编码的朋友(我还是一个初学者,希望学会以正确的方式编码,而不是快速和肮脏),我想知道如何正确地编码。我不知道这是否重要,但我只在我(非常快的)Nexus4上测试我的应用程序。刷新
光标足够快似乎没有问题,但我想知道它是否能在较慢的设备上工作。如果这对您很重要,我的数据库将包含大约10-20行和大约12列。我想这是一个非常小的数据库

以下是我的自定义适配器的相关代码:

public class IOIOSensorCursorAdapterCam extends SimpleCursorAdapter
{
static class ViewHolder
{
ImageView stateIV, removeIV;
TextView nameTV, pinNumberTV, feedIDTV, freqTV;
}

private Context ctx;
private Cursor cursor;
private IodDatabaseManager dbm;

public IOIOSensorCursorAdapterCam(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)
{
    // Inflate a layout
    _convertView = inflater.inflate(R.layout.listview_item_sensor_cam,
        null);

    holder = new ViewHolder();
    holder.stateIV = (ImageView) _convertView
        .findViewById(R.id.stateImageView);
    holder.nameTV = (TextView) _convertView
        .findViewById(R.id.sensorNameTextView);
    holder.pinNumberTV = (TextView) _convertView
        .findViewById(R.id.sensorPinNumberTextView);
    holder.feedIDTV = (TextView) _convertView
        .findViewById(R.id.sensorFeedIDTextView);
    holder.freqTV = (TextView) _convertView
        .findViewById(R.id.sensorFrequencyTextView);
    holder.removeIV = (ImageView) _convertView
        .findViewById(R.id.removeImageView);
    _convertView.setTag(holder);
}
// We recycle a View that already exists.
else
{
    holder = (ViewHolder) _convertView.getTag();
}

// Set an OnClickListener to the "Delete Icon"
holder.removeIV.setOnClickListener(new OnClickListener()
{
    @SuppressWarnings("deprecation")
    @Override
    public void onClick(View _view)
    {
    cursor.moveToPosition(_position);

    // Delete sensor from database here
    int sensorID = cursor.getInt(cursor
        .getColumnIndex(IOIOSensorSchema.SENSOR_ID));
    dbm.deleteIOIOSensor(sensorID);

    // This leads to a "CursorIndexOutOfBoundsException" and cannot
    // be used to refresh the ListView
//      cursor = dbm.getIOIOSensorsCursor();

    // Refresh ListView
    cursor.requery();
    notifyDataSetChanged();
    }
});

cursor.moveToPosition(_position);

if (cursor.getCount() > 0)
{
    int state = cursor.getInt(cursor
        .getColumnIndex(IOIOSensorSchema.STATE));

    if (state == 0)
    {
    holder.stateIV.setImageResource(R.drawable.av_play_over_video);
    holder.stateIV.setColorFilter(ctx.getResources().getColor(
        R.color.hint_lighter_gray));
    // _convertView.setAlpha((float) 0.5);
    holder.nameTV.setTextColor(ctx.getResources().getColor(
        R.color.hint_darker_gray));
    }
    else
    {
    holder.stateIV.setImageResource(R.drawable.av_pause_over_video);
    holder.stateIV.setColorFilter(ctx.getResources().getColor(
        android.R.color.holo_green_light));
    // _convertView.setAlpha((float) 1);
    holder.nameTV.setTextColor(ctx.getResources().getColor(
        android.R.color.black));
    }

    // Set the sensor's name to the according TextView
    String sensorName = cursor.getString(cursor
        .getColumnIndex(IOIOSensorSchema.NAME));
    holder.nameTV.setText(sensorName);

    // Set the sensor's pin number to the according TextView
    int pinNumber = cursor.getInt(cursor
        .getColumnIndex(IOIOSensorSchema.PIN_NUMBER));
    holder.pinNumberTV.setText("" + pinNumber);

    // Set the sensor's feed ID to the according TextView
    int feedID = cursor.getInt(cursor
        .getColumnIndex(IOIOSensorSchema.FEED_ID));
    holder.feedIDTV.setText("" + feedID);

    // Set the sensor's frequency to the according TextView
    int frequency = cursor.getInt(cursor
        .getColumnIndex(IOIOSensorSchema.FREQUENCY));
    int timeUnit = cursor.getInt(cursor
        .getColumnIndex(IOIOSensorSchema.TIME_UNIT));
    String frequencyTextViewText = "";
    switch (timeUnit)
    {
    case IodIOIOSensor.TIME_UNIT_MINUTES:
    frequencyTextViewText = frequency + " min";
    break;
    case IodIOIOSensor.TIME_UNIT_HOURS:
    frequencyTextViewText = frequency + " h";
    break;
    default:
    frequencyTextViewText = frequency + " sec";
    break;
    }
    holder.freqTV.setText(frequencyTextViewText);
}
return _convertView;
}
}
编辑:

public class IOIOSensorCursorAdapterCam extends SimpleCursorAdapter
{
static class ViewHolder
{
ImageView stateIV, removeIV;
TextView nameTV, pinNumberTV, feedIDTV, freqTV;
}

private Context ctx;
private Cursor cursor;
private IodDatabaseManager dbm;

public IOIOSensorCursorAdapterCam(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)
{
    // Inflate a layout
    _convertView = inflater.inflate(R.layout.listview_item_sensor_cam,
        null);

    holder = new ViewHolder();
    holder.stateIV = (ImageView) _convertView
        .findViewById(R.id.stateImageView);
    holder.nameTV = (TextView) _convertView
        .findViewById(R.id.sensorNameTextView);
    holder.pinNumberTV = (TextView) _convertView
        .findViewById(R.id.sensorPinNumberTextView);
    holder.feedIDTV = (TextView) _convertView
        .findViewById(R.id.sensorFeedIDTextView);
    holder.freqTV = (TextView) _convertView
        .findViewById(R.id.sensorFrequencyTextView);
    holder.removeIV = (ImageView) _convertView
        .findViewById(R.id.removeImageView);
    _convertView.setTag(holder);
}
// We recycle a View that already exists.
else
{
    holder = (ViewHolder) _convertView.getTag();
}

// Set an OnClickListener to the "Delete Icon"
holder.removeIV.setOnClickListener(new OnClickListener()
{
    @SuppressWarnings("deprecation")
    @Override
    public void onClick(View _view)
    {
    cursor.moveToPosition(_position);

    // Delete sensor from database here
    int sensorID = cursor.getInt(cursor
        .getColumnIndex(IOIOSensorSchema.SENSOR_ID));
    dbm.deleteIOIOSensor(sensorID);

    // This leads to a "CursorIndexOutOfBoundsException" and cannot
    // be used to refresh the ListView
//      cursor = dbm.getIOIOSensorsCursor();

    // Refresh ListView
    cursor.requery();
    notifyDataSetChanged();
    }
});

cursor.moveToPosition(_position);

if (cursor.getCount() > 0)
{
    int state = cursor.getInt(cursor
        .getColumnIndex(IOIOSensorSchema.STATE));

    if (state == 0)
    {
    holder.stateIV.setImageResource(R.drawable.av_play_over_video);
    holder.stateIV.setColorFilter(ctx.getResources().getColor(
        R.color.hint_lighter_gray));
    // _convertView.setAlpha((float) 0.5);
    holder.nameTV.setTextColor(ctx.getResources().getColor(
        R.color.hint_darker_gray));
    }
    else
    {
    holder.stateIV.setImageResource(R.drawable.av_pause_over_video);
    holder.stateIV.setColorFilter(ctx.getResources().getColor(
        android.R.color.holo_green_light));
    // _convertView.setAlpha((float) 1);
    holder.nameTV.setTextColor(ctx.getResources().getColor(
        android.R.color.black));
    }

    // Set the sensor's name to the according TextView
    String sensorName = cursor.getString(cursor
        .getColumnIndex(IOIOSensorSchema.NAME));
    holder.nameTV.setText(sensorName);

    // Set the sensor's pin number to the according TextView
    int pinNumber = cursor.getInt(cursor
        .getColumnIndex(IOIOSensorSchema.PIN_NUMBER));
    holder.pinNumberTV.setText("" + pinNumber);

    // Set the sensor's feed ID to the according TextView
    int feedID = cursor.getInt(cursor
        .getColumnIndex(IOIOSensorSchema.FEED_ID));
    holder.feedIDTV.setText("" + feedID);

    // Set the sensor's frequency to the according TextView
    int frequency = cursor.getInt(cursor
        .getColumnIndex(IOIOSensorSchema.FREQUENCY));
    int timeUnit = cursor.getInt(cursor
        .getColumnIndex(IOIOSensorSchema.TIME_UNIT));
    String frequencyTextViewText = "";
    switch (timeUnit)
    {
    case IodIOIOSensor.TIME_UNIT_MINUTES:
    frequencyTextViewText = frequency + " min";
    break;
    case IodIOIOSensor.TIME_UNIT_HOURS:
    frequencyTextViewText = frequency + " h";
    break;
    default:
    frequencyTextViewText = frequency + " sec";
    break;
    }
    holder.freqTV.setText(frequencyTextViewText);
}
return _convertView;
}
}
以下是我在实现解决方案后从OnCickListener获得的相关代码:

// Set an OnClickListener to the "Delete Icon"
holder.removeIV.setOnClickListener(new OnClickListener()
{
    @Override
    public void onClick(View _view)
    {
    cursor.moveToPosition(_position);

    // Delete sensor from database here
    int sensorID = cursor.getInt(cursor
        .getColumnIndex(IOIOSensorSchema.SENSOR_ID));
    dbm.deleteIOIOSensor(sensorID);

    Toast.makeText(ctx, R.string.toast_sensor_deleted,
        Toast.LENGTH_SHORT).show();

    // Refresh ListView
    cursor = dbm.getIOIOSensorsCursor();
    swapCursor(cursor);

    notifyDataSetChanged();
    }
});
如何正确刷新光标和列表视图


您可以通过再次运行代码来“刷新[您的]光标”,以获得
光标
,使用用于创建原始
光标
(请在后台线程上)的代码。通过调用
CursorAdapter

上的
changeCursor()
swapCursor()
来刷新
ListView
。非常感谢你!所以我只缺少了
swapCursor(游标)!我将解决方案添加到我的帖子中。如果您在后台线程中提到
,请
。我很好奇,你是否想过在
异步任务
加载程序
中这样做?@mattblang:如果你碰巧有
内容提供者
出于其他原因提供数据,请使用
游标加载程序
。否则,只需使用
AsyncTask
,IMHO.@commonware当
CursorLoader
调用
swapCursor(cursor)
并且选中
列表视图中的一个或多个项目并且激活CAB时,会发生什么?我该把车开完吗?我可能错了,但我想用户会觉得有点奇怪,无论他们选择了什么项目,都会突然取消选择,因为
ContentProvider
检测到数据库中发生了一些更改。@Axel:这就是我不使用
加载程序
框架的原因之一。IMHO,只有当用户不在操作的中间时才更新UI。