Android:如何在删除数据库行后重新调用Cursor来刷新ListView?

前端之家收集整理的这篇文章主要介绍了Android:如何在删除数据库行后重新调用Cursor来刷新ListView?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
这可能是一个noob问题,但我对于所有这些sqlite数据库 – 游标适配器 – ListView-Do-It-Properly-Stuff来说都是新的.

我拥有的:

在我的MainActivity我有一个ListView.我使用sqlite数据库,并使用扩展SimpleCursorAdapter的自定义适配器填充ListView.通过单击我的ActionBar中的项目,我激活上下文动作模式.一切工作到目前为止.

我想要的是:

通过点击ListView项目中的某个图标,应该删除相应的数据库行,并且应该刷新ListView.

我的问题:

如何正确刷新我的光标和我的ListView?当我不在我的OnClickListener中使用cursor.requery()并且使用cursor = dbm.getIOIOSensorsCursor()而不是在行下面的几行CursorIndexOutOfBoundsException

  1. int state = cursor.getInt(cursor.getColumnIndex(IOIOSensorSchema.STATE));

我的应用程序崩溃,但重新加载后,数据库已被删除,并且相应的ListView项目已经消失.

我猜,在get get方法中,崩溃必须与_position有关,因为_position是final.但是,当我使用cursor.requery()一切都可以正常工作.

但是这种方法已被弃用,文档说“不要使用…”.我是一个正确编码的朋友(我还是一个初学者,想要学习编码正确的方式,而不是快速而脏),并想知道如何做到这一点.我不知道这是否重要,但我只是在我的(真正快速的)Nexus 4上测试我的应用程序.似乎没有任何问题刷新光标足够快,但我不知道它是否可以在较慢的设备上工作.万一重要的是,我的数据库将包含大约10-20行和大约12列.我想这是一个非常小的数据库.

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

  1. public class IOIOSensorCursorAdapterCam extends SimpleCursorAdapter
  2. {
  3. static class ViewHolder
  4. {
  5. ImageView stateIV,removeIV;
  6. TextView nameTV,pinNumberTV,FeedIDTV,freqTV;
  7. }
  8.  
  9. private Context ctx;
  10. private Cursor cursor;
  11. private IodDatabaseManager dbm;
  12.  
  13. public IOIOSensorCursorAdapterCam(Context _context,int _layout,Cursor _cursor,String[] _from,int[] _to,int _flags)
  14. {
  15. super(_context,_layout,_cursor,_from,_to,_flags);
  16. ctx = _context;
  17. cursor = _cursor;
  18. dbm = new IodDatabaseManager(_context);
  19. }
  20.  
  21. @Override
  22. public View getView(final int _position,View _convertView,ViewGroup _parent)
  23. {
  24. ViewHolder holder = null;
  25.  
  26. LayoutInflater inflater = (LayoutInflater) ctx
  27. .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  28.  
  29. // There is no view at this position,we create a new one. In this case
  30. // by inflating an xml layout.
  31. if (_convertView == null)
  32. {
  33. // Inflate a layout
  34. _convertView = inflater.inflate(R.layout.listview_item_sensor_cam,null);
  35.  
  36. holder = new ViewHolder();
  37. holder.stateIV = (ImageView) _convertView
  38. .findViewById(R.id.stateImageView);
  39. holder.nameTV = (TextView) _convertView
  40. .findViewById(R.id.sensorNameTextView);
  41. holder.pinNumberTV = (TextView) _convertView
  42. .findViewById(R.id.sensorPinNumberTextView);
  43. holder.FeedIDTV = (TextView) _convertView
  44. .findViewById(R.id.sensorFeedIDTextView);
  45. holder.freqTV = (TextView) _convertView
  46. .findViewById(R.id.sensorFrequencyTextView);
  47. holder.removeIV = (ImageView) _convertView
  48. .findViewById(R.id.removeImageView);
  49. _convertView.setTag(holder);
  50. }
  51. // We recycle a View that already exists.
  52. else
  53. {
  54. holder = (ViewHolder) _convertView.getTag();
  55. }
  56.  
  57. // Set an OnClickListener to the "Delete Icon"
  58. holder.removeIV.setOnClickListener(new OnClickListener()
  59. {
  60. @SuppressWarnings("deprecation")
  61. @Override
  62. public void onClick(View _view)
  63. {
  64. cursor.moveToPosition(_position);
  65.  
  66. // Delete sensor from database here
  67. int sensorID = cursor.getInt(cursor
  68. .getColumnIndex(IOIOSensorSchema.SENSOR_ID));
  69. dbm.deleteIOIOSensor(sensorID);
  70.  
  71. // This leads to a "CursorIndexOutOfBoundsException" and cannot
  72. // be used to refresh the ListView
  73. // cursor = dbm.getIOIOSensorsCursor();
  74.  
  75. // Refresh ListView
  76. cursor.requery();
  77. notifyDataSetChanged();
  78. }
  79. });
  80.  
  81. cursor.moveToPosition(_position);
  82.  
  83. if (cursor.getCount() > 0)
  84. {
  85. int state = cursor.getInt(cursor
  86. .getColumnIndex(IOIOSensorSchema.STATE));
  87.  
  88. if (state == 0)
  89. {
  90. holder.stateIV.setImageResource(R.drawable.av_play_over_video);
  91. holder.stateIV.setColorFilter(ctx.getResources().getColor(
  92. R.color.hint_lighter_gray));
  93. // _convertView.setAlpha((float) 0.5);
  94. holder.nameTV.setTextColor(ctx.getResources().getColor(
  95. R.color.hint_darker_gray));
  96. }
  97. else
  98. {
  99. holder.stateIV.setImageResource(R.drawable.av_pause_over_video);
  100. holder.stateIV.setColorFilter(ctx.getResources().getColor(
  101. android.R.color.holo_green_light));
  102. // _convertView.setAlpha((float) 1);
  103. holder.nameTV.setTextColor(ctx.getResources().getColor(
  104. android.R.color.black));
  105. }
  106.  
  107. // Set the sensor's name to the according TextView
  108. String sensorName = cursor.getString(cursor
  109. .getColumnIndex(IOIOSensorSchema.NAME));
  110. holder.nameTV.setText(sensorName);
  111.  
  112. // Set the sensor's pin number to the according TextView
  113. int pinNumber = cursor.getInt(cursor
  114. .getColumnIndex(IOIOSensorSchema.PIN_NUMBER));
  115. holder.pinNumberTV.setText("" + pinNumber);
  116.  
  117. // Set the sensor's Feed ID to the according TextView
  118. int FeedID = cursor.getInt(cursor
  119. .getColumnIndex(IOIOSensorSchema.Feed_ID));
  120. holder.FeedIDTV.setText("" + FeedID);
  121.  
  122. // Set the sensor's frequency to the according TextView
  123. int frequency = cursor.getInt(cursor
  124. .getColumnIndex(IOIOSensorSchema.FREQUENCY));
  125. int timeUnit = cursor.getInt(cursor
  126. .getColumnIndex(IOIOSensorSchema.TIME_UNIT));
  127. String frequencyTextViewText = "";
  128. switch (timeUnit)
  129. {
  130. case IodioIOSensor.TIME_UNIT_MINUTES:
  131. frequencyTextViewText = frequency + " min";
  132. break;
  133. case IodioIOSensor.TIME_UNIT_HOURS:
  134. frequencyTextViewText = frequency + " h";
  135. break;
  136. default:
  137. frequencyTextViewText = frequency + " sec";
  138. break;
  139. }
  140. holder.freqTV.setText(frequencyTextViewText);
  141. }
  142. return _convertView;
  143. }
  144. }

编辑:

在实现解决方案之后,这是OnCickListener的相关代码

  1. // Set an OnClickListener to the "Delete Icon"
  2. holder.removeIV.setOnClickListener(new OnClickListener()
  3. {
  4. @Override
  5. public void onClick(View _view)
  6. {
  7. cursor.moveToPosition(_position);
  8.  
  9. // Delete sensor from database here
  10. int sensorID = cursor.getInt(cursor
  11. .getColumnIndex(IOIOSensorSchema.SENSOR_ID));
  12. dbm.deleteIOIOSensor(sensorID);
  13.  
  14. Toast.makeText(ctx,R.string.toast_sensor_deleted,Toast.LENGTH_SHORT).show();
  15.  
  16. // Refresh ListView
  17. cursor = dbm.getIOIOSensorsCursor();
  18. swapCursor(cursor);
  19.  
  20. notifyDataSetChanged();
  21. }
  22. });

解决方法

@H_301_36@

How do I refresh my Cursor and my ListView properly?

您可以通过再次运行代码获取光标,使用用于创建原始光标的代码(在后台线程上)刷新[您的]光标“.您通过调用CursorAdapter上的changeCursor()或swapCursor()来刷新ListView.

猜你在找的Android相关文章