package com.example.greenwatch.adapters; | |||||
import android.graphics.Color; | |||||
import android.view.LayoutInflater; | |||||
import android.view.View; | |||||
import android.view.ViewGroup; | |||||
import android.widget.TextView; | |||||
import androidx.annotation.NonNull; | |||||
import androidx.recyclerview.widget.RecyclerView; | |||||
import com.example.greenwatch.R; | |||||
import com.example.greenwatch.models.Device; | |||||
import java.util.ArrayList; | |||||
import java.util.Collections; | |||||
import java.util.Comparator; | |||||
import java.util.List; | |||||
public class DeviceListAdapter extends RecyclerView.Adapter<DeviceListAdapter.DeviceListHolder> { | |||||
private List<Device> devices = new ArrayList<>(); | |||||
@NonNull | |||||
@Override | |||||
public DeviceListHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { | |||||
View itemView = LayoutInflater.from(parent.getContext()) | |||||
.inflate(R.layout.device_item, parent, false); | |||||
return new DeviceListHolder(itemView); | |||||
} | |||||
@Override | |||||
public void onBindViewHolder(@NonNull DeviceListHolder holder, int position) { | |||||
Device currentDevice = devices.get(position); | |||||
holder.textViewSensorType.setText(holder.itemView.getContext().getString(R.string.sensor_type_placeholder, currentDevice.getSensorType())); | |||||
holder.textViewDeviceID.setText(holder.itemView.getContext().getString(R.string.deviceID_placeholder, currentDevice.getDeviceID())); | |||||
holder.textViewTimeStamp.setText(holder.itemView.getContext().getString(R.string.sensor_time_stamp_placeholder, currentDevice.getTimeStamp())); | |||||
holder.textViewSensorMessage.setText(holder.itemView.getContext().getString(R.string.sensor_message_placeholder, currentDevice.getSensorMassage())); | |||||
holder.setDeviceSensorStatus(currentDevice); | |||||
} | |||||
@Override | |||||
public int getItemCount() { | |||||
return devices.size(); | |||||
} | |||||
public void setDevices(List<Device> devices) { | |||||
this.devices = devices; | |||||
Collections.sort(this.devices, new Comparator<Device>() { | |||||
@Override | |||||
public int compare(Device device1, Device device2) { | |||||
if (device1.getSensorStatus() && !device2.getSensorStatus()) { | |||||
return -1; | |||||
} else if (!device1.getSensorStatus() && device2.getSensorStatus()) { | |||||
return 1; | |||||
} else { | |||||
return 0; | |||||
} | |||||
} | |||||
}); | |||||
notifyDataSetChanged(); | |||||
} | |||||
class DeviceListHolder extends RecyclerView.ViewHolder { | |||||
private TextView textViewSensorType; | |||||
private TextView textViewSensorStatus; | |||||
private TextView textViewDeviceID; | |||||
private TextView textViewTimeStamp; | |||||
private TextView textViewSensorMessage; | |||||
public DeviceListHolder(View itemView) { | |||||
super(itemView); | |||||
textViewSensorType = (TextView) itemView.findViewById(R.id.tvDeviceSensorType); | |||||
textViewSensorStatus = (TextView) itemView.findViewById(R.id.tvDeviceSensorStatus); | |||||
textViewDeviceID = (TextView) itemView.findViewById(R.id.tvDeviceID); | |||||
textViewTimeStamp = (TextView) itemView.findViewById(R.id.tvDeviceTimeStamp); | |||||
textViewSensorMessage = (TextView) itemView.findViewById(R.id.tvDeviceSensorMassage); | |||||
} | |||||
public void setDeviceSensorStatus(Device device) { | |||||
if (device.getSensorStatus()) { | |||||
textViewSensorStatus.setText(itemView.getContext().getString(R.string.sensor_status_placeholder, "AN")); | |||||
itemView.setBackgroundColor(Color.RED); | |||||
} | |||||
else { | |||||
textViewSensorStatus.setText(itemView.getContext().getString(R.string.sensor_status_placeholder, "AUS")); | |||||
itemView.setBackgroundColor(Color.WHITE); | |||||
} | |||||
} | |||||
} | |||||
} |
android:layout_width="match_parent" | android:layout_width="match_parent" | ||||
android:layout_height="match_parent" | android:layout_height="match_parent" | ||||
android:text="Hello World!" | android:text="Hello World!" | ||||
android:layout_weight="1"> | |||||
android:layout_weight="4"> | |||||
</TextView> | </TextView> | ||||
<androidx.recyclerview.widget.RecyclerView | |||||
android:id="@+id/deviceListRecyclerView" | |||||
android:layout_width="match_parent" | |||||
android:layout_height="match_parent" | |||||
android:layout_weight="2" | |||||
tools:listitem="@layout/device_item"> | |||||
</androidx.recyclerview.widget.RecyclerView> | |||||
<LinearLayout | <LinearLayout | ||||
android:layout_width="match_parent" | android:layout_width="match_parent" | ||||
android:layout_height="match_parent" | android:layout_height="match_parent" | ||||
android:orientation="vertical" | android:orientation="vertical" | ||||
android:layout_weight="1"> | |||||
android:layout_weight="3"> | |||||
<Button | <Button | ||||
android:id="@+id/audiodetectionButton" | android:id="@+id/audiodetectionButton" |
<?xml version="1.0" encoding="utf-8"?> | |||||
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" | |||||
android:layout_width="match_parent" | |||||
android:layout_height="wrap_content" | |||||
android:layout_marginEnd="8dp" | |||||
android:layout_marginStart="8dp" | |||||
android:layout_marginTop="8dp"> | |||||
<RelativeLayout | |||||
android:layout_width="wrap_content" | |||||
android:layout_height="wrap_content" | |||||
android:padding="8dp"> | |||||
<TextView | |||||
android:id="@+id/tvDeviceSensorStatus" | |||||
android:layout_width="wrap_content" | |||||
android:layout_height="wrap_content" | |||||
android:layout_alignParentEnd="true" | |||||
android:text="@string/sensor_status_placeholder" | |||||
android:textAppearance="@style/TextAppearance.AppCompat.Large"> | |||||
</TextView> | |||||
<TextView | |||||
android:id="@+id/tvDeviceSensorType" | |||||
android:layout_width="wrap_content" | |||||
android:layout_height="wrap_content" | |||||
android:text="@string/sensor_type_placeholder" | |||||
android:textAppearance="@style/TextAppearance.AppCompat.Large" | |||||
android:maxLines="1" | |||||
android:layout_toStartOf="@id/tvDeviceSensorStatus" | |||||
android:layout_alignParentStart="true" | |||||
android:ellipsize="end"> | |||||
</TextView> | |||||
<TextView | |||||
android:id="@+id/tvDeviceID" | |||||
android:layout_width="wrap_content" | |||||
android:layout_height="wrap_content" | |||||
android:layout_below="@id/tvDeviceSensorType" | |||||
android:text="@string/deviceID_placeholder"> | |||||
</TextView> | |||||
<TextView | |||||
android:id="@+id/tvDeviceTimeStamp" | |||||
android:layout_width="wrap_content" | |||||
android:layout_height="wrap_content" | |||||
android:layout_below="@id/tvDeviceID" | |||||
android:text="@string/sensor_time_stamp_placeholder"> | |||||
</TextView> | |||||
<TextView | |||||
android:id="@+id/tvDeviceSensorMassage" | |||||
android:layout_width="wrap_content" | |||||
android:layout_height="wrap_content" | |||||
android:layout_below="@id/tvDeviceTimeStamp" | |||||
android:text="@string/sensor_message_placeholder"> | |||||
</TextView> | |||||
</RelativeLayout> | |||||
</androidx.cardview.widget.CardView> |
<resources> | <resources> | ||||
<string name="app_name">GreenWatch</string> | <string name="app_name">GreenWatch</string> | ||||
<string name="sensor_type_placeholder">Sensortyp: %s</string> | |||||
<string name="sensor_status_placeholder">Sensorstatus: %s</string> | |||||
<string name="deviceID_placeholder">Sensor-ID: %s</string> | |||||
<string name="sensor_time_stamp_placeholder">Timestamp: %s</string> | |||||
<string name="sensor_message_placeholder">Sensormessage: %d</string> | |||||
</resources> | </resources> |