Advertisement
Guest User

Untitled

a guest
Sep 18th, 2014
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.82 KB | None | 0 0
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:layout_marginBottom="1dp"
  6. android:descendantFocusability="blocksDescendants" >
  7.  
  8. <ImageView
  9. android:id="@+id/iv_thumb"
  10. android:layout_width="65dp"
  11. android:layout_height="65dp"
  12. android:layout_centerVertical="true"
  13. android:padding="10dp"
  14. android:contentDescription="@string/str_iv_thumb" />
  15.  
  16. <TextView
  17. android:id="@+id/tv_article_title"
  18. android:layout_width="250dp"
  19. android:layout_height="wrap_content"
  20. android:textSize="15sp"
  21. android:textStyle="bold"
  22. android:layout_marginTop="8dp"
  23. android:layout_centerHorizontal="true"
  24. android:layout_toRightOf="@id/iv_thumb" />
  25.  
  26. <TextView
  27. android:id="@+id/tv_article_date"
  28. android:layout_width="250dp"
  29. android:layout_height="wrap_content"
  30. android:layout_centerHorizontal="true"
  31. android:layout_toRightOf="@id/iv_thumb"
  32. android:layout_below="@id/tv_article_title" />
  33.  
  34. <TextView
  35. android:id="@+id/tv_article_synopsis"
  36. android:layout_width="250dp"
  37. android:layout_height="wrap_content"
  38. android:layout_centerHorizontal="true"
  39. android:layout_toRightOf="@id/iv_thumb"
  40. android:layout_below="@id/tv_article_date" />
  41.  
  42. <ImageButton
  43. style="@style/Rem_Fav"
  44. android:id="@+id/iv_rem_favs_single"
  45. android:src="@drawable/remove_favourite"
  46. android:layout_width="25dp"
  47. android:layout_height="25dp"
  48. android:clickable="true"
  49. android:layout_marginTop="8dp"
  50. android:layout_marginRight="5dp"
  51. android:layout_alignParentRight="true"
  52. android:layout_centerVertical="true"
  53. android:contentDescription="@string/rem_favs_single"
  54. android:layout_toRightOf="@+id/tv_article_synopsis" />
  55.  
  56. <?xml version="1.0" encoding="utf-8"?>
  57. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  58. xmlns:tools="http://schemas.android.com/tools"
  59. android:layout_width="match_parent"
  60. android:layout_height="match_parent"
  61. android:orientation="vertical" >
  62.  
  63. <RelativeLayout
  64. android:id="@+id/relativeLayout1"
  65. android:layout_width="fill_parent"
  66. android:layout_height="wrap_content"
  67. style="@style/background" >
  68.  
  69. <ImageView
  70. android:id="@+id/iv_logo"
  71. android:contentDescription="@string/content"
  72. android:layout_width="250dp"
  73. android:layout_height="36dp"
  74. android:layout_centerHorizontal="true"
  75. android:src="@drawable/logo" />
  76.  
  77. <ImageButton
  78. style="@style/Rem_Fav"
  79. android:id="@+id/iv_rem_favs"
  80. android:src="@drawable/remove_favourite"
  81. android:clickable="true"
  82. android:layout_width="25dp"
  83. android:layout_marginRight="1dp"
  84. android:layout_marginTop="10dp"
  85. android:layout_alignParentRight="true"
  86. android:contentDescription="@string/rem_favs"
  87. android:layout_toRightOf="@id/iv_logo" />
  88.  
  89. </RelativeLayout>
  90.  
  91. <RelativeLayout
  92. android:id="@+id/relativeLayout2"
  93. android:layout_width="wrap_content"
  94. android:layout_height="wrap_content"
  95. android:layout_weight="1"
  96. android:layout_below="@id/relativeLayout1"
  97. style="@style/background" >
  98.  
  99. <ListView
  100. android:id="@+id/lv_Entities"
  101. android:layout_width="wrap_content"
  102. android:layout_height="fill_parent"
  103. android:clickable="true"
  104. tools:context=".MainActivity" />
  105.  
  106. </RelativeLayout>
  107. </LinearLayout>
  108.  
  109. package com.example.***;
  110.  
  111. import java.io.InputStream;
  112. import java.util.HashMap;
  113. import java.util.List;
  114.  
  115. import com.example.***.sqlite.SqliteController;
  116. import com.example.***.R;
  117.  
  118. import android.annotation.SuppressLint;
  119. import android.content.Context;
  120. import android.database.sqlite.SQLiteDatabase;
  121. import android.graphics.Bitmap;
  122. import android.graphics.BitmapFactory;
  123. import android.os.AsyncTask;
  124. import android.os.Bundle;
  125. import android.support.v4.app.Fragment;
  126. import android.support.v4.app.FragmentManager;
  127. import android.support.v4.app.FragmentTransaction;
  128. import android.util.Log;
  129. import android.view.Gravity;
  130. import android.view.LayoutInflater;
  131. import android.view.View;
  132. import android.view.ViewGroup;
  133. import android.view.View.OnClickListener;
  134. import android.view.ViewGroup.LayoutParams;
  135. import android.widget.ArrayAdapter;
  136. import android.widget.Button;
  137. import android.widget.ImageButton;
  138. import android.widget.ImageView;
  139. import android.widget.ListView;
  140. import android.widget.PopupWindow;
  141. import android.widget.TextView;
  142.  
  143. public class FavouritesFragment extends Fragment {
  144.  
  145. // Listview attribute
  146. ListView mListView;
  147.  
  148. List<HashMap<String, String>> articles = null;
  149.  
  150. SqliteController controller;
  151. SQLiteDatabase db;
  152.  
  153. ImageButton allRemoveBtn;
  154.  
  155. CustomListAdapter adapter;
  156.  
  157. public FavouritesFragment() {
  158.  
  159. }
  160.  
  161. @Override
  162. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  163. Bundle savedInstanceState) {
  164.  
  165. controller = new SqliteController(getActivity());
  166. db = controller.getWritableDatabase();
  167.  
  168. View rootView = inflater.inflate(R.layout.fragment_favourites, container, false);
  169.  
  170. // Setting the Title of the List
  171. getActivity().getActionBar().setTitle("Favourites");
  172.  
  173. // Getting the articles from the DB
  174. articles = controller.getAllArticles();
  175.  
  176. // Getting a reference to ListView of activity_main
  177. mListView = (ListView) rootView.findViewById(R.id.lv_Entities);
  178.  
  179. adapter = new CustomListAdapter(getActivity(), R.layout.lv_fav_layout);
  180.  
  181. mListView.setAdapter(adapter);
  182.  
  183. // Log.d("LISTVIEW","mListView: " + mListView);
  184.  
  185. /**
  186. * This section is for the Remove All from Favourites function
  187. * with a pop-up window asking if the user is sure about his/her
  188. * choice. If the yes button is clicked, all of the saved favourites
  189. * is deleted and the image chaged to selected image.
  190. */
  191.  
  192. if (articles.isEmpty()) {
  193. // Do not show the Delete all image button
  194. allRemoveBtn = (ImageButton) rootView.findViewById(R.id.iv_rem_favs);
  195. allRemoveBtn.setVisibility(View.INVISIBLE);
  196. }
  197. else
  198. {
  199. allRemoveBtn = (ImageButton) rootView.findViewById(R.id.iv_rem_favs);
  200. allRemoveBtn.setOnClickListener(new OnClickListener() {
  201.  
  202. @SuppressLint("InflateParams")
  203. @Override
  204. public void onClick(View arg0) {
  205.  
  206. LayoutInflater layoutInflater = (LayoutInflater) getActivity().getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  207. View popupView = layoutInflater.inflate(R.layout.popup, null);
  208.  
  209. final PopupWindow popupWindow = new PopupWindow(popupView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
  210. popupWindow.showAtLocation(popupView, Gravity.CENTER, 0, 0);
  211.  
  212. Button btnNo = (Button) popupView.findViewById(R.id.btn_no);
  213. btnNo.setOnClickListener(new Button.OnClickListener(){
  214.  
  215. @Override
  216. public void onClick(View v) {
  217.  
  218. // Close the pop-up window
  219. popupWindow.dismiss();
  220. }
  221. });
  222.  
  223. Button btnYes = (Button) popupView.findViewById(R.id.btn_yes);
  224. btnYes.setOnClickListener(new Button.OnClickListener(){
  225.  
  226. @Override
  227. public void onClick(View v) {
  228.  
  229. // Delete All from Table
  230. controller.deleteAllArticle(db, "Favourites");
  231.  
  232. // Close the pop-up window
  233. popupWindow.dismiss();
  234.  
  235. // Reload current fragment
  236. articles = controller.getAllArticles();
  237. adapter.notifyDataSetChanged();
  238.  
  239. // Hide the Image Button
  240. allRemoveBtn.setVisibility(View.INVISIBLE);
  241. }
  242. });
  243. popupWindow.showAsDropDown(allRemoveBtn, 50, -30);
  244. }
  245. });
  246.  
  247. }
  248. return rootView;
  249. }
  250.  
  251. public class CustomListAdapter extends ArrayAdapter<HashMap<String, String>>
  252. {
  253. Context context;
  254.  
  255. public CustomListAdapter(Context context, int resourceId)
  256. {
  257. super(context, resourceId);
  258. this.context = context;
  259. }
  260.  
  261. @Override
  262. public int getCount() {
  263. return articles.size();
  264. }
  265.  
  266. @Override
  267. public long getItemId(int position) {
  268. return position;
  269. }
  270. @SuppressWarnings("unused")
  271. @Override
  272. public View getView(final int position, View convertView, ViewGroup parent) {
  273.  
  274. View row = convertView;
  275. ViewHolder view_holder;
  276. final HashMap<String, String> rowItem = articles.get(position);
  277.  
  278. if (row == null)
  279. {
  280. LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  281. row = inflater.inflate(R.layout.lv_fav_layout, null);
  282.  
  283. view_holder = new ViewHolder();
  284.  
  285.  
  286. view_holder.iv_thumb = (ImageView) row.findViewById(R.id.iv_thumb);
  287. view_holder.tv_article_title = (TextView) row.findViewById(R.id.tv_article_title);
  288. view_holder.tv_article_date = (TextView) row.findViewById(R.id.tv_article_date);
  289. view_holder.tv_article_synopsis = (TextView) row.findViewById(R.id.tv_article_synopsis);
  290. view_holder.iv_rem_favs_single =(ImageButton)row.findViewById(R.id.iv_rem_favs_single);
  291.  
  292. row.setTag(view_holder);
  293. }
  294. else
  295. {
  296. view_holder = (ViewHolder) row.getTag();
  297. view_holder.iv_thumb.setImageBitmap(null);
  298. }
  299.  
  300. row.setOnClickListener(new OnClickListener() {
  301.  
  302. @Override
  303. public void onClick(View arg0) {
  304.  
  305. selectItem(position);
  306. }
  307. });
  308.  
  309. // Getting the Article ID of selected item
  310. final String articleId = articles.get(position).get("ArticleId");
  311.  
  312. // Setting of the Image
  313. String imgUrl = articles.get(position).get("ArticleUrl");
  314. new DownloadImageTask(view_holder.iv_thumb).execute(imgUrl);
  315.  
  316. // Setting the Title
  317. view_holder.tv_article_title.setText(articles.get(position).get("ArticleTitle"));
  318.  
  319. // Setting the Date
  320. view_holder.tv_article_date.setText(articles.get(position).get("ArticleDate"));
  321.  
  322. // Setting the Synopsis of the article
  323. view_holder.tv_article_synopsis.setText(articles.get(position).get("ArticleSynopsis"));
  324.  
  325. // Set the onclick listener for the remove favourite item
  326. view_holder.iv_rem_favs_single.setTag(view_holder.iv_rem_favs_single);
  327. view_holder.iv_rem_favs_single.setOnClickListener(new OnClickListener() {
  328.  
  329. @Override
  330. public void onClick(final View arg0) {
  331. LayoutInflater layoutInflater = (LayoutInflater) getActivity().getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  332. View popupView = layoutInflater.inflate(R.layout.popup, null);
  333.  
  334. final PopupWindow popupWindow2 = new PopupWindow(popupView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
  335. popupWindow2.showAtLocation(popupView, Gravity.CENTER, 0, 0);
  336.  
  337. Button btnNo = (Button) popupView.findViewById(R.id.btn_no);
  338. btnNo.setOnClickListener(new Button.OnClickListener(){
  339.  
  340. @Override
  341. public void onClick(View v) {
  342.  
  343. // DO NOT DELETE ANYTHING
  344.  
  345. // Close the pop-up window
  346. popupWindow2.dismiss();
  347. }
  348. });
  349.  
  350. Button btnYes = (Button) popupView.findViewById(R.id.btn_yes);
  351. btnYes.setOnClickListener(new Button.OnClickListener(){
  352.  
  353. @Override
  354. public void onClick(View v) {
  355.  
  356. // Delete selected item from the Table
  357. controller.deleteArticle(articleId);
  358.  
  359. // Close the pop-up window
  360. popupWindow2.dismiss();
  361.  
  362. // Reload current fragment
  363. articles = controller.getAllArticles();
  364. adapter.notifyDataSetChanged();
  365.  
  366. // Set the Image of the Image Button to selected
  367. //((ImageButton) arg0.getTag()).setImageResource(R.drawable.remove_favourite_selected);
  368.  
  369. if (articles.isEmpty()) {
  370. // Do not show the Delete all image button
  371. allRemoveBtn.setVisibility(View.INVISIBLE);
  372. }
  373. }
  374. });
  375.  
  376. popupWindow2.showAsDropDown((ImageButton) arg0.getTag(), 50, -30);
  377. }
  378. });
  379.  
  380. return row;
  381. }
  382.  
  383. private class ViewHolder{
  384. ImageView iv_thumb;
  385. TextView tv_article_title;
  386. TextView tv_article_date;
  387. TextView tv_article_synopsis;
  388. ImageButton iv_rem_favs_single;
  389. }
  390. }
  391.  
  392. // If the listview item is selected, it loads the next fragment
  393. private void selectItem(int position) {
  394.  
  395. Log.d("POSITION","Position: " + position);
  396.  
  397. // update the main content by replacing fragmentss
  398. FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
  399. FragmentTransaction transaction = fragmentManager.beginTransaction();
  400.  
  401.  
  402. FullArticleFragment fragment = new FullArticleFragment();
  403. fragment.article = articles.get(position);
  404.  
  405. // Replace whatever is in the fragment_container view with this fragment,
  406. // and add the transaction to the back stack so the user can navigate back
  407. transaction.replace(R.id.fragment_container, fragment);
  408. transaction.addToBackStack(null);
  409.  
  410. // Commit the transaction
  411. transaction.commit();
  412. }
  413.  
  414. // Download the image from the url stored in the database
  415. private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
  416. ImageView bmImage;
  417.  
  418. public DownloadImageTask(ImageView bmImage) {
  419. this.bmImage = bmImage;
  420. }
  421.  
  422. @Override
  423. protected Bitmap doInBackground(String... urls) {
  424. String urldisplay = urls[0];
  425. Bitmap mIcon11 = null;
  426. InputStream in = null;
  427.  
  428. try {
  429. in = new java.net.URL(urldisplay).openStream();
  430.  
  431. // Log.d("IMAGEURL","DIT - Url: " + urldisplay);
  432.  
  433. mIcon11 = BitmapFactory.decodeStream(in);
  434. in.close();
  435. }
  436. catch (Exception e)
  437. {
  438. Log.e("Error", e.getMessage());
  439. e.printStackTrace();
  440. }
  441.  
  442. return mIcon11;
  443. }
  444.  
  445. @Override
  446. protected void onPostExecute(Bitmap result) {
  447. bmImage.setImageBitmap(result);
  448. }
  449. }
  450. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement