Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.synergy.pharmakart.Fragment;
- import android.app.ProgressDialog;
- import android.app.SearchManager;
- import android.content.Context;
- import android.content.Intent;
- import android.database.Cursor;
- import android.database.MatrixCursor;
- import android.os.AsyncTask;
- import android.os.Bundle;
- import android.provider.BaseColumns;
- import android.support.v4.app.Fragment;
- import android.support.v4.widget.CursorAdapter;
- import android.support.v4.widget.SimpleCursorAdapter;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.SearchView;
- import android.widget.Toast;
- import com.example.synergy.pharmakart.R;
- import org.json.JSONArray;
- import org.json.JSONException;
- import org.json.JSONObject;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.net.HttpURLConnection;
- import java.net.MalformedURLException;
- import java.net.URL;
- import java.util.ArrayList;
- public class Home extends Fragment {
- // CONNECTION_TIMEOUT and READ_TIMEOUT are in milliseconds
- public static final int CONNECTION_TIMEOUT = 10000;
- public static final int READ_TIMEOUT = 15000;
- private SimpleCursorAdapter myAdapter;
- public SearchView searchView;
- SearchView searchItem;
- private String[] strArrData = {"No Suggestions"};
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- getActivity().setTitle("Pharmakart");
- final String[] from = new String[] {"fishName"};
- final int[] to = new int[] {android.R.id.text1};
- myAdapter = new SimpleCursorAdapter(getActivity(), android.R.layout.simple_spinner_dropdown_item, null, from, to, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
- // Fetch data from mysql table using AsyncTask
- new AsyncFetch().execute();
- }
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- View view = inflater.inflate(R.layout.fragment_home, container, false);
- searchItem = (SearchView) view.findViewById(R.id.search);
- searchItem.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- searchItem.setIconified(false);
- }
- });
- SearchManager searchManager = (SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE);
- if (searchItem != null) {
- searchView = (SearchView) searchItem.getActionView();
- }
- if (searchView != null) {
- searchView.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName()));
- searchView.setIconified(false);
- searchView.setSuggestionsAdapter(myAdapter);
- // Getting selected (clicked) item suggestion
- searchView.setOnSuggestionListener(new SearchView.OnSuggestionListener() {
- @Override
- public boolean onSuggestionClick(int position) {
- // Add clicked text to search box
- android.widget.CursorAdapter ca = searchView.getSuggestionsAdapter();
- Cursor cursor = ca.getCursor();
- cursor.moveToPosition(position);
- searchView.setQuery(cursor.getString(cursor.getColumnIndex("fishName")), false);
- return true;
- }
- @Override
- public boolean onSuggestionSelect(int position) {
- return true;
- }
- });
- searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
- @Override
- public boolean onQueryTextSubmit(String s) {
- return false;
- }
- @Override
- public boolean onQueryTextChange(String s) {
- // Filter data
- final MatrixCursor mc = new MatrixCursor(new String[]{BaseColumns._ID, "fishName"});
- for (int i = 0; i < strArrData.length; i++) {
- if (strArrData[i].toLowerCase().startsWith(s.toLowerCase()))
- mc.addRow(new Object[]{i, strArrData[i]});
- }
- myAdapter.changeCursor(mc);
- return false;
- }
- });
- }
- // Inflate the layout for this fragment
- return view;
- }
- protected void onNewIntent(Intent intent) {
- if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
- String query = intent.getStringExtra(SearchManager.QUERY);
- if (searchView != null) {
- searchView.clearFocus();
- }
- // User entered text and pressed search button. Perform task ex: fetching data from database and display
- }
- }
- private class AsyncFetch extends AsyncTask<String, String, String> {
- ProgressDialog pdLoading = new ProgressDialog(getActivity());
- HttpURLConnection conn;
- URL url = null;
- @Override
- protected void onPreExecute() {
- super.onPreExecute();
- //this method will be running on UI thread
- pdLoading.setMessage("tLoading...");
- pdLoading.setCancelable(false);
- pdLoading.show();
- }
- @Override
- protected String doInBackground(String... params) {
- try {
- // Enter URL address where your php file resides or your JSON file address
- url = new URL("http://synergywebdesigners.com/synergywebdesigners.com/ashish/PHP/fetch-all-fish.php");
- } catch (MalformedURLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- return e.toString();
- }
- try {
- // Setup HttpURLConnection class to send and receive data from php and mysql
- conn = (HttpURLConnection) url.openConnection();
- conn.setReadTimeout(READ_TIMEOUT);
- conn.setConnectTimeout(CONNECTION_TIMEOUT);
- conn.setRequestMethod("GET");
- // setDoOutput to true as we receive data
- conn.setDoOutput(true);
- conn.connect();
- } catch (IOException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- return e1.toString();
- }
- try {
- int response_code = conn.getResponseCode();
- // Check if successful connection made
- if (response_code == HttpURLConnection.HTTP_OK) {
- // Read data sent from server
- InputStream input = conn.getInputStream();
- BufferedReader reader = new BufferedReader(new InputStreamReader(input));
- StringBuilder result = new StringBuilder();
- String line;
- while ((line = reader.readLine()) != null) {
- result.append(line);
- }
- // Pass data to onPostExecute method
- return (result.toString());
- } else {
- return("Connection error");
- }
- } catch (IOException e) {
- e.printStackTrace();
- return e.toString();
- } finally {
- conn.disconnect();
- }
- }
- @Override
- protected void onPostExecute(String result) {
- //this method will be running on UI thread
- ArrayList<String> dataList = new ArrayList<String>();
- pdLoading.dismiss();
- if(result.equals("no rows")) {
- // Do some action if no data from database
- }else{
- try {
- JSONArray jArray = new JSONArray(result);
- // Extract data from json and store into ArrayList
- for (int i = 0; i < jArray.length(); i++) {
- JSONObject json_data = jArray.getJSONObject(i);
- dataList.add(json_data.getString("fish_name"));
- }
- strArrData = dataList.toArray(new String[dataList.size()]);
- } catch (JSONException e) {
- // You to understand what actually error is and handle it appropriately
- Toast.makeText(getActivity(), e.toString(), Toast.LENGTH_LONG).show();
- Toast.makeText(getActivity(), result.toString(), Toast.LENGTH_LONG).show();
- }
- }
- }
- }
- }
- <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context="com.example.synergy.pharmakart.Fragment.Home">
- <ScrollView
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:scrollbars="none">
- <LinearLayout
- android:orientation="vertical"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- <ImageView
- android:layout_width="match_parent"
- android:layout_height="100sp"
- app:srcCompat="@drawable/logo"
- android:layout_marginTop="20dp"
- android:id="@+id/image" />
- <LinearLayout
- android:orientation="vertical"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_marginTop="@dimen/activity_vertical_margin"
- android:layout_gravity="center">
- <SearchView
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:queryHint="@string/search_hint"
- android:layout_marginLeft="@dimen/activity_vertical_margin"
- android:layout_marginRight="@dimen/activity_vertical_margin"
- android:layout_centerVertical="true"
- android:id="@+id/search"
- android:label="@string/search_hint"
- android:showAsAction="always"
- android:background="@drawable/edittextstyle"
- android:actionViewClass="android.widget.SearchView"/>
- <TextView
- android:text="@string/advertisment"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:id="@+id/discount"
- android:textSize="10sp"
- android:gravity="center"/>
- <TextView
- android:text="or"
- android:textStyle="bold"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_marginTop="@dimen/activity_vertical_margin"
- android:textAppearance="?android:attr/textAppearanceMedium"
- android:id="@+id/textView4"
- android:gravity="center"/>
- <LinearLayout
- android:orientation="vertical"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- <Button
- android:text="Quick Order"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:id="@+id/button"
- android:textColor="#ffffff"
- android:background="@color/colorPrimary" />
- <TextView
- android:text="@string/prescription"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:id="@+id/textView5"
- android:gravity="center"
- android:layout_marginTop="@dimen/margin_left"
- android:textSize="10sp"/>
- <LinearLayout
- android:orientation="vertical"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_marginTop="@dimen/activity_vertical_margin">
- <TextView
- android:text="Get upto"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:id="@+id/textView6"
- android:gravity="center"
- android:textStyle="bold"
- android:layout_marginTop="@dimen/activity_vertical_margin"
- android:textAppearance="?android:attr/textAppearanceMedium"
- android:textColor="@color/colorPrimary"/>
- <TextView
- android:text="20% off"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:id="@+id/textView7"
- android:gravity="center"
- android:textStyle="bold"
- android:textAppearance="?android:attr/textAppearanceMedium"
- android:layout_marginTop="@dimen/activity_vertical_margin"
- android:textColor="@color/colorOffer"/>
- <TextView
- android:text="On medicine*"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:id="@+id/textView8"
- android:gravity="center"
- android:textStyle="bold"
- android:layout_marginTop="@dimen/activity_vertical_margin"
- android:textAppearance="?android:attr/textAppearanceMedium"
- android:textColor="@color/colorOffer"/>
- <TextView
- android:text="(Delivery charges Rs. 25/-)"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:id="@+id/textView9"
- android:layout_marginTop="@dimen/margin_left"
- android:gravity="center"/>
- <TextView
- android:text="Service available in Delhi."
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:id="@+id/text10"
- android:gravity="center"
- android:textStyle="bold"
- android:textSize="12dp"
- android:layout_marginTop="@dimen/activity_vertical_margin"
- android:textColor="@color/colorOffer"/>
- </LinearLayout>
- </LinearLayout>
- </LinearLayout>
- </LinearLayout>
- </ScrollView>
- </FrameLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement