ashu2121

Untitled

Feb 13th, 2016
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.62 KB | None | 0 0
  1. public class Home extends Fragment{
  2. ViewPager home_news;
  3. ArrayList<HashMap<String, String>> home_news_array = new ArrayList<HashMap<String, String>>();
  4.  
  5. public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  6. rootview = inflater.inflate(R.layout.content_navigation_drawer, container, false);
  7.  
  8. if (android.os.Build.VERSION.SDK_INT > 9) {
  9. StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
  10. StrictMode.setThreadPolicy(policy);
  11. }
  12. View homenews = rootview.findViewById(R.id.home_news);
  13. home_news = (ViewPager)homenews.findViewById(R.id.viewpager);
  14. Homenews_Jsons homenewsjson = new Homenews_Jsons();
  15. homenewsjson.execute();
  16.  
  17. News_Adapter newsviewpager = new News_Adapter(getActivity(), home_news_array);
  18. home_news.setAdapter(newsviewpager);
  19.  
  20. return rootview
  21. }
  22. private class Homenews_Jsons extends AsyncTask<ArrayList<HashMap<String, String>>, Void, ArrayList<HashMap<String, String>>> {
  23. private ProgressDialog dialog = new ProgressDialog(getActivity());
  24.  
  25. @Override
  26. protected ArrayList<HashMap<String, String>> doInBackground(ArrayList<HashMap<String, String>>... params) {
  27. try {
  28. JSONArray data = new JSONArray(lib_function.getJSONUrl( jsontags.HOME_NEWS_JOSN_URL));
  29.  
  30.  
  31. HashMap<String, String> home_news_map;
  32.  
  33. for (int i = 0; i < data.length(); i++) {
  34. JSONObject c = data.getJSONObject(i);
  35. home_news_map = new HashMap<String, String>();
  36. home_news_map.put(jsontags.home_news_name, c.getString("s"));
  37. home_news_map.put(jsontags.home_news_image, c.getString("price"));
  38. home_news_map.put(jsontags.home_news_description, c.getString("thumb"));
  39. home_news_map.put(jsontags.home_news_id, c.getString("product_id"));
  40. home_news_map.put(jsontags.home_news_date, c.getString("product_id"));
  41.  
  42. home_news_array.add(home_news_map);
  43. // liustitemsa = best_prpduct_array.size()-1;
  44. Log.d("home news ", "" + home_news_array);
  45.  
  46. // checking website is workign or not
  47. // if json return values is equuals to 0 then all further work will stop and show error
  48. if(best_prpduct_array.size() >=1)
  49. {
  50. check_website_working = 1;
  51. }
  52. else {}
  53. }
  54. best_scuuess=1;
  55. Log.d("home news suceesss " ,""+best_scuuess);
  56. Log.d("home news complete", "home news complete donwload");
  57.  
  58. } catch (JSONException e) {
  59. // TODO Auto-generated catch block
  60. e.printStackTrace();
  61. }
  62. return best_prpduct_array;
  63. }
  64.  
  65. @Override
  66. protected void onPreExecute() {
  67.  
  68. spinner.setVisibility(View.VISIBLE);
  69. Log.d("starting DialogBox", "starting DialogBox");
  70. super.onPreExecute();
  71. }
  72.  
  73. // for run mail thread UI
  74. protected void publishProgress() {
  75.  
  76.  
  77. }
  78.  
  79. @Override
  80. protected void onPostExecute(ArrayList<HashMap<String, String>> result) {
  81. super.onPostExecute(result);
  82.  
  83.  
  84.  
  85. }
  86. }
  87. }
  88.  
  89.  
  90. //=====================================================================================//
  91. NewsAdapter Class
  92.  
  93.  
  94. public class News_Adapter extends PagerAdapter{
  95. Context mContext;
  96. Global_JsonTags jsontags = new Global_JsonTags();
  97. LayoutInflater inflater;
  98. LayoutInflater mLayoutInflater;
  99. String[] abc = {"a","b","c"};
  100. ArrayList<HashMap<String, String>> home_news_arrays = new ArrayList<HashMap<String, String>>();
  101.  
  102. // contsructor
  103. public News_Adapter(Context context, ArrayList<HashMap<String, String>> home_news_array) {
  104. mContext = context;
  105. mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  106. home_news_arrays = new ArrayList<HashMap<String, String>>(home_news_array);
  107. //home_news_arrays=home_news_array;
  108. Log.d(" News Apdater items" ,"-----------------------------------------------------------------------------------------------------");
  109.  
  110. }
  111.  
  112. @Override
  113. public int getCount() {
  114. Log.d(" News Apdater items" ,""+abc.length);
  115. return abc.length;
  116. }
  117.  
  118. @Override
  119. public boolean isViewFromObject(View view, Object object) {
  120. return view == ((RelativeLayout) object);
  121. }
  122.  
  123. @Override
  124. public Object instantiateItem(ViewGroup container, int position) {
  125.  
  126. View itemView = null;
  127. try {
  128. inflater = (LayoutInflater) mContext
  129. .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  130. itemView = inflater.inflate(R.layout.news, container, false);
  131. Log.d("View created -------------------------------------------------------------------","View created");
  132. }catch (Exception e) {Log.d("Error------------------------------------------------------",""+e);}
  133. //getting views
  134. ImageView imageView = (ImageView) itemView.findViewById(R.id.imageView54);
  135. TextView news_title = (TextView) itemView.findViewById(R.id.news_title);
  136. TextView news_date = (TextView) itemView.findViewById(R.id.home_new_date);
  137. TextView news_descrition = (TextView) itemView.findViewById(R.id.home_news_description);
  138.  
  139. Log.d("values in Adapter----------",abc[position]);
  140. // maping values
  141.  
  142. /*
  143. Log.d(" News home_news_array ", home_news_arrays.get(position).get("product_name"));
  144. */
  145.  
  146. news_title.setText(abc[position]);
  147. news_date.setText(abc[position]);
  148. /* news_descrition.setText(home_news_arrays.get(position).get(jsontags.home_news_description));
  149. */
  150. /* Picasso.with(mContext)
  151. .load(home_news_arrays.get(position).get("product_image"))
  152. .placeholder(R.drawable.ic_stub) // optional
  153. .error(R.drawable.ic_error) // optionals
  154. .fit()
  155. .centerCrop()
  156. .memoryPolicy(MemoryPolicy.NO_CACHE)
  157. .into(imageView);*/
  158.  
  159. container.addView(itemView);
  160.  
  161. return itemView;
  162. }
  163.  
  164. @Override
  165. public void destroyItem(ViewGroup container, int position, Object object) {
  166. container.removeView((RelativeLayout) object);
  167. }
  168. }
Add Comment
Please, Sign In to add comment