title: Android - Working with a lot of XML Data - I/art: Background partial concurrent mark sweep GC freed
body: <p>In my project I'm going to work with a lot of XML Data like this:</p>
  
  <p></p>
  
  <pre><code>&lt;person&gt;
      &lt;name&gt;Alex&lt;/name&gt;
      &lt;data1&gt;data1&lt;/data1&gt;
      &lt;data2&gt;data2&lt;/data2&gt;
      &lt;data3&gt;data3&lt;/data3&gt;
  &lt;/person&gt;
  
  &lt;person&gt;
      &lt;name&gt;paul&lt;/name&gt;
      &lt;data1&gt;data1&lt;/data1&gt;
      &lt;data2&gt;data2&lt;/data2&gt;
      &lt;data3&gt;data3&lt;/data3&gt;
  &lt;/person&gt;
  </code></pre>
  
  <p></p>
  
  <p>Those XML files got like 600+ entrys. I call their sites with fragments and loading them takes pretty long and throws: 
  "I/Choreographer: Skipped 47 frames!  The application may be doing too much work on its main thread."
  I already know how to work around that with starting another thread. Still it takes some seconds to open the pages and the "keyword serach" I've implemented causes the Android Monitor to throw 
  "I/art: Background partial concurrent mark sweep GC freed 83556(6MB) AllocSpace objects, 834(16MB) LOS objects, 45% free, 19MB/35MB, paused 296us total 124.540ms" 
  like every 3rd time. I've already read about that thats a normal thing happning but should be avoided.</p>
  
  <p>Since I'm still pretty new to Android I wonder if I'm doing it right or if I could handle the Data better using a database for example.</p>
  
  <pre><code>private static String getValue(String tag, Element element) {
      NodeList nodeList = element.getElementsByTagName(tag).item(0).getChildNodes();
      Node node = nodeList.item(0);
      return node.getNodeValue();
  }
  
  
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                           Bundle savedInstanceState) {
      // Inflate the layout for this fragment
      //return inflater.inflate(R.layout.fragment_coffee_pref, container, false);
      final View view =  inflater.inflate(R.layout.fragment_coffee_pref, container, false);
  
      try {
          // get XML file
          AssetManager assetManager = getActivity().getAssets();
  
          if(Locale.getDefault().getLanguage().equals("de")){
              is = assetManager.open("coffee_pref_DE.xml");
          }else{
              is = assetManager.open("coffee_pref.xml");
          }
  
  
          //init XML parser
          DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
          DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
          Document doc = dBuilder.parse(is);
  
          Element element=doc.getDocumentElement();
          element.normalize();
  
          // init table
          LinearLayout CPcontainer = (LinearLayout) view.findViewById(R.id.CPcontainer);
          TableRow.LayoutParams w50Layout = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT,0.50f);
          TableRow.LayoutParams w100Layout = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT,1.0f);
          //paramsExample.setMargins(2, 2, 2, 2); // (left, top, right, bottom);
  
          // go throught XML List and insert rows for each entry
          NodeList nList = doc.getElementsByTagName("person");
  
          for (int i=0; i&lt;nList.getLength(); i++) {
  
              Node node = nList.item(i);
              if (node.getNodeType() == Node.ELEMENT_NODE) {
  
                  TableLayout tableLayout = new TableLayout(getActivity());
                  tableLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));// assuming the parent view is a LinearLayout
  
                  TableRow row1= new TableRow(getActivity());
                  TableRow row2= new TableRow(getActivity());
                  TableRow row3= new TableRow(getActivity());
                  TableRow row4= new TableRow(getActivity());
                  TableRow.LayoutParams lp = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT);
                  row1.setLayoutParams(lp);
                  row2.setLayoutParams(lp);
                  row3.setLayoutParams(lp);
                  row4.setLayoutParams(lp);
  
                  Element element2 = (Element) node;
  
                  // row 1
                  TextView tvName = new TextView(getActivity());
                  tvName.setLayoutParams(w100Layout);
                  tvName.setText(Html.fromHtml("&lt;a href=\""+getValue("link", element2)+"\"&gt;"+getValue("name", element2)+"&lt;/a&gt;"));
                  tvName.setMovementMethod(LinkMovementMethod.getInstance());
                  tvName.setBackgroundResource(R.drawable.head_shape);
                  tvName.setPadding(30, 20, 30, 20); // (left, top, right, bottom);
  
                  row1.addView(tvName);
                  tableLayout.addView(row1);
  
                  // row 2
                  TextView tvData1 = new TextView(getActivity());
                  tvData1.setLayoutParams(w50Layout);
                  tvData1.setText(getValue("data1", element2));
                  tvData1.setBackgroundResource(R.drawable.cell_shape);
                  tvData1.setPadding(30, 20, 30, 20); // (left, top, right, bottom);
  
                  TextView textData1 = new TextView(getActivity());
                  textData1.setText(R.string.tableData1);
                  textData1.setLayoutParams(w50Layout);
                  //textData1.setTypeface(null, Typeface.BOLD);
                  textData1.setBackgroundResource(R.drawable.cell_shape);
                  textData1.setPadding(30, 20, 30, 20); // (left, top, right, bottom);
  
                  row2.addView(textData1);
                  row2.addView(tvData1);
                  tableLayout.addView(row2);
  
                  // row3
                  TextView tvData2 = new TextView(getActivity());
                  tvData2.setLayoutParams(w50Layout);
                  tvData2.setText(getValue("data2", element2));
                  tvData2.setBackgroundResource(R.drawable.cell_shape);
                  tvData2.setPadding(30, 20, 30, 20); // (left, top, right, bottom);
  
                  TextView textData2 = new TextView(getActivity());
                  textData2.setText(R.string.tableData2);
                  textData2.setLayoutParams(w50Layout);
                  //textData2.setTypeface(null, Typeface.BOLD);
                  textData2.setBackgroundResource(R.drawable.cell_shape);
                  textData2.setPadding(30, 20, 30, 20); // (left, top, right, bottom);
  
                  row3.addView(textData2);
                  row3.addView(tvData2);
                  tableLayout.addView(row3);
  
                  // row 4
                  TextView tvData3 = new TextView(getActivity());
                  tvData3.setLayoutParams(w50Layout);
                  tvData3.setText(getValue("sugar", element2));
                  tvData3.setBackgroundResource(R.drawable.cell_shape);
                  tvData3.setPadding(30, 20, 30, 20); // (left, top, right, bottom);
  
                  TextView textData3 = new TextView(getActivity());
                  textData3.setText(R.string.tableData3);
                  textData3.setLayoutParams(w50Layout);
                  //textData3.setTypeface(null, Typeface.BOLD);
                  textData3.setBackgroundResource(R.drawable.cell_shape);
                  textData3.setPadding(30, 20, 30, 20); // (left, top, right, bottom);
  
                  row4.addView(textData3);
                  row4.addView(tvData3);
                  tableLayout.addView(row4);
  
                  // add table to layout
                  CPcontainer.addView(tableLayout);
              }
  
          }
  
  
      } catch (Exception e) {e.printStackTrace();}
  
  
      searchforName = (EditText) view.findViewById(R.id.searchForName);
  
      searchforName.addTextChangedListener(new TextWatcher() {
  
          public void afterTextChanged(Editable s) {
              getActivity().runOnUiThread(new Runnable() {
                  @Override
                  public void run() {
  
                  String searchedName = searchforName.getText().toString().toLowerCase();
  
                      LinearLayout CPcontainer = (LinearLayout) view.findViewById(R.id.CPcontainer);
                      for (int i = 1, j = CPcontainer.getChildCount(); i &lt; j; i++) {
                          View view2 = CPcontainer.getChildAt(i);
                          if(view2 instanceof TableLayout) {
                              TableLayout table2 = (TableLayout) view2;
  
                                  TableRow row2 = (TableRow) table2.getChildAt(0);
                                  if (!TextUtils.isEmpty(searchedName)) {
                                      TextView searchableName = (TextView) row2.getChildAt(0);
                                      String sName = Html.fromHtml(searchableName.getText().toString()).toString();
                                      if (sName.startsWith(searchedName) || sName.equals(searchedName)) {
                                          //if (sName.toLowerCase().contains(searchedName)) {
                                          table2.setVisibility(View.VISIBLE);
                                      } else {
                                          table2.setVisibility(View.GONE);
                                      }
                                  } else {
                                      table2.setVisibility(View.VISIBLE);
                                  }
                              }
                      }
                  }
              });
  
  
          }
  
          public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
  
          public void onTextChanged(CharSequence s, int start, int before, int count) {}
      });
  
      return view;
  }
  </code></pre>
  
  <p>Also since I've implemented a HTML link inside the name TextView I can't search for it anymore and I couldn't figure out how to get around it. </p>
  
  <pre><code>String sName = Html.fromHtml(searchableName.getText().toString()).toString();
  </code></pre>
  
  <p>I'm gonna be thankful for every help or information about my issues!</p>