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;
}
}
これは私のJavaクラスです。次の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>
XML ドキュメントと ArrayList に入れられた値を解析してから、ListView で ArrayList 項目をプレビューしようとしていますが、多くのエラーが発生しています。誰かが私が間違っていることを教えてもらえますか?
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>