Advertisement
Guest User

Untitled

a guest
Jan 16th, 2012
373
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.84 KB | None | 0 0
  1. package com.deitel.favoritetwittersearches;
  2.  
  3. import java.util.Arrays;
  4.  
  5. import android.app.Activity;
  6. import android.app.AlertDialog;
  7. import android.content.Context;
  8. import android.content.DialogInterface;
  9. import android.content.Intent;
  10. import android.content.SharedPreferences;
  11. import android.net.Uri;
  12. import android.os.Bundle;
  13. import android.view.LayoutInflater;
  14. import android.view.View;
  15. import android.view.View.OnClickListener;
  16. import android.view.inputmethod.InputMethodManager;
  17. import android.widget.Button;
  18. import android.widget.EditText;
  19. import android.widget.TableLayout;
  20. import android.widget.TableRow;
  21.  
  22.  
  23. public class FavoriteTwitterSearches extends Activity {
  24. /** Called when the activity is first created. */
  25. private SharedPreferences savedSearches;
  26. private TableLayout queryTableLayout;
  27. private EditText queryEditText;
  28. private EditText tagEditText;
  29.  
  30. @Override
  31. public void onCreate(Bundle savedInstanceState) {
  32. super.onCreate(savedInstanceState);
  33. setContentView(R.layout.main);
  34. //get the SharedPreferences that contains the user's saved searches
  35. savedSearches = getSharedPreferences("searches", MODE_PRIVATE);
  36.  
  37. //get references to the queryTableLayout
  38. queryTableLayout = (TableLayout) findViewById(R.id.queryTableLayout);
  39.  
  40. //get references to the two EditTexts and Save Button
  41. queryEditText = (EditText) findViewById(R.id.queryEditText);
  42. tagEditText = (EditText) findViewById(R.id.tagEditText);
  43.  
  44. //register listeners for the Save and Clear Tags Buttons
  45. Button saveButton = (Button) findViewById(R.id.saveButton);
  46. OnClickListener saveButtonListener = null;
  47. saveButton.setOnClickListener(saveButtonListener);
  48. Button clearTagsButton = (Button) findViewById(R.id.clearTagsButton);
  49. OnClickListener clearTagsButtonListener = null;
  50. clearTagsButton.setOnClickListener(clearTagsButtonListener);
  51.  
  52. refreshButtons(null); //add previously save searches to GUI
  53.  
  54. }
  55.  
  56. private void refreshButtons(Object object) {
  57. // TODO Auto-generated method stub
  58.  
  59. }
  60. //end method onCreate
  61.  
  62. //recreate search tag and edit Buttons for all saved searches;
  63. //pass null to create all the tag and edit Buttons.
  64. private void refreshButtons(String newTag)
  65. {
  66. //store saved tags in the tags array
  67. String[] tags = savedSearches.getAll().keySet().toArray(new String[0]);
  68. Arrays.sort(tags, String.CASE_INSENSITIVE_ORDER); //sort by tag
  69.  
  70. //if a new tag was added, insert in GUI at the appropriate location
  71. if (newTag != null)
  72. {
  73. makeTagGUI(newTag, Arrays.binarySearch(tags, newTag));
  74. }
  75. else //display GUI for all tags
  76. {
  77. //display all saved searches
  78. for (int index = 0; index < tags.length; ++index)
  79. makeTagGUI(tags[index], index);
  80. } //end else
  81. }
  82. private void makeTagGUI(String newTag, int binarySearch) {
  83. // TODO Auto-generated method stub
  84.  
  85. } //end method refreshButtons
  86. //add new search to the save file, then refresh all Buttons
  87. private void makeTag(String query, String tag)
  88. {
  89. //originalQuery will be null if we're modifying an existing search
  90. String originalQuery = savedSearches.getString(tag, null);
  91.  
  92. //get a SharedPreferences.Editor to store new tag/query pair
  93. SharedPreferences.Editor preferencesEditor = savedSearches.edit();
  94. preferencesEditor.putString(tag, query); //store current search
  95. preferencesEditor.apply(); //store the updated preferences
  96. //if this is a new query, add its GUI
  97. if (originalQuery == null)
  98. refreshButtons(tag); //adds a new button for this tag
  99. } //end method makeTag
  100.  
  101. //add a new tag button and corresponding edit button to the GUI
  102. private void makeTagGUI1(String tag, int index)
  103. {
  104. //get a reference to the LayoutInflater service
  105. LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  106.  
  107. //inflate new_tag_view.xml to create new tag and edit Buttons
  108. View newTagView = inflater.inflate(R.layout.new_tag_view, null);
  109.  
  110. //get newTagButton, set its text and register its listener
  111. Button newTagButton = (Button) newTagView.findViewById(R.id.newTagButton);
  112. newTagButton.setText(tag);
  113. OnClickListener queryButtonListener = null;
  114. newTagButton.setOnClickListener(queryButtonListener);
  115.  
  116. //get newEditButton and register its listener
  117. Button newEditButton = (Button) newTagView.findViewById(R.id.newEditButton);
  118. OnClickListener editButtonListener = null;
  119. newEditButton.setOnClickListener(editButtonListener);
  120.  
  121. //add new tag and edit buttons to queryTableLayout
  122. queryTableLayout.addView(newTagView, index);
  123. } //end makeTagGUI1
  124.  
  125. //remove all saved search Buttons from the app
  126. private void clearButtons()
  127. {
  128. //remove all saved search Buttons
  129. queryTableLayout.removeAllViews();
  130. } //ends method clearButtons
  131.  
  132. //create a new Button and add it to the ScrollView
  133. public OnClickListener saveButtonListener = new OnClickListener()
  134. {
  135. @Override
  136. public void onClick(View v)
  137. {
  138. //create tag if both queryEditText and tagEditText are not empty
  139. if (queryEditText.getText().length() > 0 && tagEditText.getText().length() > 0)
  140. {
  141. makeTag(queryEditText.getText().toString(), tagEditText.getText().toString());
  142. queryEditText.setText(""); // clear queryEditText
  143. tagEditText.setText(""); // clear tagEditText
  144.  
  145. //hide the soft keyboard
  146. ((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(tagEditText.getWindowToken(), 0);
  147. } // end if
  148. else // display message asking user to provide a query and a tag
  149. {
  150. // create a new AlertDialog Builder
  151. AlertDialog.Builder builder = new AlertDialog.Builder(FavoriteTwitterSearches.this);
  152.  
  153. builder.setTitle(R.string.missingTitle); // title bar string
  154.  
  155. // provide an OK button that simply dismisses the dialog
  156. builder.setPositiveButton(R.string.OK, null);
  157.  
  158. // set the message to display
  159. builder.setMessage(R.string.missingMessage);
  160.  
  161. // create AlertDialog from the AlertDialog Builder
  162. AlertDialog errorDialog = builder.create();
  163. errorDialog.show(); // display the Dialog
  164. } // end else
  165. } // end method OnClick
  166. }; // end OnClickListener anonymous inner class
  167.  
  168. // clears all saved searches
  169. public OnClickListener clearTagsButtonListener = new OnClickListener()
  170. {
  171. @Override
  172. public void onClick(View v)
  173. {
  174. // create a new AlertDialog Builder
  175. AlertDialog.Builder builder = new AlertDialog.Builder(FavoriteTwitterSearches.this);
  176.  
  177. builder.setTitle(R.string.confirmTitle); // title bar string
  178.  
  179. // provide an OK button that simply dismisses the dialog
  180. builder.setPositiveButton(R.string.erase, new DialogInterface.OnClickListener()
  181. {
  182. @Override
  183. public void onClick(DialogInterface dialog, int button)
  184. {
  185. clearButtons(); // clears all saved searches from the map
  186.  
  187. // get a SharedPreferences.Editor to clear searches
  188. SharedPreferences.Editor preferencesEditor = savedSearches.edit();
  189.  
  190. preferencesEditor.clear(); // removes all tag/query pairs
  191. preferencesEditor.apply(); // commit the changes
  192. } // end method onClick
  193. } // end anonymous inner class
  194. ); // end call to method setPositiveButton
  195.  
  196. builder.setCancelable(true);
  197. builder.setNegativeButton(R.string.cancel, null);
  198.  
  199. //set the message to display
  200. builder.setMessage(R.string.confirmMessage);
  201.  
  202. //create AlertDialog from the AlertDialog.Builder
  203. AlertDialog confirmDialog = builder.create();
  204. confirmDialog.show(); // display the Dialog
  205. } // end method onClick
  206. }; // end On Click Listener anonymous inner class
  207.  
  208. // load selected search in a web browser
  209. public OnClickListener queryButtonListener = new OnClickListener()
  210. {
  211. @Override
  212. public void onClick(View v)
  213. {
  214. // get the query
  215. String buttonText = ((Button)v).getText().toString();
  216. String query = savedSearches.getString(buttonText, null);
  217.  
  218. // create the URL corresponding to the touched Button's query
  219. String urlString = getString(R.string.searchURL) + query;
  220.  
  221. //create an Intent to launch a web browser
  222. Intent getUrl = new Intent(Intent.ACTION_VIEW, Uri.parse(urlString));
  223.  
  224. Intent getURL = null;
  225. startActivity(getURL); // execute the Intent
  226. } // end method OnClick
  227. }; // end OnClickListener anonymous inner class
  228.  
  229. // edit selected search
  230. public OnClickListener editButtonListener = new OnClickListener()
  231. {
  232. @Override
  233. public void onClick(View v)
  234. {
  235. // get all necessary GUI components
  236. TableRow buttonTableRow = (TableRow) v.getParent();
  237. Button searchButton = (Button) buttonTableRow.findViewById(R.id.newTagButton);
  238.  
  239. String tag = searchButton.getText().toString();
  240.  
  241. //set EditTexts to match the chosen tag and query
  242. tagEditText.setText(tag);
  243. queryEditText.setText(savedSearches.getString(tag, null));
  244. } // end method onClick
  245. }; // end OnClickListener anonymous inner class
  246. } // end class FavoriteTwitteSearches
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement