-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCheckListAdapter.java
More file actions
162 lines (138 loc) · 6.17 KB
/
Copy pathCheckListAdapter.java
File metadata and controls
162 lines (138 loc) · 6.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package com.example.trip_packer.adapter;
import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.example.trip_packer.Database.RoomDB;
import com.example.trip_packer.Models.Items;
import com.example.trip_packer.R;
import com.example.trip_packer.constants.myconstants;
import com.example.trip_packer.WeatherService;
import java.util.List;
public class CheckListAdapter extends RecyclerView.Adapter<ChecklistViewHolder> {
private Context context;
private List<Items> itemsList;
private RoomDB database;
private String show;
private OnItemAddedListener onItemAddedListener;
private ChecklistViewHolder holder;
private int position;
public CheckListAdapter() {
}
public CheckListAdapter(Context context, List<Items> itemsList, RoomDB database, String show) {
this.context = context;
this.itemsList = itemsList;
this.database = database;
this.show = show;
this.onItemAddedListener = onItemAddedListener;
if (itemsList.size() == 0)
Toast.makeText(context.getApplicationContext(), "NOTHING to show", Toast.LENGTH_SHORT).show();
}
@NonNull
@Override
public ChecklistViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new ChecklistViewHolder(LayoutInflater.from(context).inflate(R.layout.check_list_item, parent, false));
}
@Override
public void onBindViewHolder(@NonNull ChecklistViewHolder holder, @SuppressLint("RecyclerView") int position) {
this.holder = holder;
this.position = position;
holder.checkBox.setText(itemsList.get(position).getItemname());
holder.checkBox.setChecked(itemsList.get(position).getChecked());
if (myconstants.FALSE_STRING.equals(show)) {
holder.btnDelete.setVisibility(View.GONE);
holder.layout.setBackgroundDrawable(context.getResources().getDrawable((R.drawable.border_1)));
} else {
if (itemsList.get(position).getChecked()) {
holder.layout.setBackgroundColor(Color.parseColor("#8e546f"));
} else
holder.layout.setBackgroundDrawable(context.getResources().getDrawable((R.drawable.border_1)));
}
holder.checkBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (myconstants.WEATHER_CAMEL_CASE.equals(itemsList.get(position).getItemname())) {
openWeatherService();
} else {
Boolean check = holder.checkBox.isChecked();
database.mainDao().checkuncheck(itemsList.get(position).getID(), check);
if (myconstants.FALSE_STRING.equals(show)) {
itemsList = database.mainDao().getAllselected(true);
notifyDataSetChanged();
} else {
itemsList.get(position).setChecked(check);
notifyDataSetChanged();
Toast toastmessage = null;
if (toastmessage != null) {
toastmessage.cancel();
}
if (itemsList.get(position).getChecked()) {
toastmessage = Toast.makeText(context, "(" + holder.checkBox.getText() + ") packed", Toast.LENGTH_SHORT);
} else {
toastmessage = Toast.makeText(context, "(" + holder.checkBox.getText() + ") un-packed", Toast.LENGTH_SHORT);
}
toastmessage.show();
}
}
}
});
holder.btnDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new AlertDialog.Builder(context)
.setTitle("Delete (" + itemsList.get(position).getItemname() + ")")
.setMessage("ARE YOU SURE?")
.setPositiveButton("confirm", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
database.mainDao().delete(itemsList.get(position));
itemsList.remove(itemsList.get(position));
notifyDataSetChanged();
}
}).setNegativeButton("cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(context, "canceled", Toast.LENGTH_SHORT).show();
}
}).setIcon(R.drawable.ic_baseline_delete_forever_24).show();
}
});
}
@Override
public int getItemCount() {
return itemsList.size();
}
// Interface for item added event
public interface OnItemAddedListener {
void onItemAdded(Items item);
}
public OnItemAddedListener getOnItemAddedListener() {
return onItemAddedListener;
}
private void openWeatherService() {
Intent intent = new Intent(context, WeatherService.class);
context.startActivity(intent);
}
}
class ChecklistViewHolder extends RecyclerView.ViewHolder {
LinearLayout layout;
CheckBox checkBox;
Button btnDelete;
public ChecklistViewHolder(View itemview) {
super(itemview);
layout = itemview.findViewById(R.id.linearlayout);
checkBox = itemview.findViewById(R.id.checkbox);
btnDelete = itemview.findViewById(R.id.btnDelete);
}
}