Java basics of Android-a blood case caused by a list box and Practical Application of java packing and unpacking

Source: Internet
Author: User

Java basics of Android-a blood case caused by a list box and Practical Application of java packing and unpacking

To improve code encapsulation and readability, I pulled some of the AsyncTask inheritance internal classes that I had previously worked on to a single folder, but this pull made a mistake!

My application business is that there is a min_question_id (int) to record the currently read server-side data. It was used well when it was originally an internal class, which separated it separately, data is pulled from the beginning every time!

Okay, first go to the original code!

 

Public class getQuestionListDataTask extends AsyncTask
 
  
{Private static final int GETREFRESUCCESS = 5000; private static final int GETREQUESTERROR = 5001; private static final int DATASETEMPTY = 5002; PullToRefreshListView mPullToRefreshLayout; List
  
   
> ListItemQuestion; boolean isFirstEnter; int min_question_id; QuestionListAdapter mQuestionListAdapter;/** restart: the background data reference * isfirstEnter: record whether to enter app * min_question_id for the first time after startup: obtain the minimum id * mQuestionListAdapter for the problem List: adapter */public getQuestionListDataTask (PullToRefreshListView mPullToRefreshListView, List
   
    
> 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 in is an interface for pulling network data. I will not post it for reasons that everyone understands ~~ My requirement is that the value of min_question_id is updated in getQuesionListData every time the data is pulled (the getquesionlistask ask is instantiated and executed.

 

Obviously, the reason why the data is pulled from the beginning is that this min_question_id is not updated!

Does java pass the value ?!

Yes ?! No, this is just a misunderstanding.

① Basic type variables, including char, byte, short, int, long, float, double, and boolean.

② Non-basic variables.

Instead of passing reference, the basic variable is passing value!

What should we do? Do we need to write wrapper into a class for char, byte, short, int, long, float, double, and boolean? But it is like this! But we don't need to write it by ourselves. java itself carries these packages, which are the basic types of packaging classes!

The following types correspond to the preceding basic variables: Character, Byte, Short, Integer, Long, Float, Double, and Boolean.

In addition, it supports automatic wrap/unwrap, so Mom no longer needs to worry about my fingers!

In this way, when we need to change the value of a basic variable within a function or class, we can change both the internal and external basic type variable types to the packaging class name ~

Therefore, my current Code is as follows:

External class:

 

public  class AskFragment extends Fragment implements OnClickListener   {private static Boolean isFirstEnter=true;private  Integer min_question_id=1000;private List
 
  > listItemQuestion = new ArrayList
  
   >();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 is as follows:

 

 

Public class getQuestionListDataTask extends AsyncTask
 
  
{Private static final int GETREFRESUCCESS = 5000; private static final int GETREQUESTERROR = 5001; private static final int DATASETEMPTY = 5002; PullToRefreshListView mPullToRefreshLayout; List
  
   
> ListItemQuestion; Boolean isFirstEnter; Integer min_question_id; QuestionListAdapter mQuestionListAdapter;/** restart: the background data reference * isfirstEnter: record whether to enter app * min_question_id for the first time after startup: obtain the minimum id * mQuestionListAdapter for the problem List: adapter */public getQuestionListDataTask (PullToRefreshListView mPullToRefreshListView, List
   
    
> 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 );}
   
  
 
}
That's it! The list is displayed normally!

 

Note: Because it only serves as an example, many items are removed from the Code cut-off project, and some variables are not initialized, but do not mind these details ~

 

 

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.