Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.sentaca.android.accordion.classes.listeners;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.List;
- import org.apache.http.NameValuePair;
- import org.apache.http.message.BasicNameValuePair;
- import org.w3c.dom.Document;
- import org.w3c.dom.Element;
- import org.w3c.dom.NodeList;
- import android.app.Activity;
- import android.app.AlertDialog;
- import android.app.ProgressDialog;
- import android.content.DialogInterface;
- import android.os.AsyncTask;
- import android.util.Log;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import com.sentaca.android.accordion.R;
- import com.sentaca.android.accordion.classes.ExpandListChild;
- import com.sentaca.android.accordion.classes.ExpandListGroup;
- import com.sentaca.android.accordion.classes.ServerCommunication;
- import com.sentaca.android.accordion.classes.User;
- import com.sentaca.android.accordion.classes.XMLfunctions;
- public class VoteOnClick implements OnClickListener {
- private ArrayList<ExpandListGroup> groups;
- private Activity parent;
- private Button current;
- private View view;
- public static ProgressDialog progress;
- private ExpandListChild child;
- public static String meal_id;
- public VoteOnClick(ArrayList<ExpandListGroup> groups, Activity parent, Button current, View view, ExpandListChild child) {
- this.groups = groups;
- this.parent = parent;
- this.current = current;
- this.view = view;
- this.child = child;
- }
- public void onClick(View v) {
- progress = new ProgressDialog(parent);
- progress.setCancelable(false);
- progress.setMessage("Sending vote to the server");
- progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
- progress.show();
- new VoteWorker(parent, groups, current, view, child).execute();
- }
- }
- final class VoteWorker extends AsyncTask<Void, Integer, String> {
- private ArrayList<ExpandListGroup> groups;
- private Activity parent;
- private Button current;
- private View view;
- private ExpandListChild child;
- public VoteWorker(final Activity parent, ArrayList<ExpandListGroup> groups, Button current, View view, ExpandListChild child) {
- this.parent = parent;
- this.groups = groups;
- this.current = current;
- this.view = view;
- this.child = child;
- }
- @Override
- protected String doInBackground(final Void... params) {
- //Where I would connect to the server and return my response
- List<NameValuePair> params1 = new ArrayList<NameValuePair>();
- params1.add(new BasicNameValuePair("meal_id", Integer.toString(child.getID())));
- params1.add(new BasicNameValuePair("uid", Integer.toString(User.ID)));
- Log.d("MyDebugStatements", "Mealid : " + child.getID());
- Log.d("MyDebugStatements", "uid : " + User.ID);
- String response = null;
- try {
- response = ServerCommunication.postToServer("http://example.net/kitchen/scripts/vote", params1);
- } catch (IOException e) {
- return "IOException occured whilst sending your vote to the server.";
- }
- return response;
- }
- @Override
- protected void onPostExecute(String response) {
- VoteOnClick.progress.dismiss();
- if (response.equals("good")) {
- for (int i = 0; i < groups.size(); i++) {
- ExpandListChild child = groups.get(i).getChildItem(0);
- Button but = child.getVote();
- if (but == null) {
- Log.d("MyDebugStatements", "Button was null");
- but = (Button) view.findViewById(R.id.vote);
- child.setOrder(but);
- }
- but.setText("Vote");
- }
- current.setText("Voted");
- voteSent();
- }
- else if (response.contains("<?xml")) {
- ArrayList<String> allergies = new ArrayList<String>();
- //Convert the XML to string
- Document doc = XMLfunctions.XMLfromString(response);
- NodeList nodes = doc.getElementsByTagName("allergy");
- for (int i = 0; i < nodes.getLength(); i++) {
- Element e = (Element)nodes.item(i);
- Log.d("MyDebugStatements", "Adding allergy: " + XMLfunctions.getValue(e, "name") + " to the allergies list");
- allergies.add(XMLfunctions.getValue(e, "name"));
- }
- displayAllergies(allergies);
- }
- else {
- voteError(response);
- }
- }
- private void voteError(String response) {
- AlertDialog.Builder errorBuilder = new AlertDialog.Builder(this.parent);
- errorBuilder.setTitle("Error!");
- errorBuilder.setMessage(response);
- errorBuilder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int which) {
- // do nothing
- }
- });
- AlertDialog errorDialog = errorBuilder.create();
- errorDialog.show();
- }
- private void voteSent() {
- AlertDialog.Builder sentBuilder = new AlertDialog.Builder(this.parent);
- sentBuilder.setTitle("Thanks for voting!");
- sentBuilder.setMessage("Your vote of '" + child.getName() + "' has been registered on our system");
- sentBuilder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int which) {
- // do nothing
- }
- });
- AlertDialog sentDialog = sentBuilder.create();
- sentDialog.show();
- }
- private void displayAllergies(ArrayList<String> allergies) {
- AlertDialog.Builder allergyBuilder = new AlertDialog.Builder(this.parent);
- allergyBuilder.setTitle("Allergies found!");
- //Extract the allergies
- String message = "";
- for (int i = 0; i < allergies.size(); i++) {
- message = message + allergies.get(i) + "\n";
- }
- allergyBuilder.setMessage(message);
- Log.d("CHECKIFHERE", "Setting the positive button");
- allergyBuilder.setPositiveButton("Order", new OverrideVoteOnClick(parent, groups, current, view, child));
- Log.d("CHECKIFHERE", "Positive button set");
- allergyBuilder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int which) {
- // do nothing
- }
- });
- AlertDialog allergyDialog = allergyBuilder.create();
- allergyDialog.show();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement