gpt4 book ai didi

java - 在android java中从XML填充ListView

转载 作者:行者123 更新时间:2023-12-01 13:56:24 24 4
gpt4 key购买 nike

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类(class)。以下 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>

最佳答案

您尚未初始化 ArrayList ArrayList <String> cars;

如下所示

cars = new ArrayList<String>();

关于java - 在android java中从XML填充ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19606535/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com