Guest User

Untitled

a guest
Jul 9th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.67 KB | None | 0 0
  1. MainActivity.class
  2. -------------------------------------
  3. package com.labs.ankitt.multichoice;
  4.  
  5. import android.os.Bundle;
  6. import android.support.annotation.Nullable;
  7. import android.support.v7.app.AlertDialog;
  8. import android.support.v7.app.AppCompatActivity;
  9. import android.support.v7.widget.LinearLayoutManager;
  10. import android.support.v7.widget.RecyclerView;
  11. import android.util.Log;
  12. import android.view.LayoutInflater;
  13. import android.view.View;
  14. import android.widget.Button;
  15.  
  16. import java.util.ArrayList;
  17. import java.util.Collections;
  18. import java.util.List;
  19.  
  20. public class MainActivity extends AppCompatActivity implements View.OnClickListener {
  21.  
  22.     private Button buttonClick;
  23.     private List<String> foodAllergy;
  24.     private CustomAdapter adapter;
  25.     private RecyclerView.LayoutManager layoutManager;
  26.  
  27.     @Override
  28.     protected void onCreate(@Nullable Bundle savedInstanceState) {
  29.         super.onCreate(savedInstanceState);
  30.         setContentView(R.layout.activity_main);
  31.         buttonClick = (Button) findViewById(R.id.buttonFoodAllergy);
  32.         buttonClick.setOnClickListener(this);
  33.  
  34.         //arraylist
  35.         foodAllergy = new ArrayList<>();
  36.         String[] food = getResources().getStringArray(R.array.food_allergy);
  37.         Collections.addAll(foodAllergy, food);
  38.         Log.i("TAG", "onCreate: " + food.length);
  39.     }
  40.  
  41.     @Override
  42.     public void onClick(View v) {
  43.         if (v == buttonClick) {
  44.             AlertDialog.Builder builder = new AlertDialog.Builder(this);
  45.  
  46.             LayoutInflater inflater = getLayoutInflater();
  47.             View dialogView = (View) inflater.inflate(R.layout.custom_alert_dialog, null);
  48.             builder.setView(dialogView);
  49.  
  50.  
  51.             AlertDialog dialog = builder.create();
  52.  
  53.             dialog.show();
  54.             RecyclerView rv = (RecyclerView) dialogView.findViewById(R.id.recycler_data);
  55.             rv.setLayoutManager(new LinearLayoutManager(this));
  56.             adapter= new CustomAdapter(this, foodAllergy);
  57.             rv.setAdapter(adapter);
  58.         }
  59.     }
  60. }
  61.  
  62.  
  63.  
  64. Adapter
  65. ---------------------------------
  66. package com.labs.ankitt.multichoice;
  67.  
  68. import android.content.Context;
  69. import android.support.annotation.NonNull;
  70. import android.support.v7.widget.RecyclerView;
  71. import android.view.LayoutInflater;
  72. import android.view.View;
  73. import android.view.ViewGroup;
  74. import android.widget.CheckBox;
  75.  
  76. import java.util.List;
  77.  
  78. public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.DataViewHolder> {
  79.  
  80.     private Context context;
  81.     private List<String> foodAllergy;
  82.  
  83.     public CustomAdapter(Context context, List<String> foodAllergy) {
  84.         this.context = context;
  85.         this.foodAllergy = foodAllergy;
  86.     }
  87.  
  88.     @NonNull
  89.     @Override
  90.     public CustomAdapter.DataViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
  91.         View inflater = LayoutInflater.from(context).inflate(R.layout.item_list_data, parent, false);
  92.         return new DataViewHolder(inflater);
  93.     }
  94.  
  95.     @Override
  96.     public void onBindViewHolder(@NonNull CustomAdapter.DataViewHolder holder, int position) {
  97.         holder.checkBoxData.setText(foodAllergy.get(position).toString());
  98.     }
  99.  
  100.     @Override
  101.     public int getItemCount() {
  102.         return foodAllergy.size();
  103.     }
  104.  
  105.     public class DataViewHolder extends RecyclerView.ViewHolder {
  106.  
  107.         private CheckBox checkBoxData;
  108.  
  109.         public DataViewHolder(View itemView) {
  110.             super(itemView);
  111.             checkBoxData = (CheckBox) itemView.findViewById(R.id.checkbox_data);
  112.         }
  113.     }
  114. }
  115. ---------------------------------------
  116. activity_main
  117.  
  118. <?xml version="1.0" encoding="utf-8"?>
  119. <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  120.     xmlns:app="http://schemas.android.com/apk/res-auto"
  121.     xmlns:tools="http://schemas.android.com/tools"
  122.     android:layout_width="match_parent"
  123.     android:layout_height="match_parent"
  124.     tools:context=".MainActivity">
  125.  
  126.     <Button
  127.         android:id="@+id/buttonFoodAllergy"
  128.         android:layout_width="0dp"
  129.         android:layout_height="wrap_content"
  130.         android:layout_marginEnd="16dp"
  131.         android:layout_marginStart="16dp"
  132.         android:layout_marginTop="33dp"
  133.         android:text="Select Food Allergy"
  134.         app:layout_constraintEnd_toEndOf="parent"
  135.         app:layout_constraintStart_toStartOf="parent"
  136.         app:layout_constraintTop_toTopOf="parent" />
  137. </android.support.constraint.ConstraintLayout>
  138.  
  139. ---------------------------------------------------
  140. custom_dialog
  141.  
  142. <?xml version="1.0" encoding="utf-8"?>
  143. <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  144.     xmlns:app="http://schemas.android.com/apk/res-auto"
  145.     xmlns:tools="http://schemas.android.com/tools"
  146.     android:layout_width="match_parent"
  147.     android:layout_height="match_parent">
  148.  
  149.     <android.support.v7.widget.RecyclerView
  150.         android:id="@+id/recycler_data"
  151.         android:layout_width="0dp"
  152.         android:layout_height="0dp"
  153.         app:layout_constraintBottom_toBottomOf="parent"
  154.         app:layout_constraintEnd_toEndOf="parent"
  155.         app:layout_constraintStart_toStartOf="parent"
  156.         app:layout_constraintTop_toTopOf="parent" />
  157. </android.support.constraint.ConstraintLayout>
  158.  
  159. -------------------------------------------------------
  160. item_data
  161. <?xml version="1.0" encoding="utf-8"?>
  162. <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  163.     xmlns:app="http://schemas.android.com/apk/res-auto"
  164.     xmlns:tools="http://schemas.android.com/tools"
  165.     android:layout_width="match_parent"
  166.     android:layout_height="match_parent">
  167.  
  168.     <CheckBox
  169.         android:id="@+id/checkbox_data"
  170.         android:layout_width="0dp"
  171.         android:layout_height="wrap_content"
  172.         android:layout_marginEnd="16dp"
  173.         android:layout_marginStart="16dp"
  174.         android:layout_marginTop="16dp"
  175.         app:layout_constraintEnd_toEndOf="parent"
  176.         app:layout_constraintStart_toStartOf="parent"
  177.         app:layout_constraintTop_toTopOf="parent" />
  178. </android.support.constraint.ConstraintLayout>
  179. -------------------
  180.  
  181. <?xml version="1.0" encoding="utf-8"?>
  182. <resources>
  183.     <string-array name="food_allergy">
  184.         <item>Ankit</item>
  185.         <item>Ankit</item>
  186.         <item>Ankit</item>
  187.         <item>Ankit</item>
  188.         <item>Ankit</item>
  189.         <item>Ankit</item>
  190.         <item>Ankit</item>
  191.         <item>Ankit</item>
  192.         <item>Ankit</item>
  193.         <item>Ankit</item>
  194.         <item>Ankit</item>
  195.         <item>Ankit</item>
  196.         <item>Ankit</item>
  197.     </string-array>
  198. </resources>
Add Comment
Please, Sign In to add comment