- Sensorki – aplikacja „pogodowa” – #0
- Sensorki – widok i adapter – #1
- Sensorki – refresh – #2
- Sensorki – material design – #3
- Sensorki – wskaźnik baterii – #4
- Sensorki – widget – #5
- Sensorki – groovy/spring boot demo serwer – #6
- Sensorki – Android i Spock – #7
- Sensorki – podsumowanie – #8
- Sensorki – Serverless app i refaktoring – #9
Dane z serwera już pobrane, czas je wyświetlić. Na razie bez szaleństw – dopieszczać będziemy później 😉
| <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
| android:layout_width="wrap_content" | |
| android:layout_height="wrap_content" | |
| android:orientation="vertical"> | |
| <TextView | |
| android:id="@+id/label_tv" | |
| android:layout_width="wrap_content" | |
| android:layout_height="wrap_content" | |
| android:textAppearance="@style/Base.TextAppearance.AppCompat.Headline" /> | |
| <TextView | |
| android:id="@+id/temperature_tv" | |
| android:layout_width="wrap_content" | |
| android:layout_height="wrap_content" | |
| android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium" /> | |
| (…) | |
| </LinearLayout> |
Dla każdej miary, którą chcę wyświetlić tworzę sobie zwykły TextView. Wszystko zamknięte w LinearLayoucie. Na ten moment bardziej zależy mi żeby to działało niż żeby było ładnie, ale mimo wszystko od razu wykorzystam RecyclerView, kto wie ile sensorków trzeba będzie obserwować za kilka lat 😉 Recycler będzie miał dwie kolumny na ten moment.
| adapter = new SensorsAdapter(this); | |
| RecyclerView recyclerView = findViewById(R.id.recyclerview); | |
| recyclerView.setAdapter(adapter); | |
| recyclerView.setLayoutManager(new GridLayoutManager(this, 2)); | |
| recyclerView.setHasFixedSize(true); |
Tworzę też klasę SensorsAdapter.java, który będzie odpowiedzialny za powiązanie poszczególnych kafelków z layoutem
| @Override | |
| public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { | |
| Context context = parent.getContext(); | |
| LayoutInflater layoutInflater = LayoutInflater.from(context); | |
| View weatherView = layoutInflater.inflate(R.layout.recycler_view_item, parent, false); | |
| return new ViewHolder(weatherView); | |
| } |
i wstawienie odpowiednich danych do TextView.
| @Override | |
| public void onBindViewHolder(ViewHolder holder, int position) { | |
| final Sensor sensor = mSensor[position]; | |
| TextView label = holder.label; | |
| label.setText(sensor.label); | |
| TextView temperature = holder.temperature; | |
| temperature.setText(TextUtils.concat("Temp. ", String.valueOf(sensor.temperature), "\u00B0C")); | |
| (…) | |
| } |
No to jeszcze na koniec sam ViewHolder, który był tworzony i podpinany:
| class ViewHolder extends RecyclerView.ViewHolder { | |
| TextView label; | |
| TextView temperature; | |
| (…) | |
| ViewHolder(View itemView) { | |
| super(itemView); | |
| label = itemView.findViewById(R.id.label_tv); | |
| temperature = itemView.findViewById(R.id.temperature_tv); | |
| (…) | |
| } | |
| } |
Tak prezentuje się aplikacja w tym momencie:

Kod jak zwykle na githubie, pod kolejnym tagiem.