安卓 (xml解析、高德地图)

DOM解析

笔记

一。重要的方法:

  • 1.Document:getElementsByTagName(节点名称):根据节点名称获得节点列表
  • 2.Node:getChildNodes():获得当前节点所有的子节点
  • 3.Node:getAttributes():获取当前节点所有的属性和值
  • 4.Node:getNodeValue():获得当前自节点的值
  • 5.Node:getTextContent():获得当前节点的文本
  • 6.Node:getNodeName():获得当前节点的名称

效果图

在这里插入图片描述

布局文件

// An highlighted block
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="bw.com.unit12.Main12Activity"
    android:orientation="vertical"
    >

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

</LinearLayout>

RecyclerView适配器布局文件

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

    <TextView
    android:id="@+id/tv1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="15dp"
    />

    <TextView
        android:id="@+id/tv2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="15dp"
        />

    <TextView
        android:id="@+id/tv3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="15dp"
        />

    <TextView
        android:id="@+id/tv4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="15dp"
        />

    <TextView
        android:id="@+id/tv5"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="15dp"
        />

</LinearLayout>

xml文件

// An highlighted block
<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
    <book id="1">
        <name>冰与火之歌</name>
        <author>乔治马丁</author>
        <year>2014</year>
        <price>89</price>
    </book>
    <book id="2">
        <name>安徒生童话</name>
        <author>安徒生</author>
        <year>2004</year>
        <price>77</price>
    </book>
    <book id="3">
        <name>三国演义</name>
        <author>罗贯中</author>
        <year>2008</year>
        <price>77</price>
    </book>
    <book id="4">
        <name>红楼梦</name>
        <author>曹雪芹</author>
        <year>2004</year>
        <price>77</price>
    </book>
    <book id="5">
        <name>西游记</name>
        <author>吴承恩</author>
        <year>2004</year>
        <price>77</price>
    </book>
    <book id="6">
        <name>水浒传</name>
        <author>施耐庵</author>
        <year>2004</year>
        <price>77</price>
    </book>
    <book id="7">
        <name>斗破苍穹</name>
        <author>我爱吃西红柿</author>
        <year>2004</year>
        <price>77</price>
    </book>
    <book id="8">
        <name>狂枭</name>
        <author>实验小白鼠</author>
        <year>2004</year>
        <price>77</price>
    </book>
    <book id="9">
        <name>雪中悍刀行</name>
        <author>烽火戏诸侯</author>
        <year>2004</year>
        <price>77</price>
    </book>
    <book id="10">
        <name>绝世唐门</name>
        <author>唐家三少</author>
        <year>2004</year>
        <price>77</price>
    </book>
</bookstore>

适配器代码

// An highlighted block
package bw.com.unit12;

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

import java.util.ArrayList;

import bw.com.unit1.R;

public class RvAdapter extends RecyclerView.Adapter<RvAdapter.RvHolder>{

    private Context context;
    private ArrayList<BookBean> bookBeans;
    private RvInterface rvInterface;

    public RvAdapter(Context context, ArrayList<BookBean> bookBeans) {
        this.context = context;
        this.bookBeans = bookBeans;
    }

    public void setRvInterface(RvInterface rvInterface){
        this.rvInterface = rvInterface;
    }

    @NonNull
    @Override
    public RvHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(context).inflate(R.layout.layout_domrv, viewGroup, false);
        return new RvHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RvHolder rvHolder, final int i) {
        rvHolder.textView1.setText("id:"+bookBeans.get(i).getId());
        rvHolder.textView2.setText("name:"+bookBeans.get(i).getName());
        rvHolder.textView3.setText("author:"+bookBeans.get(i).getAuthor());
        rvHolder.textView4.setText("year:"+bookBeans.get(i).getPrice());
        rvHolder.textView5.setText("price:"+bookBeans.get(i).getYear());
        rvHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                rvInterface.onClick(i);
            }
        });
    }

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

    class RvHolder extends RecyclerView.ViewHolder{
        TextView textView1;
        TextView textView2;
        TextView textView3;
        TextView textView4;
        TextView textView5;
        public RvHolder(@NonNull View itemView) {
            super(itemView);
            textView1 = itemView.findViewById(R.id.tv1);
            textView2 = itemView.findViewById(R.id.tv2);
            textView3 = itemView.findViewById(R.id.tv3);
            textView4 = itemView.findViewById(R.id.tv4);
            textView5 = itemView.findViewById(R.id.tv5);
        }
    }

}

主类代码

// An highlighted block
package bw.com.unit12;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import java.io.InputStream;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import bw.com.unit1.R;

public class Main12Activity extends AppCompatActivity {
    private RecyclerView recyclerView;
    private ArrayList<BookBean> bookBeans;
    private RvAdapter rvAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main12);

        recyclerView = findViewById(R.id.domRv);
        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(manager);

        dom();

        rvAdapter = new RvAdapter(this,bookBeans);
        recyclerView.setAdapter(rvAdapter);
        rvAdapter.notifyDataSetChanged();

        rvAdapter.setRvInterface(new RvInterface() {
            @Override
            public void onClick(int i) {

            }
        });

    }

    private void dom() {
        bookBeans = new ArrayList<>();
        DocumentBuilderFactory factory =DocumentBuilderFactory.newInstance();
        try {
            DocumentBuilder builder = factory.newDocumentBuilder();
            InputStream is = getAssets().open("book.xml");
            Document document = builder.parse(is);

            NodeList list = document.getElementsByTagName("book");

            for (int i = 0;i<list.getLength();i++){
                Node node = list.item(i);
                BookBean bookBean = new BookBean();

                String id = node.getAttributes().getNamedItem("id").getNodeValue();
                bookBean.setId(id);

                NodeList childNodes = node.getChildNodes();

                for (int j = 0;j<childNodes.getLength();j++){
                    Node node1 = childNodes.item(j);
                    if ("name".equals(node1.getNodeName())){
                        bookBean.setName(node1.getTextContent());
                    } else if ("author".equals(node1.getNodeName())){
                        bookBean.setAuthor(node1.getTextContent());
                    }else if("year".equals(node1.getNodeName())){
                        bookBean.setYear(node1.getTextContent());
                    }else if("price".equals(node1.getNodeName())){
                        bookBean.setPrice(node1.getTextContent());
                    }
                }
                bookBeans.add(bookBean);
            }

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

高德地图

效果图

在这里插入图片描述

主类代码

// An highlighted block
package bw.com.mygaode;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.maps2d.AMap;
import com.amap.api.maps2d.CameraUpdate;
import com.amap.api.maps2d.CameraUpdateFactory;
import com.amap.api.maps2d.MapView;
import com.amap.api.maps2d.model.BitmapDescriptorFactory;
import com.amap.api.maps2d.model.LatLng;
import com.amap.api.maps2d.model.MarkerOptions;

public class MainActivity extends AppCompatActivity {
    private MapView mapView;
    private AMap    aMap;
    private AMapLocationClient client;//负责定位的类

    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            client.stopLocation();//关闭定位
        }
    };

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

        mapView = findViewById(R.id.map);
        mapView.onCreate(savedInstanceState);//必须要写
        initmap();

    }

    public void initmap(){
        aMap = mapView.getMap();//获得地图对象
        aMap.setMapType(AMap.MAP_TYPE_NORMAL);//设置地图样式(普通、卫星)

        client = new AMapLocationClient(MainActivity.this);//定位(参数上下文对象)
        client.setLocationListener(locationListener);//给定位加监听

        AMapLocationClientOption option = new AMapLocationClientOption();
        option.setInterval(1000);//每隔1000毫秒去定位一次
        option.setNeedAddress(true);//是否显示地址
        option.setMockEnable(true);//是否显示模拟定位
        option.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);//高精度定位

        client.setLocationOption(option);
        client.startLocation();//启动定位
//        client.stopLocation();//关闭定位

//        handler.sendEmptyMessageDelayed(0,5000);//延迟发送,5秒过后

    }
    //定位发生改变,要获得当前的位置
    public AMapLocationListener locationListener = new AMapLocationListener() {
        @Override
        public void onLocationChanged(AMapLocation aMapLocation) {
            //定位成功
            if(aMapLocation.getErrorCode() == 0){
                double latitude = aMapLocation.getLatitude();//纬度
                double longitude = aMapLocation.getLongitude();//经度
                LatLng latLng = new LatLng(latitude,longitude);//坐标对象
                aMap.clear();//清空当前地图定位
                MarkerOptions options = new MarkerOptions();//小蓝点,蓝色定位标记
                options.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE));
                options.position(latLng);//定位图标要显示的位置
                options.draggable(true);

                aMap.addMarker(options);//给地图添加标记

                CameraUpdate update = CameraUpdateFactory.changeLatLng(latLng);//更新定位范围
                aMap.moveCamera(update);//显示到定位区域
            }else{
                //定位失败
                Toast.makeText(MainActivity.this, "定位失败!", Toast.LENGTH_SHORT).show();
            }
        }
    };


    //界面无焦点,但可见
    @Override
    protected void onResume() {
        super.onResume();
        mapView.onResume();
    }

    //界面失去焦点并不可见
    @Override
    protected void onPause() {
        super.onPause();
        mapView.onPause();
    }

    //界面销毁
    @Override
    protected void onDestroy() {
        super.onDestroy();
        mapView.onDestroy();
        client.stopLocation();//关闭定位
    }
}

权限

// An highlighted block
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="bw.com.mygaode">
    <!--允许程序打开网络套接字-->
    <uses-permission android:name="android.permission.INTERNET" />
    <!--允许程序设置内置sd卡的写权限-->
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <!--允许程序获取网络状态-->
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <!--允许程序访问WiFi网络信息-->
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <!--允许程序读写手机状态和身份-->
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <!--允许程序访问CellID或WiFi热点来获取粗略的位置-->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <meta-data
            android:name="com.amap.api.v2.apikey"
            android:value="1a662dbda3db746ca04956e3a863ab96"
            ></meta-data>
        <service android:name="com.amap.api.location.APSService"></service>

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>



    </application>

</manifest>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值