Java tutorial
/* * Copyright (C) 2011 Chris Gao <chris@exina.net> * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.cnm.cnmrc.fragment.search; import java.io.ByteArrayOutputStream; import java.util.ArrayList; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.os.AsyncTask; import android.os.Bundle; import android.support.v4.app.Fragment; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.animation.Animation; import android.view.animation.Animation.AnimationListener; import android.view.animation.AnimationUtils; import android.widget.AdapterView; import android.widget.FrameLayout; import android.widget.ImageView; import android.widget.ListView; import android.widget.Toast; import com.cnm.cnmrc.MainActivity; import com.cnm.cnmrc.R; import com.cnm.cnmrc.fragment.vodtvch.VodSemiDetailAdapter; import com.cnm.cnmrc.parser.SearchVod; import com.cnm.cnmrc.parser.SearchVodList; import com.cnm.cnmrc.parser.SearchVodParser; import com.cnm.cnmrc.util.UrlAddress; import com.cnm.cnmrc.util.Util; public class SearchVodSub extends Fragment implements View.OnClickListener { public static SearchVodSub newInstance(String search) { SearchVodSub f = new SearchVodSub(); Bundle args = new Bundle(); args.putString("search", search); // vod or tvch f.setArguments(args); return f; } FrameLayout preventClickDispatching; // ? ?? ?? . ListView listView; VodSemiDetailAdapter adapter; ArrayList<SearchVod> mResult = null; String search; /** * ? * 1) ? ?? ? Api?? . * 2) ? Activity Fragment? ??? ? ? ? ?? . ( ?? ? ?...) * 3) ?? ? ? elements ?? . ? ?. (, VOD_ID --> ID) * : ?? ? ? ? ? ? . "_"? . (, areaCode) * : element? ? string ? ? ? ? "_"? . (, AREA_CODE) * 4) url? ?? ? ?. */ @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View layout = inflater.inflate(R.layout.vod_semidetail, container, false); preventClickDispatching = (FrameLayout) layout.findViewById(R.id.search_vod_prevent_click_dispatching); preventClickDispatching.setOnClickListener(this); // listview listView = (ListView) layout.findViewById(R.id.vod_semidetail); listView.setDivider(null); listView.setDividerHeight(0); listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Fragment f = getActivity().getSupportFragmentManager() .findFragmentByTag(((MainActivity) getActivity()).TAG_FRAGMENT_SEARCH); if (f != null) { Bundle bundle = makeBundle(view, position); ((SearchMain) f).showDetailVod(bundle); } } }); return layout; } private Bundle makeBundle(View view, int position) { String title = adapter.getItem(position).getTitle(); String hd = adapter.getItem(position).getHd(); String grade = adapter.getItem(position).getGrade(); String director = adapter.getItem(position).getDirector(); String actor = adapter.getItem(position).getActor(); String price = adapter.getItem(position).getPrice(); String contents = adapter.getItem(position).getContents(); ImageView logo = (ImageView) view.findViewById(R.id.logo_img); Bitmap logoImage = ((BitmapDrawable) logo.getDrawable()).getBitmap(); // ? no image ? ?? . Bundle bundle = new Bundle(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); Bitmap bitmap = logoImage; boolean result = bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos); if (result) { // ?? ... not all Formats support all bitmap configs directly } else { bitmap = Util.getNoBitmap(getActivity()); bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos); } byte[] logoImg = baos.toByteArray(); bundle.putByteArray("logoImg", logoImg); bundle.putString("title", title); bundle.putString("hd", hd); bundle.putString("grade", grade); bundle.putString("director", director); bundle.putString("actor", actor); bundle.putString("price", price); bundle.putString("contents", contents); return bundle; } @Override public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) { Animation anim; if (enter) anim = AnimationUtils.loadAnimation(getActivity(), R.anim.fragment_entering); else anim = AnimationUtils.loadAnimation(getActivity(), R.anim.fragment_exit); anim.setAnimationListener(new AnimationListener() { public void onAnimationStart(Animation animation) { } public void onAnimationEnd(Animation animation) { ((MainActivity) getActivity()).getMyProgressBar().show(); showSearchVod(); } public void onAnimationRepeat(Animation animation) { } }); return anim; } private void showSearchVod() { // check network and data loading if (Util.GetNetworkInfo(getActivity()) == 99) { Util.AlertShow(getActivity()); } else { search = getArguments().getString("search"); new SearchVodAsyncTask().execute(search); } } long elapsedTime; SearchVodList list; private class SearchVodAsyncTask extends AsyncTask<String, Void, ArrayList<SearchVod>> { @Override protected void onPreExecute() { super.onPreExecute(); } protected ArrayList<SearchVod> doInBackground(String... params) { String requestURL = UrlAddress.Search.getSearchVod(params[0], "0", "0", "TitleAsc"); SearchVodParser parser = new SearchVodParser(requestURL, getActivity()); elapsedTime = System.currentTimeMillis(); parser.start(); Log.i("hwang", "elapsedTime of search vod : " + (System.currentTimeMillis() - elapsedTime)); list = parser.getList(); return parser.getList().getList(); } // onPostExecute displays the results of the AsyncTask. @Override protected void onPostExecute(ArrayList<SearchVod> result) { mResult = result; if (mResult == null) onCancelled(); else { Log.i("hwang", "search vod result count : " + list.getTotalCount()); if (!list.getResultCode().equals("100")) { String desc = Util.getErrorCodeDesc(list.getResultCode()); Toast.makeText(getActivity(), desc, Toast.LENGTH_LONG).show(); } else { adapter = new VodSemiDetailAdapter(getActivity(), R.layout.list_item_vod_semidetail, mResult); listView.setAdapter(adapter); } ((MainActivity) getActivity()).getMyProgressBar().dismiss(); } } @Override protected void onCancelled() { Util.onCancelled(getActivity()); super.onCancelled(); } } @Override public void onDestroyView() { super.onDestroyView(); // ---------------------------------------------------------------------------------------------- // vod? ?? vodtopmenu fragment vod destory? ??? vodtopmenu? ? destory . // ? ???? ? fragment ? vod? ? ? ? Activity? . // ---------------------------------------------------------------------------------------------- { Fragment f = getActivity().getSupportFragmentManager().findFragmentByTag("search_vod_detail"); if (f != null) { getActivity().getSupportFragmentManager().beginTransaction().remove(f).commit(); } } } @Override public void onAttach(Activity activity) { super.onAttach(activity); } @Override public void onDetach() { super.onDetach(); } @Override public void onClick(View v) { // TODO Auto-generated method stub } }