title: Fill ListView from XML in android java
body: <pre><code>public class MainActivity extends Activity {
  
  ArrayList &lt;String&gt; cars;
  ListView list;
  ArrayAdapter &lt;String&gt; adapter;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      populateListView();
  
  }
  
  private void populateListView() {
      if(!checkForCars()){
  
      } else{
      list = (ListView) findViewById(R.id.listView1);
      adapter = new ArrayAdapter &lt;String&gt;(this, R.layout.carprofileview, cars);
      list.setAdapter(adapter);
  
      }
  }
  
  private boolean checkForCars() {
  
      if(getNodeValue().size() &gt; 0){
          return true;
      }
      return false;
  
  }
  
  private ArrayList getNodeValue(){
  
      try {
       DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
          DocumentBuilder builder = domFactory.newDocumentBuilder();
          Document dDoc = builder.parse("raw/cars.xml");
  
          XPath xPath = XPathFactory.newInstance().newXPath();
  
          NodeList make = (NodeList) xPath.evaluate("//carMake//text()", dDoc, XPathConstants.NODE);
          NodeList model = (NodeList) xPath.evaluate("//carModel//text()", dDoc, XPathConstants.NODE);
  
          String car;
  
          for(int i=0; i&lt; make.getLength(); i++){
           for(int j=0; j&lt; model.getLength(); j++){
               car = make.item(i).getNodeValue() + " " + model.item(j).getNodeValue();
               cars.add(car);
              } 
          }
      } catch (Exception e) {
          e.printStackTrace();
      }
      return cars;
      }
  }
  </code></pre>
  
  <p>This is my java class. The following xml:</p>
  
  <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:paddingBottom="@dimen/activity_vertical_margin"
  android:paddingLeft="@dimen/activity_horizontal_margin"
  android:paddingRight="@dimen/activity_horizontal_margin"
  android:paddingTop="@dimen/activity_vertical_margin"
  tools:context=".MainActivity" &gt;
  
  &lt;TextView
      android:id="@+id/textView1"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:gravity="center"
      android:text="Choose a profile"
      android:textSize="25dp" /&gt;
  
  &lt;Button
      android:id="@+id/bCreateCar"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:layout_alignLeft="@+id/textView1"
      android:layout_below="@+id/textView1"
      android:layout_marginTop="46dp"
      android:text="Create a new car profile" /&gt;
  
  &lt;TextView
      android:id="@+id/textView2"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignLeft="@+id/bCreateCar"
      android:layout_below="@+id/bCreateCar"
      android:layout_marginTop="25dp"
      android:text="Choose car profile"
      android:textSize="15dp"/&gt;
  
  &lt;ListView
      android:id="@+id/listView1"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_alignLeft="@+id/textView2"
      android:layout_below="@+id/textView2"
      android:layout_marginTop="32dp" &gt;
  &lt;/ListView&gt;
  </code></pre>
  
  <p></p>
  
  <p>I am trying to parse a XML document and the values put in a ArrayList, and then preview the ArrayList items in a ListView but I am getting a lot of errors. Can someone tell me what am I doing wrong?</p>
  
  <h2>R.layout.carproileview</h2>
  
  <pre><code>    &lt;?xml version="1.0" encoding="utf-8"?&gt; 
  &lt;LinearLayout xmlns:android="schemas.android.com/apk/res/android"; android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:gravity="center"&gt; 
  &lt;TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Large Text" android:textAppearance="?android:attr/textAppearanceLarge" /&gt; 
  &lt;/LinearLayout&gt;    
  </code></pre>