Advertisement
Guest User

Code MainActivity.java

a guest
Feb 6th, 2019
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.16 KB | None | 0 0
  1. package fr.seolis.alertesseolis;
  2.  
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.os.Environment;
  6. import android.support.v7.app.AppCompatActivity;
  7. import android.util.Log;
  8. import android.view.View;
  9. import android.widget.Button;
  10. import android.widget.ExpandableListView;
  11.  
  12. import org.xmlpull.v1.XmlPullParser;
  13. import org.xmlpull.v1.XmlPullParserException;
  14. import org.xmlpull.v1.XmlPullParserFactory;
  15.  
  16. import fr.seolis.alertesseolis.Model.DataItem;
  17. import fr.seolis.alertesseolis.Model.SubCategoryItem;
  18.  
  19. import java.io.FileNotFoundException;
  20. import java.io.IOException;
  21. import java.io.StringReader;
  22. import java.util.ArrayList;
  23. import java.util.HashMap;
  24. import java.io.FileOutputStream;
  25. import java.io.File;
  26. import java.io.Reader;
  27.  
  28. public class MainActivity extends AppCompatActivity {
  29.  
  30.     private Button btn;
  31.     private ExpandableListView lvCategory;
  32.  
  33.     private ArrayList<DataItem> arCategory;
  34.     private ArrayList<SubCategoryItem> arSubCategory;
  35.     private ArrayList<ArrayList<SubCategoryItem>> arSubCategoryFinal;
  36.  
  37.     private ArrayList<HashMap<String, String>> parentItems;
  38.     private ArrayList<ArrayList<HashMap<String, String>>> childItems;
  39.     private MyCategoriesExpandableListAdapter myCategoriesExpandableListAdapter;
  40.  
  41.     @Override
  42.     protected void onCreate(Bundle savedInstanceState) {
  43.         super.onCreate(savedInstanceState);
  44.         setContentView(R.layout.activity_main);
  45.  
  46.         btn = findViewById(R.id.btnEnregistrer);
  47.  
  48.         btn.setOnClickListener(new View.OnClickListener() {
  49.             @Override
  50.             public void onClick(View v) {
  51.                 Intent intent = new Intent(MainActivity.this,CheckedActivity.class);
  52.                 startActivity(intent);
  53.             }
  54.         });
  55.  
  56.         setupReferences();
  57.     }
  58.  
  59.     public void readXml(){
  60.         final String xmlFile = "userData";
  61.         ArrayList<String> userData = new ArrayList<String>();
  62.         try {
  63.             FileOutputStream fileos = new FileOutputStream (new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + "AlertesSeolis" + "/" + "userData.xml"));
  64.             inputBuffer = new char[fis.available()];
  65.             isr.read(inputBuffer);
  66.             data = new String(inputBuffer);
  67.             isr.close();
  68.             fis.close();
  69.         }
  70.         catch (FileNotFoundException e3) {
  71.             // TODO Auto-generated catch block
  72.             e3.printStackTrace();
  73.         }
  74.         catch (IOException e) {
  75.             // TODO Auto-generated catch block
  76.             e.printStackTrace();
  77.         }
  78.         XmlPullParserFactory factory = null;
  79.         try {
  80.             factory = XmlPullParserFactory.newInstance();
  81.         }
  82.         catch (XmlPullParserException e2) {
  83.             // TODO Auto-generated catch block
  84.             e2.printStackTrace();
  85.         }
  86.         factory.setNamespaceAware(true);
  87.         XmlPullParser xpp = null;
  88.         try {
  89.             xpp = factory.newPullParser();
  90.         }
  91.         catch (XmlPullParserException e2) {
  92.             // TODO Auto-generated catch block
  93.             e2.printStackTrace();
  94.         }
  95.         try {
  96.             xpp.setInput(new StringReader(data));
  97.         }
  98.         catch (XmlPullParserException e1) {
  99.             // TODO Auto-generated catch block
  100.             e1.printStackTrace();
  101.         }
  102.         int eventType = 0;
  103.         try {
  104.             eventType = xpp.getEventType();
  105.         }
  106.         catch (XmlPullParserException e1) {
  107.             // TODO Auto-generated catch block
  108.             e1.printStackTrace();
  109.         }
  110.         while (eventType != XmlPullParser.END_DOCUMENT){
  111.             if (eventType == XmlPullParser.START_DOCUMENT) {
  112.                 System.out.println("Start document");
  113.             }
  114.             else if (eventType == XmlPullParser.START_TAG) {
  115.                 System.out.println("Start tag "+xpp.getName());
  116.             }
  117.             else if (eventType == XmlPullParser.END_TAG) {
  118.                 System.out.println("End tag "+xpp.getName());
  119.             }
  120.             else if(eventType == XmlPullParser.TEXT) {
  121.                 userData.add(xpp.getText());
  122.             }
  123.             try {
  124.                 eventType = xpp.next();
  125.             }
  126.             catch (XmlPullParserException e) {
  127.                 // TODO Auto-generated catch block
  128.                 e.printStackTrace();
  129.             }
  130.             catch (IOException e) {
  131.                 // TODO Auto-generated catch block
  132.                 e.printStackTrace();
  133.             }
  134.         }
  135.         String userName = userData.get(0);
  136.         String password = userData.get(1);
  137.     }
  138.  
  139.     private void setupReferences() {
  140.  
  141.         lvCategory = findViewById(R.id.EVAbos);
  142.         arCategory = new ArrayList<>();
  143.         arSubCategory = new ArrayList<>();
  144.         parentItems = new ArrayList<>();
  145.         childItems = new ArrayList<>();
  146.  
  147.         /*DataItem dataItem = new DataItem();
  148.         dataItem.setCategoryId("1");
  149.         dataItem.setCategoryName("Adventure");
  150.  
  151.         arSubCategory = new ArrayList<>();
  152.         for(int i = 1; i < 6; i++) {
  153.  
  154.             SubCategoryItem subCategoryItem = new SubCategoryItem();
  155.             subCategoryItem.setCategoryId(String.valueOf(i));
  156.             subCategoryItem.setIsChecked(ConstantManager.CHECK_BOX_CHECKED_FALSE);
  157.             subCategoryItem.setSubCategoryName("Adventure: "+i);
  158.             arSubCategory.add(subCategoryItem);
  159.         }
  160.         dataItem.setSubCategory(arSubCategory);
  161.         arCategory.add(dataItem);
  162.  
  163.         dataItem = new DataItem();
  164.         dataItem.setCategoryId("2");
  165.         dataItem.setCategoryName("Art");
  166.         arSubCategory = new ArrayList<>();
  167.         for(int j = 1; j < 6; j++) {
  168.  
  169.             SubCategoryItem subCategoryItem = new SubCategoryItem();
  170.             subCategoryItem.setCategoryId(String.valueOf(j));
  171.             subCategoryItem.setIsChecked(ConstantManager.CHECK_BOX_CHECKED_FALSE);
  172.             subCategoryItem.setSubCategoryName("Art: "+j);
  173.             arSubCategory.add(subCategoryItem);
  174.         }
  175.         dataItem.setSubCategory(arSubCategory);
  176.         arCategory.add(dataItem);*/
  177.  
  178.  
  179.  
  180.  
  181.         Log.d("TAG", "setupReferences: "+arCategory.size());
  182.  
  183.         for(DataItem data : arCategory){
  184. //                        Log.i("Item id",item.id);
  185.             ArrayList<HashMap<String, String>> childArrayList =new ArrayList<HashMap<String, String>>();
  186.             HashMap<String, String> mapParent = new HashMap<String, String>();
  187.  
  188.             mapParent.put(ConstantManager.Parameter.CATEGORY_ID,data.getCategoryId());
  189.             mapParent.put(ConstantManager.Parameter.CATEGORY_NAME,data.getCategoryName());
  190.  
  191.             int countIsChecked = 0;
  192.             for(SubCategoryItem subCategoryItem : data.getSubCategory()) {
  193.  
  194.                 HashMap<String, String> mapChild = new HashMap<String, String>();
  195.                 mapChild.put(ConstantManager.Parameter.SUB_ID,subCategoryItem.getSubId());
  196.                 mapChild.put(ConstantManager.Parameter.SUB_CATEGORY_NAME,subCategoryItem.getSubCategoryName());
  197.                 mapChild.put(ConstantManager.Parameter.CATEGORY_ID,subCategoryItem.getCategoryId());
  198.                 mapChild.put(ConstantManager.Parameter.IS_CHECKED,subCategoryItem.getIsChecked());
  199.  
  200.                 if(subCategoryItem.getIsChecked().equalsIgnoreCase(ConstantManager.CHECK_BOX_CHECKED_TRUE)) {
  201.  
  202.                     countIsChecked++;
  203.                 }
  204.                 childArrayList.add(mapChild);
  205.             }
  206.  
  207.             if(countIsChecked == data.getSubCategory().size()) {
  208.  
  209.                 data.setIsChecked(ConstantManager.CHECK_BOX_CHECKED_TRUE);
  210.             }else {
  211.                 data.setIsChecked(ConstantManager.CHECK_BOX_CHECKED_FALSE);
  212.             }
  213.  
  214.             mapParent.put(ConstantManager.Parameter.IS_CHECKED,data.getIsChecked());
  215.             childItems.add(childArrayList);
  216.             parentItems.add(mapParent);
  217.  
  218.         }
  219.  
  220.         ConstantManager.parentItems = parentItems;
  221.         ConstantManager.childItems = childItems;
  222.  
  223.         myCategoriesExpandableListAdapter = new MyCategoriesExpandableListAdapter(this,parentItems,childItems,false);
  224.         lvCategory.setAdapter(myCategoriesExpandableListAdapter);
  225.     }
  226. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement