Advertisement
Guest User

VoteOnClick

a guest
Oct 9th, 2012
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.29 KB | None | 0 0
  1. package com.sentaca.android.accordion.classes.listeners;
  2.  
  3. import java.io.IOException;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6.  
  7. import org.apache.http.NameValuePair;
  8. import org.apache.http.message.BasicNameValuePair;
  9. import org.w3c.dom.Document;
  10. import org.w3c.dom.Element;
  11. import org.w3c.dom.NodeList;
  12.  
  13. import android.app.Activity;
  14. import android.app.AlertDialog;
  15. import android.app.ProgressDialog;
  16. import android.content.DialogInterface;
  17. import android.os.AsyncTask;
  18. import android.util.Log;
  19. import android.view.View;
  20. import android.view.View.OnClickListener;
  21. import android.widget.Button;
  22.  
  23. import com.sentaca.android.accordion.R;
  24. import com.sentaca.android.accordion.classes.ExpandListChild;
  25. import com.sentaca.android.accordion.classes.ExpandListGroup;
  26. import com.sentaca.android.accordion.classes.ServerCommunication;
  27. import com.sentaca.android.accordion.classes.User;
  28. import com.sentaca.android.accordion.classes.XMLfunctions;
  29.  
  30. public class VoteOnClick implements OnClickListener {
  31.     private ArrayList<ExpandListGroup> groups;
  32.     private Activity parent;
  33.     private Button current;
  34.     private View view;
  35.     public static ProgressDialog progress;
  36.     private ExpandListChild child;
  37.     public static String meal_id;
  38.  
  39.     public VoteOnClick(ArrayList<ExpandListGroup> groups, Activity parent, Button current, View view, ExpandListChild child) {
  40.         this.groups = groups;
  41.         this.parent = parent;
  42.         this.current = current;
  43.         this.view = view;
  44.         this.child = child;
  45.     }
  46.  
  47.     public void onClick(View v) {
  48.         progress = new ProgressDialog(parent);
  49.         progress.setCancelable(false);
  50.         progress.setMessage("Sending vote to the server");
  51.         progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
  52.         progress.show();
  53.        
  54.         new VoteWorker(parent, groups, current, view, child).execute();
  55.     }
  56. }
  57.  
  58. final class VoteWorker extends AsyncTask<Void, Integer, String> {
  59.     private ArrayList<ExpandListGroup> groups;
  60.     private Activity parent;
  61.     private Button current;
  62.     private View view;
  63.     private ExpandListChild child;
  64.  
  65.     public VoteWorker(final Activity parent, ArrayList<ExpandListGroup> groups, Button current, View view, ExpandListChild child) {
  66.         this.parent = parent;
  67.         this.groups = groups;
  68.         this.current = current;
  69.         this.view = view;
  70.         this.child = child;
  71.     }
  72.  
  73.     @Override
  74.     protected String doInBackground(final Void... params) {
  75.         //Where I would connect to the server and return my response
  76.         List<NameValuePair> params1 = new ArrayList<NameValuePair>();
  77.         params1.add(new BasicNameValuePair("meal_id", Integer.toString(child.getID())));
  78.         params1.add(new BasicNameValuePair("uid", Integer.toString(User.ID)));
  79.        
  80.         Log.d("MyDebugStatements", "Mealid : " + child.getID());
  81.         Log.d("MyDebugStatements", "uid : " + User.ID);
  82.        
  83.         String response = null;
  84.         try {
  85.             response = ServerCommunication.postToServer("http://example.net/kitchen/scripts/vote", params1);
  86.         } catch (IOException e) {
  87.             return "IOException occured whilst sending your vote to the server.";
  88.         }
  89.         return response;
  90.     }
  91.    
  92.     @Override
  93.     protected void onPostExecute(String response) {
  94.         VoteOnClick.progress.dismiss();      
  95.        
  96.         if (response.equals("good")) {
  97.             for (int i = 0; i < groups.size(); i++) {
  98.                 ExpandListChild child = groups.get(i).getChildItem(0);
  99.                 Button but = child.getVote();
  100.                
  101.                 if (but == null) {
  102.                     Log.d("MyDebugStatements", "Button was null");
  103.                     but = (Button) view.findViewById(R.id.vote);
  104.                     child.setOrder(but);
  105.                 }
  106.                
  107.                 but.setText("Vote");
  108.             }
  109.             current.setText("Voted");
  110.             voteSent();
  111.         }
  112.         else if (response.contains("<?xml")) {         
  113.             ArrayList<String> allergies = new ArrayList<String>();         
  114.            
  115.             //Convert the XML to string
  116.             Document doc = XMLfunctions.XMLfromString(response);
  117.            
  118.             NodeList nodes = doc.getElementsByTagName("allergy");
  119.             for (int i = 0; i < nodes.getLength(); i++) {
  120.                 Element e = (Element)nodes.item(i);
  121.                 Log.d("MyDebugStatements", "Adding allergy: " + XMLfunctions.getValue(e, "name") + " to the allergies list");
  122.                 allergies.add(XMLfunctions.getValue(e, "name"));
  123.             }
  124.            
  125.             displayAllergies(allergies);
  126.         }
  127.         else {
  128.             voteError(response);
  129.         }      
  130.     }
  131.    
  132.     private void voteError(String response) {
  133.         AlertDialog.Builder errorBuilder = new AlertDialog.Builder(this.parent);
  134.         errorBuilder.setTitle("Error!");
  135.         errorBuilder.setMessage(response);
  136.        
  137.         errorBuilder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
  138.             public void onClick(DialogInterface dialog, int which) {
  139.                 // do nothing      
  140.             }
  141.         });
  142.        
  143.         AlertDialog errorDialog = errorBuilder.create();
  144.         errorDialog.show();
  145.     }
  146.    
  147.     private void voteSent() {
  148.         AlertDialog.Builder sentBuilder = new AlertDialog.Builder(this.parent);
  149.         sentBuilder.setTitle("Thanks for voting!");
  150.         sentBuilder.setMessage("Your vote of '" + child.getName() + "' has been registered on our system");
  151.        
  152.         sentBuilder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
  153.             public void onClick(DialogInterface dialog, int which) {
  154.                 // do nothing      
  155.             }
  156.         });
  157.        
  158.         AlertDialog sentDialog = sentBuilder.create();
  159.         sentDialog.show();
  160.     }
  161.    
  162.     private void displayAllergies(ArrayList<String> allergies) {
  163.         AlertDialog.Builder allergyBuilder = new AlertDialog.Builder(this.parent);
  164.         allergyBuilder.setTitle("Allergies found!");
  165.        
  166.         //Extract the allergies
  167.         String message = "";
  168.        
  169.         for (int i = 0; i < allergies.size(); i++) {
  170.             message = message + allergies.get(i) + "\n";
  171.         }
  172.        
  173.         allergyBuilder.setMessage(message);
  174.        
  175.         Log.d("CHECKIFHERE", "Setting the positive button");
  176.         allergyBuilder.setPositiveButton("Order", new OverrideVoteOnClick(parent, groups, current, view, child));
  177.         Log.d("CHECKIFHERE", "Positive button set");
  178.        
  179.         allergyBuilder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
  180.             public void onClick(DialogInterface dialog, int which) {
  181.                 // do nothing
  182.             }
  183.         });
  184.        
  185.         AlertDialog allergyDialog = allergyBuilder.create();
  186.         allergyDialog.show();
  187.     }
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement