下面是详细讲解“Android解析XML(PULL)展示到ListView”的完整攻略:
一、PULL解析XML
PULL是一种常用的解析XML文件的方式,它的优点是速度快,内存占用少,应用广泛,下面是使用PULL解析XML文件的步骤:
-
获取XmlPullParser对象
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
XmlPullParser parser = factory.newPullParser(); -
设置输入流和编码方式
parser.setInput(inputstream, "UTF-8");
-
获取节点事件类型并循环解析
java
//定义一个List用来保存xml数据
List<SomeModel> dataList = new ArrayList<SomeModel>();
SomeModel data;
int eventType = parser.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
String nodeName = parser.getName();
switch (eventType) {
case XmlPullParser.START_TAG:
//解析节点,每个节点的解析方式不同,需要根据具体情况处理
if ("node_name".equals(nodeName)) {
data = new SomeModel();
data.setId(Integer.parseInt(parser.getAttributeValue(null, "id")));
data.setName(parser.getAttributeValue(null, "name"));
dataList.add(data);
}
break;
case XmlPullParser.END_TAG:
break;
default:
break;
}
eventType = parser.next();
}
//解析完成,返回数据
return dataList;
二、把解析好的数据展示到ListView上
实现把解析好的数据展示到ListView上,需要分别实现ListView的Adapter和Item布局文件,具体步骤如下:
-
实现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">
<TextView
android:id="@+id/tv_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<TextView
android:id="@+id/tv_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</LinearLayout> -
实现ListView的Adapter
```java
public class MyAdapter extends BaseAdapter {
private ListmDataList;
private Context mContext;public MyAdapter(List<SomeModel> dataList, Context context) { this.mDataList = dataList; this.mContext = context; } @Override public int getCount() { return mDataList.size(); } @Override public Object getItem(int position) { return mDataList.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder; if (convertView == null) { convertView = LayoutInflater.from(mContext).inflate(R.layout.item_layout, parent, false); holder = new ViewHolder(); holder.tv_id = convertView.findViewById(R.id.tv_id); holder.tv_name = convertView.findViewById(R.id.tv_name); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } SomeModel data = mDataList.get(position); holder.tv_id.setText(String.valueOf(data.getId())); holder.tv_name.setText(data.getName()); return convertView; } static class ViewHolder { TextView tv_id; TextView tv_name; }
}
``` -
在Activity中使用Adapter展示ListView
首先,我们需要在Activity中获取解析好的List数据,这里就不演示具体获取数据的方法了。
java
ListView listView = findViewById(R.id.listview);
MyAdapter adapter = new MyAdapter(dataList, this);
listView.setAdapter(adapter); -
带有搜索功能的ListView展示
```java
ListView listView = findViewById(R.id.listview);
MyAdapter adapter = new MyAdapter(dataList, this);
listView.setAdapter(adapter);EditText et_search = findViewById(R.id.et_search);
et_search.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}@Override public void onTextChanged(CharSequence s, int start, int before, int count) { } @Override public void afterTextChanged(Editable s) { String searchText = s.toString(); List<SomeModel> searchList = new ArrayList<>(); if (TextUtils.isEmpty(searchText)) { searchList.addAll(dataList); } else { for (SomeModel data : dataList) { if (data.getName().contains(searchText)) { searchList.add(data); } } } adapter.setData(searchList); adapter.notifyDataSetChanged(); }
});
```
至此,整个“Android解析XML(PULL)展示到ListView”的完整攻略已结束,希望能对您有所帮助。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Android解析XML(PULL)展示到ListView - Python技术站