Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.15 KB | None | 0 0
  1.  
  2. ------------ Pobieranie stringów z pliku xml ------------
  3. // Get a string resource from your app's Resources
  4. String hello = getResources().getString(R.string.hello_world);
  5.  
  6. // Or supply a string resource to a method that requires a string
  7. TextView textView = new TextView(this);
  8. textView.setText(R.string.hello_world);
  9.  
  10.  
  11.  
  12. ------------ Różne języki ------------
  13. MyProject/
  14. res/
  15. values/
  16. strings.xml
  17. values-es/
  18. strings.xml
  19. values-fr/
  20. strings.xml
  21.  
  22.  
  23.  
  24.  
  25. ------------ Różne rozdzielczości ------------
  26. MyProject/
  27. res/
  28. layout/ # default (portrait)
  29. main.xml
  30. layout-land/ # landscape
  31. main.xml
  32. layout-large/ # large (portrait)
  33. main.xml
  34. layout-large-land/ # large landscape
  35. main.xml
  36.  
  37.  
  38.  
  39. ------------ Współczynniki przeliczania zdjęć dla różnych rozdzielczości ------------
  40. • xhdpi: 2.0
  41. • hdpi: 1.5
  42. • mdpi: 1.0 (baseline)
  43. • ldpi: 0.75
  44.  
  45. This means that if you generate a 200x200 image for xhdpi devices, you should generate the same resource in 150x150 for hdpi, 100x100 for mdpi, and 75x75 for ldpi devices.
  46.  
  47. Then, place the files in the appropriate drawable resource directory:
  48. • MyProject/
  49. • res/
  50. • drawable-xhdpi/
  51. • awesomeimage.png
  52. • drawable-hdpi/
  53. • awesomeimage.png
  54. • drawable-mdpi/
  55. • awesomeimage.png
  56. • drawable-ldpi/
  57. • awesomeimage.png
  58.  
  59.  
  60. ------------ Adapter – czyli przesyłanie z danych (tablica stringów) na OnCreateView (adapter is used to populate a ListView) ------------
  61. Parametry – Context, ID of list item layout, ID of text view, list of data
  62. Context – pozwala na dostęp do system resources i services itd., posiada globalne iformacje na temat środowiska aplikacji
  63.  
  64. - Wywołujemy getActivity aby uzyskać dostęp do activity
  65. ID of list item layout – podajemy layout na którym mają być wyświetlane informacje (R.layout.list_item_forecast)
  66. ID of text view – podajemy id text view (R.id.list_item_forecast_textview) dokładne ID jakiegoś text view
  67. List of data – lista danych (lista stringów zrobiona z tablicy stringów)
  68.  
  69.  
  70. ------------ Odnajdywanie Views – XML Layout -> View Hierarchy -> Java Code ------------
  71. Button b = (Button) this.findViewById(R.id.btn);
  72. LinearLayout container = (LinearLayout) this.findViewById(R.id.container);
  73.  
  74.  
  75. ------------ Przypisanie Adaptera do ListView ------------
  76. mForecastAdapter =
  77. new ArrayAdapter<String>(
  78. // The current context (this fragment's parent)
  79. getActivity(),
  80. // ID of list item layout
  81. R.layout.list_item_forecast,
  82. // ID of the textview to populate
  83. R.id.list_item_forecast_textview,
  84. // Forecast data
  85. weekForecast);
  86.  
  87. // Get a reference to the ListView, and attach this adapter to
  88. ListView listView = (ListView) rootView.findViewById(
  89. R.id.listview_forecast);
  90. listView.setAdapter(mForecastAdapter);
  91.  
  92. return rootView;
  93.  
  94. For an Activity
  95. We inflate the layout and associate it with the Activity by calling the setContentView method in onCreate in our Activity:
  96.  
  97. setContentView(R.layout.activity_main);
  98.  
  99. For a Fragment
  100. In our Fragment classes we inflate the layout in the onCreateView method, which includes a LayoutInflater as a parameter:
  101.  
  102. View rootView = inflater.inflate(R.layout.fragment_main, container, false);
  103.  
  104. The root view, or view element which contains all the other views, is returned by the inflate method of the LayoutInflater. We then should return this rootView for the onCreateView.
  105.  
  106.  
  107. ------------ Nawiązanie połączenia URL z danymi o stanie pogody JSON ------------
  108. // These two need to be declared outside the try/catch
  109. // so that they can be closed in the finally block.
  110. HttpURLConnection urlConnection = null;
  111. BufferedReader reader = null;
  112.  
  113. // Will contain the raw JSON response as a string.
  114. String forecastJsonStr = null;
  115. try {
  116. // Construct the URL for the OpenWeatherMap query
  117. // Possible parameters are avaiable at OWM's forecast API page, at
  118. // http://openweathermap.org/API#forecast
  119. URL url = new URL("http://api.openweathermap.org/data/2.5/forecast/daily?q=94043&mode=json&units=metric&cnt=7");
  120.  
  121. // Create the request to OpenWeatherMap, and open the connection
  122. urlConnection = (HttpURLConnection) url.openConnection();
  123. urlConnection.setRequestMethod("GET");
  124. urlConnection.connect();
  125.  
  126. // Read the input stream into a String
  127. InputStream inputStream = urlConnection.getInputStream();
  128. StringBuffer buffer = new StringBuffer();
  129. if (inputStream == null) {
  130. // Nothing to do.
  131. return null;
  132. }
  133. reader = new BufferedReader(new InputStreamReader(inputStream));
  134.  
  135. String line;
  136. while ((line = reader.readLine()) != null) {
  137. // Since it's JSON, adding a newline isn't necessary (it won't affect parsing)
  138. // But it does make debugging a *lot* easier if you print out the completed
  139. // buffer for debugging.
  140. buffer.append(line + "\n");
  141. }
  142.  
  143. if (buffer.length() == 0) {
  144. // Stream was empty. No point in parsing.
  145. return null;
  146. }
  147. forecastJsonStr = buffer.toString();
  148. } catch (IOException e) {
  149. Log.e("PlaceholderFragment", "Error ", e);
  150. // If the code didn't successfully get the weather data, there's no point in attemping to parse it.
  151. return null;
  152. } finally{
  153. if (urlConnection != null) {
  154. urlConnection.disconnect();
  155. }
  156. if (reader != null) {
  157. try {
  158. reader.close();
  159. } catch (final IOException e) {
  160. Log.e("PlaceholderFragment", "Error closing stream", e);
  161. }
  162. }
  163. }
  164. return rootView;
  165. } } }
  166.  
  167. Any reason why you are using StringBuffer instead of StringBuilder?
  168. Also, nitpicking: the creation of the buffer could be better placed on line 28 instead of 22, because the buffer is needed only after checking that the input stream is not null.
  169.  
  170.  
  171. ------------ Są dwa rodzaje połączeń w Androidzie (dwie klasy) ------------
  172. HttpUrlConnection oraz HttpClient – polecany to HttpUrlConnection
  173.  
  174. Logi informacyjne
  175. Error, Warn, Info, Debug, Verbose
  176. Log.e(String tag, String msg), Log.w(‘’,’’), Log.i(), Log.d(), Log.v()
  177.  
  178. MyOpenWeatherMapApiKey = „b2825dac52ff0d6415eb76a1a2bad2a5;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement