android fragment hidenChanged方法分析

来源:互联网 发布:自己的社会网络 编辑:程序博客网 时间:2024/06/05 20:15

在fragment中,这个方法,每次在界面出现的时候都会执行,记录一下,防止以后忘了。

@Override

public void onHiddenChanged(boolean hidd) {
if (!hidd) {


if (getActivity() == null) {
return;
}


// 当新增价格提示成功之后,跳转回价格提示界面,更新数据和界面
inittipspriceData();
}

}


在这之前,我遇到这样的错误,就是切换fragment的时候,会报如下错误:

The content of the adapter has changed but ListView did not receive a notification. Make sure the content of your adapter is not modified from a background thread, but only from the UI thread. [in ListView(2131362078, class client.verbank.mtp.allone.frame.price.pricewarning.listview.ActionSlideExpandableListView) with Adapter(class client.verbank.mtp.allone.frame.price.pricewarning.listview.SlideExpandableListAdapter)]

这个错误困扰了很久,不过无非是因为数据没有及时更新,没有通知给适配器,后面我想到当你要更新界面的时候,你的数据和更新的fireTableDataChange();通知更新的语句最好是放到一起,这样就不会产生其他的错误,当然,事先判断适配器是否为null,也是必要的,代码如下:

@Overridepublic void onHiddenChanged(boolean hidd) {if (!hidd) {if (getActivity() == null) {return;} else {maps = initFirstData();fireTableDataChange();}}}public void fireTableDataChange() {getProgress().show();getHandler().post(new Runnable() {@Overridepublic void run() {// tableAdapter.notifyDataSetInvalidated();if (tipspriceAd != null) {tipspriceAd.notifyDataSetChanged();getProgress().dismiss();}}});}

=================================2015年10月21日09:32:25更新===================================

当你在getActivity()方法取到的是null的时候,你使用getString(R.String.id)取到的是空的,这个时候如何避免fragment生成的时候,activity还没有示例化,防止取到为空:解决方式是重启一个线程,可以使getActivity()不为空:

private void queryReport(final int leftThumbIndex) {getProgressDialog().show();new Thread() {@Overridepublic void run() {try {index = leftThumbIndex;queryOrderHisReport(leftThumbIndex);} finally {Runnable work = new Runnable() {@Overridepublic void run() {getProgressDialog().dismiss();}};getHandler().post(work);}}}.start();}
.getCorrespondingTicket());rowDataMap.put(ORDERHIS_TC_STICKETSTR,value.getCorrespondingTicket() < 0.000001 ? "": <span style="color:#ff0000;">getString(R.string.sticket));</span>
成功解决了。



0 0
原创粉丝点击