ResultAdapter(List<Result> prevTest) { super(getActivity(), 0, prevTest); }
@Override public void run() { //attempt to fix bug: if (getListAdapter() == null) return; // try { getListView().setSelection(getListAdapter().getCount() - 1); // } catch (IllegalStateException e) { // } } });
/** * Called when the fragment's activity has been created and this * fragment's view hierarchy instantiated. It can be used to do final * initialization once these pieces are in place, such as retrieving * views or restoring state. It is also useful for fragments that use * {@link #setRetainInstance(boolean)} to retain their instance, * as this callback tells the fragment when it is fully associated with * the new activity instance. This is called after {@link #onCreateView} * and before {@link #onViewStateRestored(Bundle)}. * * @param savedInstanceState If the fragment is being re-created from * a previous saved state, this is the state. */ @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); mResultArray = Calculator.getCalculator(getActivity()).getResultList(); ResultAdapter adapter = new ResultAdapter(mResultArray); setListAdapter(adapter); }
convertView = getActivity().getLayoutInflater(). inflate(R.layout.list_item_result, parent, false); textViewUnitTimestamp.setVisibility(View.GONE); if (result != null && result.containsUnits()){ String text = getResources().getString(R.string.word_Converted) + " " + result.getQueryUnitTextLong() + " " + getResources().getString(R.string.word_to) + " " + result.getAnswerUnitTextLong() + ":"; textViewUnitDesc.setText(ViewUtils.fromHtml("<i>" + text + "</i>"));
Toast toast = Toast.makeText(getActivity(), "ERROR: onClick parameter view is null", Toast.LENGTH_LONG); toast.show(); return; int position = getListView().getPositionForView((View) view.getParent()); Calculator calc = Calculator.getCalculator(getActivity());
@Override public boolean onLongClick(View view) { Toast toast = Toast.makeText(view.getContext(), "Result Deleted", Toast.LENGTH_SHORT); toast.setGravity(Gravity.CENTER, 0, 0); toast.show(); //get the listView position of this answer/query int position = getListView().getPositionForView((View) view.getParent()); //delete associated previous expression mResultArray.remove(position); mCallback.updateScreen(true); return false; } });
/** * Grabs newest data from Calculator, updates the main display, and gives an * option to scroll down the result list * * @param updateResult pass true to update result list * @param instantScroll pass true to scroll instantly, otherwise use animation */ private void updateScreen(boolean updateResult, boolean instantScroll) { mDisplay.updateTextFromCalc(); //Update EditText view //will preview become visible during this screen update? boolean makePreviewVisible = !mCalc.isSolved() && !mCalc.isPreviewEmpty() && !mCalc.isUnitSelected(); //if preview just appeared, move the history list up so the last item //doesn't get hidden by the preview if (mResultPreview.getVisibility() != View.VISIBLE && makePreviewVisible){ updateResult = true; instantScroll = true; } mResultPreview.setVisibility(makePreviewVisible ? View.VISIBLE : View.GONE); updatePreviewText(ContextCompat.getColor(mAppContext, R.color.preview_si_suffix_text_color)); //if we hit equals, update result list if (updateResult) mResultListFrag.refresh(instantScroll); }
mResultListFrag = new ResultListFragment(); fm.beginTransaction().add(R.id.resultListFragmentContainer, mResultListFrag).commit();
/** * Update the listView and scroll to the bottom * @param instantScroll if false, use animated scroll to bottom, * otherwise use scroll instantly */ public void refresh(boolean instantScroll) { if (getListAdapter() == null) return; //notify the adapter that the listview needs to be updated ((ResultAdapter) getListAdapter()).notifyDataSetChanged(); //scroll to the bottom of the list if (instantScroll){ //post a runnable for setSelection otherwise it won't be called getListView().post(new Runnable() { @Override public void run() { //attempt to fix bug: if (getListAdapter() == null) return; // try { getListView().setSelection(getListAdapter().getCount() - 1); // } catch (IllegalStateException e) { // } } }); } else getListView().smoothScrollToPosition(getListAdapter().getCount() - 1); } }