赞
踩
1、首先,在布局文件中写一个Button控件和用来展示数据的ListView控件。
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="获取XML"
android:onClick="getdata"/>
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/lv_main_data">
</ListView>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="dfd"
android:id="@+id/tv_listview_one"
/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="dfd"
android:id="@+id/tv_listview_two"
/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="dfd"
android:id="@+id/tv_listview_three"
/>
然后在MainActivity.java通过解析XML拿到数据。解析XML有三种方法,方法如下:
一、DOM解析器
DOM(Document Object Model):是一种用于XML文档的对象模型,直接访问XML文档中所有部分。
特点:解析速度快,一次性解析整个文档。加载大文件时候效率低。代码如下:
- public class MainActivity extends AppCompatActivity {
- private ListView lv_main_data;
- private ProgressDialog pd;
- private List<UserEntity> lists = new ArrayList<>();
- private MainActivity.myadapter myadapter;
- String currentTag=null;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- lv_main_data = (ListView) findViewById(R.id.lv_main_data);//拿到listview的ID
- myadapter = new myadapter();
- lv_main_data.setAdapter(myadapter);
- pd = new ProgressDialog(this);//动画加载中
- pd.setMessage("locaging.....");
- }
- class myadapter extends BaseAdapter{
-
- @Override
- public int getCount() {
- return lists.size();
- }
- @Override
- public Object getItem(int position) {
- return lists.get(position);
- }
- @Override
- public long getItemId(int position) {
- return position;
- }
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- if (convertView==null){
- convertView= LayoutInflater.from(MainActivity.this).inflate(R.layout.dom_listview,null);
- DomActivity activity=new DomActivity();
- activity.uname= (TextView) convertView.findViewById(R.id.tv_listview_one);
- activity.content= (TextView) convertView.findViewById(R.id.tv_listview_two);
- activity.anthour= (TextView) convertView.findViewById(R.id.tv_listview_three);
- convertView.setTag(activity);
- }
- DomActivity domActivity= (DomActivity) convertView.getTag();
- domActivity.uname.setText(lists.get(position).getUname());
- domActivity.content.setText(lists.get(position).getContent());
- domActivity.anthour.setText(lists.get(position).getAnthour());
- return convertView;
- }
- }
- //获取xml数据
- public void getdata(View view){
- new mytask().execute();
- pd.show();
- }
- //写一个继承AsyncTask,重写它的两个方法
- // 1、获取网络数据:doInBackground
- // 2、更新UI(数据):onPostExecute
- class mytask extends AsyncTask{
-
- private UserEntity userEntity;
- private UserEntity userEntity1;
-
- @Override
- protected Object doInBackground(Object[] params)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。