Android pull解析xml存放展示recyclerView

主类:

package com.example.day12rikao;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;

import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private List<xmlStringtest> lists = new ArrayList<>();
    private String xmlString = "<apps>\n" +
            "\t<app>\n" +
            "\t\t<id>1</id>\n" +
            "\t\t<name>jim</name>\n" +
            "\t\t<address>河北</address>\n" +
            "\t</app>\n" +
            "\t<app>\n" +
            "\t\t<id>2</id>\n" +
            "\t\t<name>anna</name>\n" +
            "\t\t<address>北京</address>\n" +
            "\t</app>\n" +
            "\t<app>\n" +
            "\t\t<id>3</id>\n" +
            "\t\t<name>tom</name>\n" +
            "\t\t<address>天津</address>\n" +
            "\t</app>\n" +
            "</apps>";
    private Button button;
    private RecyclerView recyclerview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();

        XmlPullWithParser(xmlString);


    }

    private void initView() {
        button = (Button) findViewById(R.id.button);
        recyclerview = (RecyclerView) findViewById(R.id.recyclerview);
        LinearLayoutManager linearLayoutManager=new LinearLayoutManager(this);
        recyclerview.setLayoutManager(linearLayoutManager);

        button.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button:

                //适配
                MyRecyclerView myRecyclerView=new MyRecyclerView(this,lists);
                recyclerview.setAdapter(myRecyclerView);
                break;
        }
    }

    private void XmlPullWithParser(String xmlString) {

        try {
            //解析器
            XmlPullParserFactory xmlPullParserFactory = XmlPullParserFactory.newInstance();
            XmlPullParser xmlPullParser = xmlPullParserFactory.newPullParser();

            //要解析的xml
            StringReader stringReader = new StringReader(xmlString);
            xmlPullParser.setInput(stringReader);

            //类型
            int eventType = xmlPullParser.getEventType();

            String id = "";
            String name = "";
            String address = "";

            //不是结束标签就循环
            while (eventType != XmlPullParser.END_DOCUMENT) {

                //标签名
                String nodename = xmlPullParser.getName();
                switch (eventType) {
                    case XmlPullParser.START_TAG:

                        //开始标签
                        if ("id".equals(nodename)) {
                            id = xmlPullParser.nextText();
                        } else if ("name".equals(nodename)) {
                            name = xmlPullParser.nextText();
                        } else if ("address".equals(nodename)) {
                            address = xmlPullParser.nextText();
                        }
                        break;

                    case XmlPullParser.END_TAG:

                        //结束标签
                        if ("app".equals(nodename)) {

                            xmlStringtest xmlStringtest = new xmlStringtest(id, name, address);
                            lists.add(xmlStringtest);
//                            Log.e("hh",id);
//                            Log.e("hh",name);
//                            Log.e("hh",address);
                        }
                        break;
                }
                //下一个节点
                eventType = xmlPullParser.next();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

主布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:orientation="vertical"
    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"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/button"
        android:text="点击展示解析后的xml"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    </android.support.v7.widget.RecyclerView>

</LinearLayout>

xmlStringtest:

package com.example.day12rikao;


public class xmlStringtest {

    private String id;
    private String name;
    private String address;

    @Override
    public String toString() {
        return "xmlStringtest{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", address='" + address + '\'' +
                '}';
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public xmlStringtest(String id, String name, String address) {

        this.id = id;
        this.name = name;
        this.address = address;
    }
}

myrecyclerView:

package com.example.day12rikao;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import java.util.List;

public class MyRecyclerView extends RecyclerView.Adapter<MyHolder> {

    private Context context;
    private List<xmlStringtest> lists;

    public MyRecyclerView(Context context, List<xmlStringtest> lists) {
        this.context = context;
        this.lists = lists;
    }

    @NonNull
    @Override
    public MyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(context).inflate(R.layout.item,null);
        MyHolder myHolder=new MyHolder(view);

        return myHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull MyHolder holder, final int position) {

        holder._id.setText(lists.get(position).getId());
        holder._name.setText(lists.get(position).getName());
        holder._address.setText(lists.get(position).getAddress());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context, lists.get(position).getName(), Toast.LENGTH_SHORT).show();
            }
        });

    }

    @Override
    public int getItemCount() {
        return lists.size();
    }
}

item布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/_id"
        android:layout_width="match_parent"
        android:layout_height="50dp" />
    <TextView
        android:id="@+id/_name"
        android:layout_width="match_parent"
        android:layout_height="50dp" />
    <TextView
        android:id="@+id/_address"
        android:layout_width="match_parent"
        android:layout_height="50dp" />
</LinearLayout>

myHolder:

package com.example.day12rikao;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

class MyHolder extends RecyclerView.ViewHolder {
    public TextView _id;
    public TextView _name;
    public TextView _address;

    public MyHolder(View itemView) {
        super(itemView);

        _id = itemView.findViewById(R.id._id);
        _name = itemView.findViewById(R.id._name);
        _address = itemView.findViewById(R.id._address);
    }
}

猜你喜欢

转载自blog.csdn.net/LIXIAONA_1101/article/details/81703037
今日推荐