1。
关于Adapter的notifyDataSetChanged()和notifyDataSetInvalidated()
今天写代码的时候遇到一个问题,后发现是由于错误的使用的notifyDataSetChanged()而导致的。
API上并不详细:
public void notifyDataSetChanged ()
Notifies the attached View that the underlying data has been changed and it should refresh itself.
public void notifyDataSetInvalidated ()
暂时理解为对一个Item进行数据改变的时候用notifyDataSetChanged,notifyDataSetChanged是有缓存的。
整个Adapter重置的时候使用notifyDataSetInvalidated 。
2。
关于存储卡的插拔监听:
private BroadcastReceiver broadcastRec;
// 自己写一个广播监听函数
broadcastRec = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.i("YT", intent.getAction());
if (intent.getAction().equals(
"android.intent.action.MEDIA_MOUNTED"))// SD卡已经成功挂载
{
// Log.i("YT", intent.getAction());
}
else if (intent.getAction().equals(
"android.intent.action.MEDIA_REMOVED")// 各种未挂载状态
|| intent.getAction().equals(
"android.intent.action.ACTION_MEDIA_UNMOUNTED")
|| intent
.getAction()
.equals(
"android.intent.action.ACTION_MEDIA_BAD_REMOVAL")) {
// Log.i("YT", intent.getAction());
}
}
};
// 在IntentFilter中选择你要监听的行为
IntentFilter intentFilter = new IntentFilter(
Intent.ACTION_MEDIA_MOUNTED);
intentFilter.addAction(Intent.ACTION_MEDIA_UNMOUNTED);
intentFilter.addAction(Intent.ACTION_MEDIA_REMOVED);
intentFilter.addAction(Intent.ACTION_MEDIA_BAD_REMOVAL);
intentFilter.addDataScheme("file");
registerReceiver(broadcastRec, intentFilter);// 注册监听函数
if (broadcastRec != null) {
unregisterReceiver(broadcastRec);// 使用完注销广播监听函数
}
本文解析了Android中Adapter的两种数据更新方法:notifyDataSetChanged()与notifyDataSetInvalidated()的区别及应用场景,并提供了存储卡插拔状态的监听实现。
6800

被折叠的 条评论
为什么被折叠?



