DeviceListAdapter implement delete device.

This commit is contained in:
Denis-Cosmin Nutiu 2021-01-23 20:04:25 +02:00
parent 22230a4cbc
commit 1034087553
3 changed files with 60 additions and 14 deletions

View file

@ -1,10 +1,13 @@
package dev.nuculabs.nucuhub.ui.settings.device;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import dev.nuculabs.nucuhub.R;
import dev.nuculabs.nucuhub.domain.Device;
@ -12,18 +15,23 @@ import dev.nuculabs.nucuhub.domain.Device;
import java.util.ArrayList;
public class DeviceListAdapter extends BaseAdapter {
private final String TAG = DeviceListAdapter.class.getName();
private final ArrayList<Device> items = new ArrayList<>();
private final Context context;
private final LayoutInflater inflater;
private final DeleteDeviceAction deleteDeviceAction;
private final StarDeviceAction starDeviceAction;
private final DeviceManagementDialog deviceManagementDialog;
public DeviceListAdapter(Context context) {
this.context = context;
public DeviceListAdapter(Context context, DeviceManagementDialog dmd) {
this.inflater = LayoutInflater.from(context);
this.deleteDeviceAction = new DeleteDeviceAction();
this.starDeviceAction = new StarDeviceAction();
this.deviceManagementDialog = dmd;
}
public DeviceListAdapter(ArrayList<Device> items, Context context) {
this(context);
public DeviceListAdapter(ArrayList<Device> items, Context context, DeviceManagementDialog dmd) {
this(context, dmd);
this.items.addAll(items);
}
@ -48,16 +56,50 @@ public class DeviceListAdapter extends BaseAdapter {
if (view == null) {
view = inflater.inflate(R.layout.settings_device_list_item, null);
}
final TextView text = view.findViewById(R.id.settingsDeviceListDeviceListItemText);
text.setText(items.get(position).toString());
final TextView text = view.findViewById(R.id.settings_device_list_item_text);
final TextView devicePosition = view.findViewById(R.id.settings_device_list_item_position);
final ImageButton deleteButton = view.findViewById(R.id.settings_device_list_item_delete_button);
final ImageButton starButton = view.findViewById(R.id.settings_device_list_item_select_button);
// TODO: Add click listeners for Delete And Select Buttons.
// TODO: Mark selected item row?
text.setText(items.get(position).toString());
devicePosition.setText(Integer.toString(position));
deleteButton.setOnClickListener(deleteDeviceAction);
starButton.setOnClickListener(starDeviceAction);
return view;
}
@Override
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
deviceManagementDialog.updatePreferenceEntryValues();
}
public boolean add(Device device) {
Log.d(TAG, "Adding item " + device.toString());
return items.add(device);
}
private class DeleteDeviceAction implements View.OnClickListener {
@Override
public void onClick(View v) {
View parentContainer = (View) v.getParent().getParent();
TextView positionText = parentContainer.findViewById(R.id.settings_device_list_item_position);
items.remove(Integer.parseInt(positionText.getText().toString()));
notifyDataSetChanged();
}
}
private class StarDeviceAction implements View.OnClickListener {
private final String TAG = StarDeviceAction.class.getName();
@Override
public void onClick(View v) {
// TODO: Highlight selected item row?
LinearLayout parent = (LinearLayout) v.getParent().getParent();
CharSequence text = ((TextView) parent.getChildAt(0)).getText();
Log.i(TAG, "StarDeviceAction " + text.toString());
}
}
}

View file

@ -45,7 +45,7 @@ public class DeviceManagementDialog extends Dialog {
final ListView deviceListView = requireViewById(R.id.settings_device_dialog_list);
final Button addDeviceButton = requireViewById(R.id.settings_device_add_button);
deviceTextInputLayout = requireViewById(R.id.settings_device_input_device);
adapter = new DeviceListAdapter(getContext());
adapter = new DeviceListAdapter(getContext(), this);
deviceListView.setAdapter(adapter);
@ -87,7 +87,7 @@ public class DeviceManagementDialog extends Dialog {
dismiss();
}
private void updatePreferenceEntryValues() {
public void updatePreferenceEntryValues() {
int itemsLength = adapter.getCount();
CharSequence[] entries = new CharSequence[itemsLength];
HashSet<String> entriesSet = new HashSet<>();

View file

@ -7,19 +7,23 @@
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/settingsDeviceListDeviceListItemText"
android:layout_height="wrap_content" android:id="@+id/settings_device_list_item_text"
tools:text="Device Name" android:textSize="18sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/settings_device_list_item_position"
android:visibility="gone"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp" android:layout_weight="1">
<ImageButton
android:layout_width="100dp"
android:layout_height="wrap_content" android:id="@+id/settingsDeviceListDeviceItemSelectButton"
android:layout_height="wrap_content" android:id="@+id/settings_device_list_item_select_button"
android:src="@drawable/ic_baseline_star_border_24"
android:contentDescription="@string/select_icon"/>
<ImageButton
android:layout_width="100dp"
android:layout_height="wrap_content" android:id="@+id/settingsDeviceListDeviceItemDeleteButton"
android:layout_height="wrap_content" android:id="@+id/settings_device_list_item_delete_button"
android:src="@drawable/ic_baseline_delete_24" android:contentDescription="@string/delete_icon"/>
</LinearLayout>
</LinearLayout>