/** * onCreate method Once a user enter this activity, this method will give each view an object to * help other methods set data or listeners. * * @param savedInstanceState The saved instance state bundle. */ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_my_question); sortOptionSpinner = (Spinner) findViewById(R.id.my_question_sort_spinner); mListView = (ScrollListView) findViewById(R.id.my_question_ListView); mListView.setPullLoadEnable(false); mHandler = new Handler(); mcontext = this; }
/** stop refresh and loading, reset header and the footer view. */ private void onLoad() { timestamp = new Date(); mListView.stopRefresh(); mListView.stopLoadMore(); mListView.setRefreshTime(timestamp.toString()); }
/** * onStart method Setup the adapter for the user's question list, and setup listener for each item * (question) in the user's question list. */ @Override public void onStart() { super.onStart(); cacheController = new CacheController(mcontext); authorMapController = new AuthorMapController(mcontext); authorMapManager = new AuthorMapManager(); myQuestionListController = new QuestionListController(); myQuestionListManager = new QuestionListManager(); adapter = new QuestionListAdapter( mcontext, R.layout.single_question, myQuestionListController.getQuestionArrayList()); adapter.setSortingOption(sortByDate); spinAdapter = new ArrayAdapter<String>(mcontext, R.layout.spinner_item, sortOption); mListView.setAdapter(adapter); sortOptionSpinner.setAdapter(spinAdapter); sortOptionSpinner.setOnItemSelectedListener(new change_category_click()); updateList(); /** * Jump to the layout of the choosen question, and show details when click on an item (a * question) in the favorite question list */ mListView.setOnItemClickListener( new OnItemClickListener() { /** * display the layout of the chosen question, and show details when click on an item (a * question) in the searching result list * * @param parent The adapter of the item in the list. * @param view The view. * @param pos The position of a question. * @param id The ID of a question. */ @Override public void onItemClick(AdapterView<?> parent, View view, int pos, long id) { long questionID = myQuestionListController.getQuestion(pos - 1).getID(); Intent intent = new Intent(mcontext, QuestionDetailActivity.class); intent.putExtra(QuestionDetailActivity.QUESTION_ID, questionID); intent.putExtra(QuestionDetailActivity.CACHE_LIST, cacheList); startActivity(intent); } }); /** Delete an item (a question) in the favorite list when a user long clicks the question. */ mListView.setOnItemLongClickListener( new OnItemLongClickListener() { /** * If the user is the author of the question, and the user long click the item (the * question) in the question list, then remove the selected question from the question * list. * * @param parent The adapter of the item in the list. * @param view The view. * @param pos The position of a question. * @param id The ID of a question. */ @Override public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { Question question = myQuestionListController.getQuestion(position - 1); if (User.author != null && User.author.getUserId() == question.getUserId()) { Toast.makeText( mcontext, "Deleting the Question: " + question.getTitle(), Toast.LENGTH_LONG) .show(); Thread thread = new DeleteThread(question.getID()); thread.start(); } else { Toast.makeText(mcontext, "Only Author to the Question can delete", Toast.LENGTH_LONG) .show(); } return true; } }); /** Update the current questions on screen, if a user scroll his/her favorite question list */ mListView.setScrollListViewListener( new IXListViewListener() { /** * Will called to update the content in the favorite question list when the data is * changed or sorted; also, this method will tell the user the current interval of the * question that are displayed on the screen */ @Override public void onRefresh() { mHandler.postDelayed( new Runnable() { @Override public void run() { adapter.notifyDataSetChanged(); onLoad(); } }, 2000); } /** * this method will be called when a user up or down scroll the favorite question list to * update the corresponding questions on the screen; also, this method will tell the user * the current interval of the question that are displayed on the screen */ @Override public void onLoadMore() { mHandler.postDelayed( new Runnable() { @Override public void run() { adapter.notifyDataSetChanged(); onLoad(); } }, 2000); } }); }