Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ------------ Pobieranie stringów z pliku xml ------------
- // Get a string resource from your app's Resources
- String hello = getResources().getString(R.string.hello_world);
- // Or supply a string resource to a method that requires a string
- TextView textView = new TextView(this);
- textView.setText(R.string.hello_world);
- ------------ Różne języki ------------
- MyProject/
- res/
- values/
- strings.xml
- values-es/
- strings.xml
- values-fr/
- strings.xml
- ------------ Różne rozdzielczości ------------
- MyProject/
- res/
- layout/ # default (portrait)
- main.xml
- layout-land/ # landscape
- main.xml
- layout-large/ # large (portrait)
- main.xml
- layout-large-land/ # large landscape
- main.xml
- ------------ Współczynniki przeliczania zdjęć dla różnych rozdzielczości ------------
- • xhdpi: 2.0
- • hdpi: 1.5
- • mdpi: 1.0 (baseline)
- • ldpi: 0.75
- 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.
- Then, place the files in the appropriate drawable resource directory:
- • MyProject/
- • res/
- • drawable-xhdpi/
- • awesomeimage.png
- • drawable-hdpi/
- • awesomeimage.png
- • drawable-mdpi/
- • awesomeimage.png
- • drawable-ldpi/
- • awesomeimage.png
- ------------ Adapter – czyli przesyłanie z danych (tablica stringów) na OnCreateView (adapter is used to populate a ListView) ------------
- Parametry – Context, ID of list item layout, ID of text view, list of data
- Context – pozwala na dostęp do system resources i services itd., posiada globalne iformacje na temat środowiska aplikacji
- - Wywołujemy getActivity aby uzyskać dostęp do activity
- ID of list item layout – podajemy layout na którym mają być wyświetlane informacje (R.layout.list_item_forecast)
- ID of text view – podajemy id text view (R.id.list_item_forecast_textview) dokładne ID jakiegoś text view
- List of data – lista danych (lista stringów zrobiona z tablicy stringów)
- ------------ Odnajdywanie Views – XML Layout -> View Hierarchy -> Java Code ------------
- Button b = (Button) this.findViewById(R.id.btn);
- LinearLayout container = (LinearLayout) this.findViewById(R.id.container);
- ------------ Przypisanie Adaptera do ListView ------------
- mForecastAdapter =
- new ArrayAdapter<String>(
- // The current context (this fragment's parent)
- getActivity(),
- // ID of list item layout
- R.layout.list_item_forecast,
- // ID of the textview to populate
- R.id.list_item_forecast_textview,
- // Forecast data
- weekForecast);
- // Get a reference to the ListView, and attach this adapter to
- ListView listView = (ListView) rootView.findViewById(
- R.id.listview_forecast);
- listView.setAdapter(mForecastAdapter);
- return rootView;
- For an Activity
- We inflate the layout and associate it with the Activity by calling the setContentView method in onCreate in our Activity:
- setContentView(R.layout.activity_main);
- For a Fragment
- In our Fragment classes we inflate the layout in the onCreateView method, which includes a LayoutInflater as a parameter:
- View rootView = inflater.inflate(R.layout.fragment_main, container, false);
- 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.
- ------------ Nawiązanie połączenia URL z danymi o stanie pogody JSON ------------
- // These two need to be declared outside the try/catch
- // so that they can be closed in the finally block.
- HttpURLConnection urlConnection = null;
- BufferedReader reader = null;
- // Will contain the raw JSON response as a string.
- String forecastJsonStr = null;
- try {
- // Construct the URL for the OpenWeatherMap query
- // Possible parameters are avaiable at OWM's forecast API page, at
- // http://openweathermap.org/API#forecast
- URL url = new URL("http://api.openweathermap.org/data/2.5/forecast/daily?q=94043&mode=json&units=metric&cnt=7");
- // Create the request to OpenWeatherMap, and open the connection
- urlConnection = (HttpURLConnection) url.openConnection();
- urlConnection.setRequestMethod("GET");
- urlConnection.connect();
- // Read the input stream into a String
- InputStream inputStream = urlConnection.getInputStream();
- StringBuffer buffer = new StringBuffer();
- if (inputStream == null) {
- // Nothing to do.
- return null;
- }
- reader = new BufferedReader(new InputStreamReader(inputStream));
- String line;
- while ((line = reader.readLine()) != null) {
- // Since it's JSON, adding a newline isn't necessary (it won't affect parsing)
- // But it does make debugging a *lot* easier if you print out the completed
- // buffer for debugging.
- buffer.append(line + "\n");
- }
- if (buffer.length() == 0) {
- // Stream was empty. No point in parsing.
- return null;
- }
- forecastJsonStr = buffer.toString();
- } catch (IOException e) {
- Log.e("PlaceholderFragment", "Error ", e);
- // If the code didn't successfully get the weather data, there's no point in attemping to parse it.
- return null;
- } finally{
- if (urlConnection != null) {
- urlConnection.disconnect();
- }
- if (reader != null) {
- try {
- reader.close();
- } catch (final IOException e) {
- Log.e("PlaceholderFragment", "Error closing stream", e);
- }
- }
- }
- return rootView;
- } } }
- Any reason why you are using StringBuffer instead of StringBuilder?
- 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.
- ------------ Są dwa rodzaje połączeń w Androidzie (dwie klasy) ------------
- HttpUrlConnection oraz HttpClient – polecany to HttpUrlConnection
- Logi informacyjne
- Error, Warn, Info, Debug, Verbose
- Log.e(String tag, String msg), Log.w(‘’,’’), Log.i(), Log.d(), Log.v()
- MyOpenWeatherMapApiKey = „b2825dac52ff0d6415eb76a1a2bad2a5;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement