我正在尝试解析具有以下结构的xml文件:
<?xml version="1.0" encoding="utf-8"?>
<root>
<tag1 id="92">
<tag11>Some text for parsing</tag11>
<tag12 src="http://some_valid_url_with_image" />
</tag1>
<tag2 attr1="111111" attr2="222222" attr2="333333">
<tag21>Some text for parsing 2</tag21>
<tag22 attr1="4444444">Some text for parsing 3</tag22>
<tag23 attr1="5555555">Some text for parsing 4</tag23>
<tag24 attr1="32">Some text for parsing 5</tag24>
<tag25 attr1="21">Some text for parsing 6</tag25>
</tag2>
</root>
我将XMLPullParser与开关盒结构和我自己的适配器一起使用:
public class XMLParse extends AppCompatActivity {
String tagname = "";
String tagtext = "";
String tag11 = "tag11";
String tag12 = "tag12";
String tag12VALUE = "";
public Bitmap myBitmap;
private List<SomeItem> items = new ArrayList();
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_some);
listView = findViewById(R.id.listView);
IV = findViewById(R.id.iconsrc);
try {
XmlPullParser parser = getResources().getXml(R.xml.myxml);
while (parser.next() != XmlPullParser.END_DOCUMENT) {
switch (parser.getEventType()){
case XmlPullParser.TEXT :
tagtext = parser.getText();
if (tagname != null && tagname.equals(tag11)) {
items.add(new SomeItem(tagtext, IV));
}
if (tagname != null && tagname.equals(tag12)){
tag12VALUE = parser.getAttributeValue(0);
InputStream input = new URL(tag12VALUE).openStream();
myBitmap = BitmapFactory.decodeStream(input);
IV.setImageBitmap(myBitmap);
}
break;
case XmlPullParser.START_TAG:
tagname = parser.getName();
break;
case XmlPullParser.END_TAG:
tagname = parser.getName();
break;
}
}
} catch (Throwable t) {
Toast.makeText(this,
"Error while loading xml document: " + t.toString(), Toast.LENGTH_LONG)
.show();
}
SomeItemAdapter adapter = new SomeItemAdapter(this, R.layout.list_item, items);
listView.setAdapter(adapter);
}
这是我的适配器:
public class SomeItemAdapter extends ArrayAdapter<SomeItem>{
private LayoutInflater inflater;
private int layout;
private List<SomeItem> items;
public SomeItemAdapter (Context context, int resource, List<SomeItem> items) {
super(context, resource, items);
this.items= items;
this.layout = resource;
this.inflater = LayoutInflater.from(context);
}
public View getView(int position, View convertView, ViewGroup parent) {
View view=inflater.inflate(this.layout, parent, false);
ImageView icon = view.findViewById(R.id.iconsrc);
TextView nameView = view.findViewById(R.id.name);
SomeItem item = items.get(position);
//i think here i must TODO some code with ImageView and TextView,
smth like setImageResource and setText, but how I can do this If I getting my text and image while parsing?
return view;
}
}
最后一堂课,有我的作品:
public class SomeItem{
private String name;
private ImageView iconsrc;
public SomeItem(String name, ImageView iconsrc){
this.name=name;
this.iconsrc=iconsrc;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public ImageView getIconResource() {
return this.iconsrc;
}
public void setIconResource(ImageView iconsrc) {
this.iconsrc = iconsrc;
}
}
list_item.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="16dp">
<ImageView
android:id="@+id/iconsrc"
android:layout_marginRight="16dp"
android:layout_marginEnd="16dp"
android:layout_width="40dp"
android:layout_height="40dp" />
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
>
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name"
android:gravity="center|left"
/>
</LinearLayout>
</LinearLayout>
我希望获取带图像的ListView,该URL是从xml解析的,并且将从此xml中的标记中获取一些文本。 但是实际上我的ListView中什么也没得到。
正如我在代码中所写的那样,我认为我不是我的适配器的财产,所以主要问题是:我的代码有什么问题以及如何获取具有解析数据的ListView?
UPD: 列表视图中的文本问题已解决,但现在图像加载出现问题:
if (tagname != null && tagname.equals(tag12)){
这里的tagname = tag11,为什么?我应该在其他地方或其他地方调用parser.next()?