dimanche 17 novembre 2019

How can I pass the value of checked boxes to execute in (Button) btn_buy_now.setOnclickListener in android studio?

How can I pass the value of checked boxes to execute in (Button) btn_buy_now.setOnclickListener?

Upon clicking the button, the .php file will execute and search for the cheapiest amount of the ingredient in the products table. I've read about KMP algorithm but I have no idea how can I use it. or is there other way I can do this without using KMP?

I know this is too much. But my deadline is approaching and I can't think of other place or person to ask for help. Codes below are not mine. Most of it are just copied from tutorials. Please help me. Thanks in advance Seniores.

    package com.example.a05;

    import android.content.Context;
    import android.content.Intent;
    import android.os.AsyncTask;
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    import android.widget.Button;
    import android.widget.CheckBox;
    import android.widget.Filter;
    import android.widget.Filterable;
    import android.widget.ListView;
    import android.widget.ProgressBar;
    import android.widget.TextView;
    import android.widget.Toast;

    import com.androidnetworking.AndroidNetworking;
    import com.androidnetworking.common.Priority;
    import com.androidnetworking.error.ANError;
    import com.androidnetworking.interfaces.JSONArrayRequestListener;

    import org.json.JSONArray;
    import org.json.JSONException;
    import org.json.JSONObject;

    import java.io.IOException;
    import java.io.InputStream;
    import java.net.HttpURLConnection;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.util.ArrayList;

    public class Cart extends AppCompatActivity {
        public class Order {
            /*INSTANCE FIELDS*/
            private String id;
            private String user_id;
            private String i_id;
            private String i_name;

        /*GETTERS AND SETTERS*/

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getUser_id() {
            return user_id;
        }

        public void setUser_id(String user_id) {
            this.user_id = user_id;
        }

        public String getI_id() {
            return i_id;
        }

        public void setI_id(String i_id) {
            this.i_id = i_id;
        }

        public String getI_name() {
            return i_name;
        }

        public void setI_name(String i_name) {
            this.i_name = i_name;
        }


        /* TO STRING         */
        @Override
        public String toString() {
            return i_name;
        }
    }


    class FilterHelper extends Filter {
        ArrayList<Order> currentList;
        ListViewAdapter adapter;
        Context c;

        public FilterHelper(ArrayList<Order> currentList, ListViewAdapter adapter, Context c) {
            this.currentList = currentList;
            this.adapter = adapter;
            this.c = c;
        }

        /*
        - Perform actual filtering.
         */
        @Override
        protected Filter.FilterResults performFiltering(CharSequence constraint) {
            Filter.FilterResults filterResults = new Filter.FilterResults();

            if (constraint != null && constraint.length() > 0) {
                //CHANGE TO UPPER
                constraint = constraint.toString().toUpperCase();

                //HOLD FILTERS WE FIND
                ArrayList<Order> foundFilters = new ArrayList<>();

                Order spacecraft = null;

                //ITERATE CURRENT LIST
                for (int i = 0; i < currentList.size(); i++) {
                    spacecraft = currentList.get(i);

                    //SEARCH
                    if (spacecraft.getI_name().toUpperCase().contains(constraint)) {
                        //ADD IF FOUND
                        foundFilters.add(spacecraft);
                    }
                }

                //SET RESULTS TO FILTER LIST
                filterResults.count = foundFilters.size();
                filterResults.values = foundFilters;
            } else {
                //NO ITEM FOUND.LIST REMAINS INTACT
                filterResults.count = currentList.size();
                filterResults.values = currentList;
            }

            //RETURN RESULTS
            return filterResults;
        }

        @Override
        protected void publishResults(CharSequence charSequence, Filter.FilterResults filterResults) {
            adapter.setOrders((ArrayList<Order>) filterResults.values);
            adapter.refresh();
        }
    }

    /*
    Our custom adapter class
     */
    public class ListViewAdapter extends BaseAdapter implements Filterable {

        Context c;
        ArrayList<Order> spacecrafts;
        public ArrayList<Order> currentList;
        FilterHelper filterHelper;

        public ListViewAdapter(Context c, ArrayList<Order> spacecrafts) {
            this.c = c;
            this.spacecrafts = spacecrafts;
            this.currentList = spacecrafts;
        }

        @Override
        public int getCount() {
            return spacecrafts.size();
        }

        @Override
        public Object getItem(int i) {
            return spacecrafts.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        //-----------------------------------------------------------------------------------------------------------------------
        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            if (view == null) {
                view = LayoutInflater.from(c).inflate(R.layout.cart_model, viewGroup, false);
            }

            TextView i_name = view.findViewById(R.id.i_name);
            final Button btn_remove = view.findViewById(R.id.btn_remove);
            Button btn_buy_now = view.findViewById(R.id.btn_buy_now);
            Button btn_x = view.findViewById(R.id.btn_x);
            final CheckBox cb_check = view.findViewById(R.id.cb_check);

            final Order s = (Order) this.getItem(i);

            i_name.setText(s.getI_name());

            btn_x.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    final String id = (s.getId());
                    final String i_name = (s.getI_name());
                    String link = "http://abcabc.com/remove_on_cart.php?id=" + id;

                    Toast.makeText(Cart.this, i_name + " removed.", Toast.LENGTH_LONG).show();
                    new updateData().execute(link);
                    Intent intent = new Intent(Cart.this, Cart.class);
                    startActivity(intent);
                }
            });


//
//                    cb_check.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
//                        @Override
//                        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//
//                            if(((CompoundButton) buttonView).isChecked()){
//
//                    Toast.makeText(Cart.this, " x to cart", Toast.LENGTH_LONG).show();
//                            }
//                        }
//                    });

            return view;
        }

//-----------------------------------------------------------------------------------------------------------------------

        public void setOrders(ArrayList<Order> filteredIngredients) {
            this.spacecrafts = filteredIngredients;

        }

        @Override
        public Filter getFilter() {
            if (filterHelper == null) {
                filterHelper = new FilterHelper(currentList, this, c);
            }

            return filterHelper;
        }

        public void refresh() {
            notifyDataSetChanged();
        }
    }

    /*
    Our HTTP Client
     */
    public class JSONDownloader {

        Intent intent = getIntent();
        final int recipe_id = intent.getIntExtra("id", 0);

        //SAVE/RETRIEVE URLS
        private final String JSON_DATA_URL = "http://abcabc.com/select_cart.php";
        //INSTANCE FIELDS
        private final Context c;

        public JSONDownloader(Context c) {
            this.c = c;
        }

        /*
        Fetch JSON Data
         */
        public ArrayList<Order> retrieve(final ListView mListView, final ProgressBar myProgressBar) {
            final ArrayList<Order> downloadedData = new ArrayList<>();
            myProgressBar.setIndeterminate(true);
            myProgressBar.setVisibility(View.VISIBLE);

            AndroidNetworking.get(JSON_DATA_URL)
                    .setPriority(Priority.HIGH)
                    .build()
                    .getAsJSONArray(new JSONArrayRequestListener() {
                        @Override
                        public void onResponse(JSONArray response) {
                            JSONObject jo;
                            Order s;
                            try {
                                for (int i = 0; i < response.length(); i++) {
                                    jo = response.getJSONObject(i);

                                    String id = jo.getString("id");
                                    String user_id = jo.getString("user_id");
                                    String i_id = jo.getString("i_id");
                                    String i_name = jo.getString("i_name");

                                    s = new Order();
                                    s.setId(id);
                                    s.setUser_id(user_id);
                                    s.setI_id(i_id);
                                    s.setI_name(i_name);
                                    downloadedData.add(s);
                                }
                                myProgressBar.setVisibility(View.GONE);

                            } catch (JSONException e) {
                                myProgressBar.setVisibility(View.GONE);
                                Toast.makeText(c, "GOOD RESPONSE BUT JAVA CAN'T PARSE JSON IT RECEIEVED. " + e.getMessage(), Toast.LENGTH_LONG).show();
                            }
                        }

                        //ERROR
                        @Override
                        public void onError(ANError anError) {
                            anError.printStackTrace();
                            myProgressBar.setVisibility(View.GONE);
                            Toast.makeText(c, "UNSUCCESSFUL :  ERROR IS : " + anError.getMessage(), Toast.LENGTH_LONG).show();
                        }
                    });
            return downloadedData;
        }
    }

    //----------------------------------------------------------------------------
    ArrayList<Order> spacecrafts = new ArrayList<>();
    ListView myListView;
    ListViewAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cart);

        myListView = findViewById(R.id.myListView);//
        Button btn_buy_now = findViewById(R.id.btn_buy_now);//

        final ProgressBar myProgressBar = findViewById(R.id.myProgressBar);//


        spacecrafts = new JSONDownloader(Cart.this).retrieve(myListView, myProgressBar);
        adapter = new ListViewAdapter(this, spacecrafts);
        myListView.setAdapter(adapter);


        btn_buy_now.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //???
                String link2 = "http://abcabc.com/buy_now.php?";
                new updateData2.execute(link2); //--error
            }
        });
    }

    public class updateData extends AsyncTask<String, String, String> {

        @Override
        protected String doInBackground(String... params) {
            HttpURLConnection conn = null;

            try {
                URL url;
                url = new URL(params[0]);
                conn = (HttpURLConnection) url.openConnection();
                if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
                    InputStream is = conn.getInputStream();
                } else {
                    InputStream err = conn.getErrorStream();
                }
                return "Done";
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (conn != null) {
                    conn.disconnect();
                }
            }
            return null;
        }

    }

    public class updateData2 extends AsyncTask<String, String, String> {

        @Override
        protected String doInBackground(String... params) {
            HttpURLConnection conn = null;

            try {
                URL url;
                url = new URL(params[0]);
                conn = (HttpURLConnection) url.openConnection();
                if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
                    InputStream is = conn.getInputStream();
                } else {
                    InputStream err = conn.getErrorStream();
                }
                return "Done";
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (conn != null) {
                    conn.disconnect();
                }
            }
            return null;
        }

    }


}

my tables enter image description here




Aucun commentaire:

Enregistrer un commentaire