自定义Preference时遇到的问题

在自定义Preference时需要使用一个完全不同的布局,在使用的过程中遇到一个问题,就是数据刷新不了.找到很长时间,现在总结一下原因,以便后续犯了类似的错误.

 
 上面图中的红色部分就是我想要的效果的未完成的样子,本意是想传递4个float 类型的数字后,可以把手机存储的使用情况显示出来.
但是发现数据也已经传递成功了,对应的textview 打印的值也是期望的xxGB ,但是实际显示的确实错误的.
下面贴上主要的源码:
DoovInternalStorageItemPreference.java 是自定义的 Preference
   
   
  1. public class DoovInternalStorageItemPreference extends Preference {
  2. private final String TAG = DoovInternalStorageItemPreference.class.getSimpleName();
  3. private TextView mTotalSpace ;
  4. private TextView mOthersSpace ;
  5. private TextView mUsedSpace ;
  6. private TextView mAvaliableSpace ;
  7. private float total;
  8. private float others;
  9. private float used;
  10. private float avaliable;
  11. public DoovInternalStorageItemPreference(Context context,
  12. AttributeSet attrs, int defStyleAttr) {
  13. this(context, attrs, defStyleAttr,0);
  14. }
  15. public DoovInternalStorageItemPreference(Context context, AttributeSet attrs) {
  16. this(context, attrs,0);
  17. }
  18. public DoovInternalStorageItemPreference(Context context) {
  19. this(context,null);
  20. }
  21. public DoovInternalStorageItemPreference(Context context,
  22. AttributeSet attrs, int defStyleAttr, int defStyleRes) {
  23. super(context, attrs, defStyleAttr, defStyleRes);
  24. setLayoutResource(R.layout.doov_preference_internal_stroge);
  25. Log.d(TAG,"DoovInternalStorageItemPreference mTotalSpace=" + mTotalSpace);
  26. }
  27. @Override
  28. protected View onCreateView(ViewGroup parent) {
  29. View view = super.onCreateView(parent);
  30. Log.d(TAG,"onCreateView=" + mTotalSpace);
  31. return view;
  32. }
  33. @Override
  34. protected void onBindView(View view) {
  35. super.onBindView(view);
  36. mTotalSpace = (TextView)view.findViewById(R.id.total_storage);
  37. mOthersSpace = (TextView)view.findViewById(R.id.others_storage);
  38. mUsedSpace = (TextView)view.findViewById(R.id.used_storage);
  39. mAvaliableSpace = (TextView)view.findViewById(R.id.available_storage);
  40. Log.d(TAG,"onBindView=" + mTotalSpace);
  41. }
  42. public void setData(float total,float other,float used,float avaliable){
  43. this.total = (float)(Math.round(total*100))/100;//保留2个小数点
  44. this.others = (float)(Math.round(other*100))/100;//保留2个小数点
  45. this.used = (float)(Math.round(used*100))/100;//保留2个小数点
  46. this.avaliable = (float)(Math.round(avaliable*100))/100;//保留2个小数点
  47. Log.d(TAG,"total=" + mTotalSpace.getText().toString() +
  48. "others=" + others + "other=" + other);
  49. //error
  50. mTotalSpace.setText(getContext().getString(R.string.memoryusage_item_total_space, this.total));
  51. mOthersSpace.setText(getContext().getString(R.string.memoryusage_item_available_space, this.others));
  52. mUsedSpace.setText(getContext().getString(R.string.memoryusage_item_used_space, this.used));
  53. mAvaliableSpace.setText(getContext().getString(R.string.memoryusage_item_other_space, this.avaliable));
  54. notifyChanged();
  55. /*
  56. //ok
  57. mTotalSpace.setText(getContext().getString(R.string.memoryusage_item_total_space, total));
  58. mOthersSpace.setText(getContext().getString(R.string.memoryusage_item_available_space, other));
  59. mUsedSpace.setText(getContext().getString(R.string.memoryusage_item_used_space, used));
  60. mAvaliableSpace.setText(getContext().getString(R.string.memoryusage_item_other_space, avaliable));*/
  61. }
  62. }
光键就是上面的这个自定义的Preference.它的setData 中有2种写法,一种是带notifyChange(),另外一种不是.第一种是错误的.第2种是错误的.
原因就是调用notifyChange() 之后会导致 DoovInternalStorageItemPreference 的onCreateView() 和onBindView()重新执行.也就相当于layout被重新加载了.所以手机显示的值就是配置的xml 里面的默认的字符.
去掉notifyChange() 就恢复正常了.

其实这样还是有其他问题.继续看调用setData() 的地方
   
   
  1. public class DoovPreferenceActivity extends PreferenceActivity {
  2. private DoovInternalStorageItemPreference mpreference ;
  3. @Override
  4. protected void onCreate(Bundle savedInstanceState) {
  5. super.onCreate(savedInstanceState);
  6. addPreferencesFromResource(R.xml.test_preferencescreen);
  7. mpreference = new DoovInternalStorageItemPreference(this.getApplicationContext());
  8. //mpreference.setData(7, 8, 9, 10);
  9. getPreferenceScreen().addPreference(mpreference);
  10. }
  11. @Override
  12. protected void onResume() {
  13. super.onResume();
  14. new Handler().postDelayed(new Runnable() {
  15. @Override
  16. public void run() {
  17. mpreference.setData(17, 18, 19, 20);
  18. }
  19. }, 200);
  20. //mpreference.setData(17, 18, 19, 20);
  21. }
  22. }
在onResume 里面有2个调用setData() 的方法的地方.在上面代码的前提下,第2个是会报错的.但是第一种方法有不常用,怎么办?
所以正确的写法是应该在setData() 里面只记录下相关float 类型的值,将TextView 相关的数据更新操作放在Preference 的 onBindView() 里面去.

   
   
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:settings="http://schemas.android.com/apk/res/com.android.settings"
  4. android:layout_width="match_parent"
  5. android:layout_height="wrap_content">
  6. <TextView
  7. android:id="@+id/total_storage"
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content"
  10. android:layout_marginLeft="16dip"
  11. android:layout_marginTop="10dip"
  12. android:layout_alignParentTop="true"
  13. android:layout_alignParentLeft="true"
  14. android:text="@string/memoryusage_item_total_space"
  15. android:textColor="#999999"
  16. android:textSize="14sp" />
  17. <TextView
  18. android:id="@+id/others_storage"
  19. android:layout_width="wrap_content"
  20. android:layout_height="wrap_content"
  21. android:layout_marginTop="10dip"
  22. android:layout_alignParentTop="true"
  23. android:layout_toRightOf="@id/total_storage"
  24. android:layout_alignLeft="@+id/available_storage"
  25. android:text="@string/memoryusage_item_other_space"
  26. android:textColor="#999999"
  27. android:textSize="14sp" />
  28. <TextView
  29. android:id="@+id/available_storage"
  30. android:layout_width="wrap_content"
  31. android:layout_height="wrap_content"
  32. android:layout_marginLeft="32dip"
  33. android:layout_marginTop="10dip"
  34. android:layout_marginBottom="10dip"
  35. android:layout_toRightOf="@+id/used_storage"
  36. android:layout_below="@+id/others_storage"
  37. android:text="@string/memoryusage_item_available_space"
  38. android:textColor="#999999"
  39. android:textSize="14sp" />
  40. <TextView
  41. android:id="@+id/used_storage"
  42. android:layout_width="wrap_content"
  43. android:layout_height="wrap_content"
  44. android:layout_marginLeft="16dip"
  45. android:layout_marginTop="10dip"
  46. android:layout_marginBottom="10dip"
  47. android:layout_alignParentLeft="true"
  48. android:layout_below="@id/total_storage"
  49. android:text="@string/memoryusage_item_used_space"
  50. android:textColor="#999999"
  51. android:textSize="14sp" />
  52. </RelativeLayout>


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值