標籤:android java 值傳遞 基礎資料型別 (Elementary Data Type) 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;}@Overrideprotected Void doInBackground(Void... arg0) {// TODO Auto-generated method stubif(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;}@Overrideprotected void onPostExecute(Void result) {// TODO Auto-generated method stubmQuestionListAdapter.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;}@Overrideprotected Void doInBackground(Void... arg0) {// TODO Auto-generated method stubif(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;}@Overrideprotected void onPostExecute(Void result) {// TODO Auto-generated method stubmQuestionListAdapter.notifyDataSetChanged();mPullToRefreshLayout.onRefreshComplete();super.onPostExecute(result);}
}
就這樣就搞定了,有木有!列表正常顯示了!
說明:由於只是樣本作用,代碼截自我的項目,很多東西刪去了,也有的變數沒初始化,但大家不要介意這些細節~
Android 之Java基礎---一個列表框引發的血案暨java裝箱拆箱的實際應用