gpt4 book ai didi

java - 在android中,如何在 ListView 上使用xml pull解析器显示xml文件中的数据?

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

我是一名新的 Android 程序员,最近,

我正在使用ListView中的pull parser来解析xml文件。但我没能做到同样的事情。请建议我如何做到这一点。

我的实际代码是

MainActivity.java

package com.example.simplexmlpullapp;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends Activity {
ListView list;
TextView text1;
String str;
ArrayList<Data> arrdata;
ArrayList<String> arrayofString, parcedCountry;
String textforinput = "<foo>Hello World!</foo>";
String parsedData = "";
XmlPullParser xmlPullParser;
public myAdapter adapter;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// text1=(TextView)findViewById(R.id.text1);
list=(ListView)findViewById(R.id.li);

arrayofString = new ArrayList<String>();
parcedCountry = new ArrayList<String>();


try
{
InputStream is;
is=getAssets().open("countries.xml");
BufferedReader r = new BufferedReader(new InputStreamReader(is));
StringBuilder total = new StringBuilder();
String line;
while ((line = r.readLine()) != null)
{
total.append(line);
}



parseCountries(total.toString());

// ArrayAdapter<String>adapter=new ArrayAdapter<String>(this,
// android.R.layout.simple_list_item_1,parcedCountry);
// list.setAdapter(adapter);
}
catch (Exception e)
{
// TODO: handle exception
}

arrayofString=new ArrayList<String>();

}



private void parseCountries(String string)
{

int eventType=0;
String countries = null;
boolean countryFlag=false;
ArrayList<String>parsedCountry=new ArrayList<String>();
try
{
xmlPullParser=XmlPullParserFactory.newInstance().newPullParser();
xmlPullParser.setInput(new StringReader(string));
eventType = xmlPullParser.getEventType();
}
catch (XmlPullParserException e) {
e.printStackTrace();
}

while(eventType!=XmlPullParser.END_DOCUMENT)
{
switch (eventType)
{
case XmlPullParser.START_DOCUMENT:
break;

case XmlPullParser.START_TAG:
if(xmlPullParser.getName().equalsIgnoreCase("country"))
{
countryFlag=true;
}
break;
case XmlPullParser.TEXT:

if(countryFlag)
{
countries=xmlPullParser.getText().toString().trim();
Log.i("parse_example", "country"+countries);
}
break;

case XmlPullParser.END_TAG:
if(xmlPullParser.getName().equalsIgnoreCase("country"))
{
parsedCountry.add(countries);
countryFlag=false;
}
break;
//end of switch
}

try {
eventType=xmlPullParser.next();

// for(int i=0;i<arrdata.size();i++)
// {
//
// Data data=arrdata.get(i);
// arrayofString.add(data.getCountry());
// }

} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}


}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}


public class myAdapter extends ArrayAdapter<String>
{
private final Context context;
int id;
ArrayList<String> arrayList=new ArrayList<String>();
public myAdapter(Context context, int textViewResourceId,
ArrayList<String> objects) {

super(context, textViewResourceId, objects);
this.context=context;
id=textViewResourceId;
arrayList=objects;
// TODO Auto-generated constructor stub
}

@Override
public String getItem(int position) {
// TODO Auto-generated method stub
return super.getItem(position);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return super.getCount();
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return super.getItemId(position);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
return super.getView(position, convertView, parent);
myAdapter adapter=new myAdapter(this,android.R.,parcedCountry);
list.setAdapter(adapter);

}
}

}

MainActivity.xml

<LinearLayout 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:orientation="vertical" >

<ListView
android:id="@+id/li"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#000000"
>
</ListView>

</LinearLayout>

最佳答案

查看this使用 XML 解析器的示例:也许您会找到一些帮助。

关于java - 在android中,如何在 ListView 上使用xml pull解析器显示xml文件中的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18782946/

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