Android学习笔记day_02-----保存数据
程序员文章站
2022-03-14 19:02:38
...
一、xml文件解析
1.以下面天气weather.xml解析为例
<?xml version="1.0" encoding="utf-8"?>
<infos>
<city id="1">
<temp>20℃/30℃</temp>
<weather>晴天多云</weather>
<name>上海</name>
<pm>80</pm>
<wind>1级</wind>
</city>
<city id="2">
<temp>26℃/32℃</temp>
<weather>晴天</weather>
<name>北京</name>
<pm>98</pm>
<wind>3级</wind>
</city>
<city id="3">
<temp>15℃/24℃</temp>
<weather>多云</weather>
<name>重庆</name>
<pm>30</pm>
<wind>5级</wind>
</city>
</infos>
2.创建bean的类WeatherInfo,并且生成get、set方法**
public class WeatherInfo {
private int id;
private String name;
private String weather;
private String temp;
private String pm;
private String wind;
}
3.重点是工具类WeatherServer的编写,工具类中设计到的是解析器和对应xml的解析功能
public class WeatherServer {
public static List<WeatherInfo> getWeatherInfo(InputStream is) throws Exception {
// 得到解析器
XmlPullParser parser = Xml.newPullParser();
// 初始化解析器,第一个参数代表包含xml的数据
parser.setInput(is, "utf-8");
List<WeatherInfo> weatherInfoList = null;
WeatherInfo weatherInfo = null;
// 得到当前事件类型
int type = parser.getEventType();
// 文档结束标签
while (type != XmlPullParser.END_DOCUMENT) {
switch (type) {
// 是开始标签的话
case XmlPullParser.START_TAG:
if ("infos".equals(parser.getName())) {
weatherInfoList = new ArrayList<WeatherInfo>();
} else if ("city".equals(parser.getName())) {
weatherInfo = new WeatherInfo();
String idString = parser.getAttributeValue(0);
weatherInfo.setId(Integer.parseInt(idString));
} else if ("temp".equals(parser.getName())) {
// 获取节点中的内容
String temp = parser.nextText();
weatherInfo.setTemp(temp);
} else if ("weather".equals(parser.getName())) {
// 获取节点中的内容
String weather = parser.nextText();
weatherInfo.setWeather(weather);
} else if ("name".equals(parser.getName())) {
// 获取节点中的内容
String name = parser.nextText();
weatherInfo.setName(name);
} else if ("pm".equals(parser.getName())) {
// 获取节点中的内容
String pm = parser.nextText();
weatherInfo.setPm(pm);
} else if ("wind".equals(parser.getName())) {
// 获取节点中的内容
String wind = parser.nextText();
weatherInfo.setWind(wind);
}
break;
case XmlPullParser.END_TAG:
// 一个城市的信息处理完
if ("city".equals(parser.getName())) {
weatherInfoList.add(weatherInfo);
weatherInfo = null;
}
break;
default:
break;
}
//解析完一个,指定到下一个
type = parser.next();
}
return weatherInfoList;
}
}
4.最后一步是在actiity.xml中解析到布局文件中去
public class MainActivity extends Activity implements OnClickListener {
private TextView select_city, select_weather, select_temp, select_wind, select_pm;
private Map<String, String> map;
private List<Map<String, String>> list;
private String temp, weather, name, pm, wind;
private ImageView icon;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 初始化文本控件
select_city = findViewById(R.id.select_city);
select_weather = findViewById(R.id.select_weather);
select_temp = findViewById(R.id.temp);
select_wind = findViewById(R.id.wind);
select_pm = findViewById(R.id.pm);
icon = findViewById(R.id.icon);
findViewById(R.id.city_sh).setOnClickListener(this);
findViewById(R.id.city_bj).setOnClickListener(this);
findViewById(R.id.city_cq).setOnClickListener(this);
try {
List<WeatherInfo> infos = WeatherServer
.getWeatherInfo(MainActivity.class.getClassLoader()
.getResourceAsStream("weather.xml"));
// 循环读取infos中的每一条数据
list = new ArrayList<Map<String, String>>();
for (WeatherInfo info : infos) {
map = new HashMap<String, String>();
map.put("temp", info.getTemp());
map.put("weather", info.getWeather());
map.put("name", info.getName());
map.put("pm", info.getPm());
map.put("wind", info.getWind());
list.add(map);
}
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(this, "解析失败!", 0).show();
}
//默认显示的天气
getMap(1, R.drawable.sun);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.city_bj:
getMap(0, R.drawable.cloud_sun);
break;
case R.id.city_sh:
getMap(1, R.drawable.sun);
break;
case R.id.city_cq:
getMap(2, R.drawable.clouds);
break;
default:
break;
}
}
//获取数据并且设置到页面上面
private void getMap(int number, int iconNumber) {
Map<String, String> bjMap = list.get(number);
temp = bjMap.get("temp");
weather = bjMap.get("weather");
name = bjMap.get("name");
pm = bjMap.get("pm");
wind = bjMap.get("wind");
select_city.setText(name);
select_weather.setText(weather);
select_temp.setText("" + temp);
select_wind.setText("风力 : " + wind);
select_pm.setText("pm: " + pm);
icon.setImageResource(iconNumber);
}
}
遇到的问题:
在Android中经常遇到的问题就是当把程序部署到模拟器上的时候会提示:程序停止运行,然后直接退出
在这个程序中是因为:MainActivity中的
List<WeatherInfo> infos = WeatherServer
.getWeatherInfo(MainActivity.class.getClassLoader()
.getResourceAsStream("weather.xml"));
中的weather.xml后面的xml没有写
最后加上效果图,布局文件代码没有贴出来
二、SharedPreferences将数据储存到xml文件中
1.案例如下,保存到名为data的文件中
/**
* 保存QQ号与密码到data.xml中
* @param context
* @param number
* @param password
* @return
*/
public static boolean saveUserInfo(Context context,String number,String password){
SharedPreferences sharedPreferences = context.getSharedPreferences("data", Context.MODE_PRIVATE);//第二个参数是文件操作模式
Editor editor = sharedPreferences.edit();
editor.putString("username", number);
editor.putString("pwd", password);
editor.commit();
return true;
}