44import android .app .Activity ;
55import android .app .AlertDialog ;
66import android .content .Context ;
7- import android .content .DialogInterface ;
87import android .graphics .Typeface ;
98import android .graphics .drawable .Drawable ;
109import android .os .Build ;
1110import android .support .v4 .content .ContextCompat ;
1211import android .support .v7 .widget .LinearLayoutManager ;
1312import android .support .v7 .widget .RecyclerView ;
14- import android .text .method .KeyListener ;
1513import android .view .LayoutInflater ;
1614import android .view .View ;
1715import android .widget .EditText ;
2927 * Created by Ahmed Yusuf on 18/11/18.
3028 */
3129@ SuppressLint ("ValidFragment" )
32- public class MyEdittextCheckbox extends LinearLayout {
30+ public class MyMultipleCheckbox extends LinearLayout {
31+ public enum SelectedBy {
32+ ID ,
33+ VALUE
34+ }
3335 Context context ;
3436 Activity activity ;
3537 LinearLayout formLayout ;
@@ -46,6 +48,7 @@ public class MyEdittextCheckbox extends LinearLayout{
4648 RecyclerView listViewCheckbox ;
4749
4850 StringBuilder selectedValue , selectedId ;
51+ CheckboxAdapter adapter = null ;
4952
5053 public static class Builder implements GeneralBuilder <Builder >, Cloneable {
5154 //required
@@ -154,17 +157,17 @@ public Builder setBackgroundDrawable(Drawable backgroundDrawable){
154157 return this ;
155158 }
156159
157- public MyEdittextCheckbox create (){
158- return new MyEdittextCheckbox (this );
160+ public MyMultipleCheckbox create (){
161+ return new MyMultipleCheckbox (this );
159162 }
160163
161164 @ Override
162- public MyEdittextCheckbox .Builder clone () throws CloneNotSupportedException {
163- return (MyEdittextCheckbox .Builder )super .clone ();
165+ public MyMultipleCheckbox .Builder clone () throws CloneNotSupportedException {
166+ return (MyMultipleCheckbox .Builder )super .clone ();
164167 }
165168 }
166169
167- public MyEdittextCheckbox (Builder builder ) {
170+ public MyMultipleCheckbox (Builder builder ) {
168171 super (builder .context , null , builder .defStyleAttr );
169172 this .context = builder .context ;
170173 this .activity = builder .activity ;
@@ -202,7 +205,8 @@ public MyEdittextCheckbox(Builder builder) {
202205 this .formLayout = builder .formLayout ;
203206 this .formLayout .addView (this .view );
204207 }
205- editTextContent .setOnClickListener (v -> openFormData ());
208+ createAlertDialog ();
209+ editTextContent .setOnClickListener (v -> alertDialogMultipleCheckbox .show ());
206210 editTextContent .setKeyListener (null );
207211 }
208212
@@ -216,6 +220,28 @@ public void setValue(String text){
216220 editTextContent .setText (text );
217221 }
218222
223+ public void setSelected (ArrayList <String > dataList , SelectedBy selectedBy ){
224+ if (dataList !=null && adapter !=null && list !=null ) {
225+ int n = 0 ;
226+ selectedId = new StringBuilder ();
227+ selectedValue = new StringBuilder ();
228+ for (String data : dataList ) {
229+ for (CheckboxData checkboxData : list ) {
230+ if ((selectedBy == SelectedBy .ID && String .valueOf (checkboxData .getId ()).equals (data )) ||
231+ (selectedBy == SelectedBy .VALUE && String .valueOf (checkboxData .getValue ()).equals (data ))) {
232+ checkboxData .setChecked (true );
233+ selectedId .append ((n > 0 ) ? ", " : "" ).append (checkboxData .getId ());
234+ selectedValue .append ((n > 0 ) ? ", " : "" ).append (checkboxData .getValue ());
235+ n ++;
236+ break ;
237+ }
238+ }
239+ }
240+ editTextContent .setText (selectedValue .toString ());
241+ adapter .notifyDataSetChanged ();
242+ }
243+ }
244+
219245 public TextView getTxtTitle () {
220246 return txtTitle ;
221247 }
@@ -243,7 +269,7 @@ public void setNullable(boolean nullable) {
243269 }
244270
245271
246- void openFormData (){//set up for add data dialog
272+ private void createAlertDialog (){
247273 if (alertDialogMultipleCheckbox == null ) {
248274 AlertDialog .Builder dialogBuilder = new AlertDialog .Builder (activity );
249275 LayoutInflater dialogInflater = activity .getLayoutInflater ();
@@ -252,11 +278,10 @@ void openFormData(){//set up for add data dialog
252278 TextView txtTitle = dialogView .findViewById (R .id .txtFormTitle );
253279 listViewCheckbox = dialogView .findViewById (R .id .listCheckbox );
254280
255- CheckboxAdapter adapter = new CheckboxAdapter (context , list );
281+ adapter = new CheckboxAdapter (context , list );
256282 listViewCheckbox .setAdapter (adapter );
257283 listViewCheckbox .setLayoutManager (new LinearLayoutManager (activity .getBaseContext ()));
258284
259-
260285 txtTitle .setText (title );
261286 dialogBuilder .setPositiveButton ("Save" , (dialog , which ) -> {
262287 listSelected .clear ();
@@ -279,8 +304,6 @@ void openFormData(){//set up for add data dialog
279304
280305 alertDialogMultipleCheckbox = dialogBuilder .create ();
281306 }
282- alertDialogMultipleCheckbox .show ();
283-
284307 }
285308
286309 public boolean isFilled () {
@@ -311,5 +334,9 @@ public String getSelectedId() {
311334 return selectedId .toString ();
312335 }
313336
337+ public ArrayList <CheckboxData > getSelectedCheckbox (){
338+ return listSelected ;
339+ }
340+
314341}
315342
0 commit comments