Advertisement
Guest User

Untitled

a guest
Mar 21st, 2017
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.54 KB | None | 0 0
  1. package com.example.synergy.pharmakart.Fragment;
  2.  
  3. import android.app.ProgressDialog;
  4. import android.app.SearchManager;
  5. import android.content.Context;
  6. import android.content.Intent;
  7. import android.database.Cursor;
  8. import android.database.MatrixCursor;
  9. import android.os.AsyncTask;
  10. import android.os.Bundle;
  11. import android.provider.BaseColumns;
  12. import android.support.v4.app.Fragment;
  13. import android.support.v4.widget.CursorAdapter;
  14. import android.support.v4.widget.SimpleCursorAdapter;
  15. import android.view.LayoutInflater;
  16. import android.view.View;
  17. import android.view.ViewGroup;
  18. import android.widget.SearchView;
  19. import android.widget.Toast;
  20.  
  21. import com.example.synergy.pharmakart.R;
  22.  
  23. import org.json.JSONArray;
  24. import org.json.JSONException;
  25. import org.json.JSONObject;
  26.  
  27. import java.io.BufferedReader;
  28. import java.io.IOException;
  29. import java.io.InputStream;
  30. import java.io.InputStreamReader;
  31. import java.net.HttpURLConnection;
  32. import java.net.MalformedURLException;
  33. import java.net.URL;
  34. import java.util.ArrayList;
  35.  
  36. public class Home extends Fragment {
  37.  
  38. // CONNECTION_TIMEOUT and READ_TIMEOUT are in milliseconds
  39. public static final int CONNECTION_TIMEOUT = 10000;
  40. public static final int READ_TIMEOUT = 15000;
  41. private SimpleCursorAdapter myAdapter;
  42.  
  43. public SearchView searchView;
  44. SearchView searchItem;
  45. private String[] strArrData = {"No Suggestions"};
  46. @Override
  47. public void onCreate(Bundle savedInstanceState) {
  48. super.onCreate(savedInstanceState);
  49. getActivity().setTitle("Pharmakart");
  50. final String[] from = new String[] {"fishName"};
  51. final int[] to = new int[] {android.R.id.text1};
  52. myAdapter = new SimpleCursorAdapter(getActivity(), android.R.layout.simple_spinner_dropdown_item, null, from, to, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
  53.  
  54. // Fetch data from mysql table using AsyncTask
  55. new AsyncFetch().execute();
  56. }
  57.  
  58. @Override
  59. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  60. Bundle savedInstanceState) {
  61. View view = inflater.inflate(R.layout.fragment_home, container, false);
  62. searchItem = (SearchView) view.findViewById(R.id.search);
  63. searchItem.setOnClickListener(new View.OnClickListener() {
  64. @Override
  65. public void onClick(View v) {
  66. searchItem.setIconified(false);
  67. }
  68. });
  69. SearchManager searchManager = (SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE);
  70. if (searchItem != null) {
  71. searchView = (SearchView) searchItem.getActionView();
  72. }
  73. if (searchView != null) {
  74. searchView.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName()));
  75. searchView.setIconified(false);
  76. searchView.setSuggestionsAdapter(myAdapter);
  77. // Getting selected (clicked) item suggestion
  78. searchView.setOnSuggestionListener(new SearchView.OnSuggestionListener() {
  79. @Override
  80. public boolean onSuggestionClick(int position) {
  81.  
  82. // Add clicked text to search box
  83. android.widget.CursorAdapter ca = searchView.getSuggestionsAdapter();
  84. Cursor cursor = ca.getCursor();
  85. cursor.moveToPosition(position);
  86. searchView.setQuery(cursor.getString(cursor.getColumnIndex("fishName")), false);
  87. return true;
  88. }
  89.  
  90. @Override
  91. public boolean onSuggestionSelect(int position) {
  92. return true;
  93. }
  94. });
  95. searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
  96. @Override
  97. public boolean onQueryTextSubmit(String s) {
  98. return false;
  99. }
  100.  
  101. @Override
  102. public boolean onQueryTextChange(String s) {
  103.  
  104. // Filter data
  105. final MatrixCursor mc = new MatrixCursor(new String[]{BaseColumns._ID, "fishName"});
  106. for (int i = 0; i < strArrData.length; i++) {
  107. if (strArrData[i].toLowerCase().startsWith(s.toLowerCase()))
  108. mc.addRow(new Object[]{i, strArrData[i]});
  109. }
  110. myAdapter.changeCursor(mc);
  111. return false;
  112. }
  113. });
  114. }
  115. // Inflate the layout for this fragment
  116. return view;
  117. }
  118.  
  119. protected void onNewIntent(Intent intent) {
  120.  
  121. if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
  122. String query = intent.getStringExtra(SearchManager.QUERY);
  123. if (searchView != null) {
  124. searchView.clearFocus();
  125. }
  126.  
  127. // User entered text and pressed search button. Perform task ex: fetching data from database and display
  128.  
  129. }
  130. }
  131.  
  132. private class AsyncFetch extends AsyncTask<String, String, String> {
  133.  
  134. ProgressDialog pdLoading = new ProgressDialog(getActivity());
  135. HttpURLConnection conn;
  136. URL url = null;
  137.  
  138. @Override
  139. protected void onPreExecute() {
  140. super.onPreExecute();
  141.  
  142. //this method will be running on UI thread
  143. pdLoading.setMessage("tLoading...");
  144. pdLoading.setCancelable(false);
  145. pdLoading.show();
  146.  
  147. }
  148.  
  149. @Override
  150. protected String doInBackground(String... params) {
  151. try {
  152.  
  153. // Enter URL address where your php file resides or your JSON file address
  154. url = new URL("http://synergywebdesigners.com/synergywebdesigners.com/ashish/PHP/fetch-all-fish.php");
  155.  
  156. } catch (MalformedURLException e) {
  157. // TODO Auto-generated catch block
  158. e.printStackTrace();
  159. return e.toString();
  160. }
  161. try {
  162.  
  163. // Setup HttpURLConnection class to send and receive data from php and mysql
  164. conn = (HttpURLConnection) url.openConnection();
  165. conn.setReadTimeout(READ_TIMEOUT);
  166. conn.setConnectTimeout(CONNECTION_TIMEOUT);
  167. conn.setRequestMethod("GET");
  168.  
  169. // setDoOutput to true as we receive data
  170. conn.setDoOutput(true);
  171. conn.connect();
  172.  
  173. } catch (IOException e1) {
  174. // TODO Auto-generated catch block
  175. e1.printStackTrace();
  176. return e1.toString();
  177. }
  178.  
  179. try {
  180.  
  181. int response_code = conn.getResponseCode();
  182.  
  183. // Check if successful connection made
  184. if (response_code == HttpURLConnection.HTTP_OK) {
  185.  
  186. // Read data sent from server
  187. InputStream input = conn.getInputStream();
  188. BufferedReader reader = new BufferedReader(new InputStreamReader(input));
  189. StringBuilder result = new StringBuilder();
  190. String line;
  191.  
  192. while ((line = reader.readLine()) != null) {
  193. result.append(line);
  194. }
  195.  
  196. // Pass data to onPostExecute method
  197. return (result.toString());
  198.  
  199. } else {
  200. return("Connection error");
  201. }
  202.  
  203. } catch (IOException e) {
  204. e.printStackTrace();
  205. return e.toString();
  206. } finally {
  207. conn.disconnect();
  208. }
  209.  
  210.  
  211. }
  212.  
  213. @Override
  214. protected void onPostExecute(String result) {
  215.  
  216. //this method will be running on UI thread
  217. ArrayList<String> dataList = new ArrayList<String>();
  218. pdLoading.dismiss();
  219.  
  220.  
  221. if(result.equals("no rows")) {
  222.  
  223. // Do some action if no data from database
  224.  
  225. }else{
  226.  
  227. try {
  228.  
  229. JSONArray jArray = new JSONArray(result);
  230.  
  231. // Extract data from json and store into ArrayList
  232. for (int i = 0; i < jArray.length(); i++) {
  233. JSONObject json_data = jArray.getJSONObject(i);
  234. dataList.add(json_data.getString("fish_name"));
  235. }
  236.  
  237. strArrData = dataList.toArray(new String[dataList.size()]);
  238.  
  239. } catch (JSONException e) {
  240. // You to understand what actually error is and handle it appropriately
  241. Toast.makeText(getActivity(), e.toString(), Toast.LENGTH_LONG).show();
  242. Toast.makeText(getActivity(), result.toString(), Toast.LENGTH_LONG).show();
  243. }
  244.  
  245. }
  246.  
  247. }
  248.  
  249. }
  250.  
  251. }
  252.  
  253. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  254. xmlns:app="http://schemas.android.com/apk/res-auto"
  255. xmlns:tools="http://schemas.android.com/tools"
  256. android:layout_width="match_parent"
  257. android:layout_height="match_parent"
  258. tools:context="com.example.synergy.pharmakart.Fragment.Home">
  259.  
  260.  
  261. <ScrollView
  262. android:layout_width="match_parent"
  263. android:layout_height="match_parent"
  264. android:scrollbars="none">
  265.  
  266. <LinearLayout
  267. android:orientation="vertical"
  268. android:layout_width="match_parent"
  269. android:layout_height="match_parent">
  270.  
  271. <ImageView
  272. android:layout_width="match_parent"
  273. android:layout_height="100sp"
  274. app:srcCompat="@drawable/logo"
  275. android:layout_marginTop="20dp"
  276. android:id="@+id/image" />
  277.  
  278. <LinearLayout
  279. android:orientation="vertical"
  280. android:layout_width="match_parent"
  281. android:layout_height="match_parent"
  282. android:layout_marginTop="@dimen/activity_vertical_margin"
  283. android:layout_gravity="center">
  284.  
  285. <SearchView
  286. android:layout_width="match_parent"
  287. android:layout_height="match_parent"
  288. android:queryHint="@string/search_hint"
  289. android:layout_marginLeft="@dimen/activity_vertical_margin"
  290. android:layout_marginRight="@dimen/activity_vertical_margin"
  291. android:layout_centerVertical="true"
  292. android:id="@+id/search"
  293. android:label="@string/search_hint"
  294. android:showAsAction="always"
  295. android:background="@drawable/edittextstyle"
  296. android:actionViewClass="android.widget.SearchView"/>
  297.  
  298. <TextView
  299. android:text="@string/advertisment"
  300. android:layout_width="match_parent"
  301. android:layout_height="wrap_content"
  302. android:id="@+id/discount"
  303. android:textSize="10sp"
  304. android:gravity="center"/>
  305.  
  306. <TextView
  307. android:text="or"
  308. android:textStyle="bold"
  309. android:layout_width="match_parent"
  310. android:layout_height="wrap_content"
  311. android:layout_marginTop="@dimen/activity_vertical_margin"
  312. android:textAppearance="?android:attr/textAppearanceMedium"
  313. android:id="@+id/textView4"
  314. android:gravity="center"/>
  315.  
  316. <LinearLayout
  317. android:orientation="vertical"
  318. android:layout_width="match_parent"
  319. android:layout_height="match_parent">
  320.  
  321.  
  322. <Button
  323. android:text="Quick Order"
  324. android:layout_width="match_parent"
  325. android:layout_height="wrap_content"
  326. android:id="@+id/button"
  327. android:textColor="#ffffff"
  328. android:background="@color/colorPrimary" />
  329.  
  330. <TextView
  331. android:text="@string/prescription"
  332. android:layout_width="match_parent"
  333. android:layout_height="wrap_content"
  334. android:id="@+id/textView5"
  335. android:gravity="center"
  336. android:layout_marginTop="@dimen/margin_left"
  337. android:textSize="10sp"/>
  338.  
  339. <LinearLayout
  340. android:orientation="vertical"
  341. android:layout_width="match_parent"
  342. android:layout_height="match_parent"
  343. android:layout_marginTop="@dimen/activity_vertical_margin">
  344. <TextView
  345. android:text="Get upto"
  346. android:layout_width="match_parent"
  347. android:layout_height="wrap_content"
  348. android:id="@+id/textView6"
  349. android:gravity="center"
  350. android:textStyle="bold"
  351. android:layout_marginTop="@dimen/activity_vertical_margin"
  352. android:textAppearance="?android:attr/textAppearanceMedium"
  353. android:textColor="@color/colorPrimary"/>
  354. <TextView
  355. android:text="20% off"
  356. android:layout_width="match_parent"
  357. android:layout_height="wrap_content"
  358. android:id="@+id/textView7"
  359. android:gravity="center"
  360. android:textStyle="bold"
  361. android:textAppearance="?android:attr/textAppearanceMedium"
  362. android:layout_marginTop="@dimen/activity_vertical_margin"
  363. android:textColor="@color/colorOffer"/>
  364. <TextView
  365. android:text="On medicine*"
  366. android:layout_width="match_parent"
  367. android:layout_height="wrap_content"
  368. android:id="@+id/textView8"
  369. android:gravity="center"
  370. android:textStyle="bold"
  371. android:layout_marginTop="@dimen/activity_vertical_margin"
  372. android:textAppearance="?android:attr/textAppearanceMedium"
  373. android:textColor="@color/colorOffer"/>
  374.  
  375. <TextView
  376. android:text="(Delivery charges Rs. 25/-)"
  377. android:layout_width="match_parent"
  378. android:layout_height="wrap_content"
  379. android:id="@+id/textView9"
  380. android:layout_marginTop="@dimen/margin_left"
  381. android:gravity="center"/>
  382. <TextView
  383. android:text="Service available in Delhi."
  384. android:layout_width="match_parent"
  385. android:layout_height="wrap_content"
  386. android:id="@+id/text10"
  387. android:gravity="center"
  388. android:textStyle="bold"
  389. android:textSize="12dp"
  390. android:layout_marginTop="@dimen/activity_vertical_margin"
  391. android:textColor="@color/colorOffer"/>
  392. </LinearLayout>
  393. </LinearLayout>
  394. </LinearLayout>
  395. </LinearLayout>
  396. </ScrollView>
  397. </FrameLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement