标签:android java 值传递 基本数据类型 integer
为了提高代码的封装性及可读性,我把原来手懒搞的一些AsyncTask的继承内部类决定都单独拉到一个文件夹中,但这一拉,出事情了!
我的应用业务是,有一个min_question_id(int )来记录目前读取到的服务器端数据,原来是内部类的时候,用的好好的,这把它单独剥离出来,每次拉取数据却是从头拉取了!
好了,先上原来的代码!
public class getQuestionListDataTask extends AsyncTask<Void, Void, Void> { private static final int GETREFRESUCCESS=5000; private static final int GETREQUESTERROR=5001; private static final int DATASETEMPTY=5002; PullToRefreshListView mPullToRefreshLayout; List<HashMap<String, Object>> listItemQuestion; boolean isFirstEnter; int min_question_id; QuestionListAdapter mQuestionListAdapter; /* * mpPullToRefreshListView:异步刷新工作对应的PullToRefreshListView * mList:PullToRefreshListView对应的后台数据引用 * isfirstEnter:记录是否第一次开机后第一次进入app * min_question_id:获取问题列表时候的最小id * mQuestionListAdapter:驱动mpPullToRefreshListView的适配器 */ public getQuestionListDataTask(PullToRefreshListView mPullToRefreshListView,List<HashMap<String, Object>> mList, booleanisfirstEnter,int min_question_id,QuestionListAdapter mQuestionListAdapter) { super(); this.mPullToRefreshLayout=mPullToRefreshListView; this.listItemQuestion=mList; this.isFirstEnter=isfirstEnter; this.min_question_id=min_question_id; this.mQuestionListAdapter=mQuestionListAdapter; } @Override protected Void doInBackground(Void... arg0) { // TODO Auto-generated method stub if(mPullToRefreshLayout.isHeaderShown()) { listItemQuestion.clear(); getQuestionListData(0, true); isFirstEnter=false; } if(mPullToRefreshLayout.isFooterShown()) { getQuestionListData(min_question_id,false); } else { listItemQuestion.clear(); getQuestionListData(0, true); //getHttpData(min_question_id,false); } return null; } @Override protected void onPostExecute(Void result) { // TODO Auto-generated method stub mQuestionListAdapter.notifyDataSetChanged(); mPullToRefreshLayout.onRefreshComplete(); super.onPostExecute(result); }
}里面的getQuesionListData是一个拉取网络数据的接口,由于大家都懂的的原因,我就不贴出来啦~~我的要求是每一次拉取完数据(实例化一次getQuesionListDataTask并执行),都在getQuesionListData更新min_question_id的值。
显然,数据每次都还从头拉的原因就是这个min_question_id并没有得到更新!
java的传递不是值传递吗?!
是吗?!不是,这只是我的一个误区。
①基本类型变量,包括char、byte、short、int、long、float、double、boolean 。
②非基本变量。
而非基本变量是引用传递,基本变量是值传递!
那怎么办呢?难道我们要把char、byte、short、int、long、float、double、boolean这些东西自己写个wrapper把它们wrap成类?却是是这样的!但是我们不用自己写,java自身就带有这些包,就是这些基本类型的包装类!
分别和上面基本变量类型对应的是:Character,Byte,Short,Integer,Long,Float,Double,Boolean。
而且支持自动wrap/unwrap,妈妈再也不用担心我的手指了!
这样,我们就可以在需要在函数或类内部改变基本变量的值时,将其里面和外面的基本类型变量类型都改为包装类名~
于是,我现在的代码是这样的:
外部类:
public class AskFragment extends Fragment implements OnClickListener { private static Boolean isFirstEnter=true; private Integer min_question_id=1000; private List<HashMap<String, Object>> listItemQuestion = new ArrayList<HashMap<String, Object>>(); private View rootView; private View headerView; private ImageButton mSearchButton,mAddButton,headPicButton; private PullToRefreshListView mPullToRefreshLayout; QuestionListAdapter mQuestionListAdapter; public void onActivityCreated(Bundle savedInstanceState) { new getQuestionListDataTask(mPullToRefreshLayout, listItemQuestion, isFirstEnter, min_question_id, mQuestionListAdapter).execute(); } } }getQuestionListDataTask是这样的:
public class getQuestionListDataTask extends AsyncTask<Void, Void, Void> { private static final int GETREFRESUCCESS=5000; private static final int GETREQUESTERROR=5001; private static final int DATASETEMPTY=5002; PullToRefreshListView mPullToRefreshLayout; List<HashMap<String, Object>> listItemQuestion; Boolean isFirstEnter; Integer min_question_id; QuestionListAdapter mQuestionListAdapter; /* * mpPullToRefreshListView:异步刷新工作对应的PullToRefreshListView * mList:PullToRefreshListView对应的后台数据引用 * isfirstEnter:记录是否第一次开机后第一次进入app * min_question_id:获取问题列表时候的最小id * mQuestionListAdapter:驱动mpPullToRefreshListView的适配器 */ public getQuestionListDataTask(PullToRefreshListView mPullToRefreshListView,List<HashMap<String, Object>> mList, Boolean isfirstEnter,Integer min_question_id,QuestionListAdapter mQuestionListAdapter) { super(); this.mPullToRefreshLayout=mPullToRefreshListView; this.listItemQuestion=mList; this.isFirstEnter=isfirstEnter; this.min_question_id=min_question_id; this.mQuestionListAdapter=mQuestionListAdapter; } @Override protected Void doInBackground(Void... arg0) { // TODO Auto-generated method stub if(mPullToRefreshLayout.isHeaderShown()) { listItemQuestion.clear(); getQuestionListData(0, true); isFirstEnter=false; } if(mPullToRefreshLayout.isFooterShown()) { getQuestionListData(min_question_id,false); } else { listItemQuestion.clear(); getQuestionListData(0, true); //getHttpData(min_question_id,false); } return null; } @Override protected void onPostExecute(Void result) { // TODO Auto-generated method stub mQuestionListAdapter.notifyDataSetChanged(); mPullToRefreshLayout.onRefreshComplete(); super.onPostExecute(result); }
}就这样就搞定了,有木有!列表正常显示了!
说明:由于只是示例作用,代码截自我的项目,很多东西删去了,也有的变量没初始化,但大家不要介意这些细节~
Android 之Java基础---一个列表框引发的血案暨java装箱拆箱的实际应用
标签:android java 值传递 基本数据类型 integer
原文地址:http://blog.csdn.net/tianyuan521521/article/details/45542803