0
public class MainActivity extends Activity {

ArrayList <String> cars;
ListView list;
ArrayAdapter <String> 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 <String>(this, R.layout.carprofileview, cars);
    list.setAdapter(adapter);

    }
}

private boolean checkForCars() {

    if(getNodeValue().size() > 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< make.getLength(); i++){
         for(int j=0; j< model.getLength(); j++){
             car = make.item(i).getNodeValue() + " " + model.item(j).getNodeValue();
             cars.add(car);
            } 
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return cars;
    }
}

This is my java class. The following xml:

<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" >

<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" />

<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" />

<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"/>

<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" >
</ListView>

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?

R.layout.carproileview

    <?xml version="1.0" encoding="utf-8"?> 
<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"> 
<TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Large Text" android:textAppearance="?android:attr/textAppearanceLarge" /> 
</LinearLayout>    
4
  • Can you post the error log? Commented Oct 26, 2013 at 12:12
  • can you please show cars.xml content? Commented Oct 26, 2013 at 12:53
  • <?xml version="1.0" encoding="utf-8"?> <resources> <car> <carMake>Make</carMake> <carModel>Model</carModel> </car> </resources> Commented Oct 26, 2013 at 13:05
  • @Student22b Please post the adapter class for the listview if listview content are not visible. Adapter is the one who is responsible for rendering the content on the screen. Since you are using custom layout, you have to use custom adapter. Commented Oct 28, 2013 at 5:48

1 Answer 1

1

You have't initialize the ArrayList ArrayList <String> cars;

like below

cars = new ArrayList<String>();
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you!!! This resolved a problem, but it isn't working properly. My ListView is not showing any item, but the XML (to parse) is not empty. Can you help me with that?
This fixes an error, but it doesn't fix the actual problem with the design of the ListView and its content :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.