数据解析练习
数据解析
要求:txt数据存放assets目录中中,通过数据流读取txt文件中的信息,并利用Gson工具实现Json解析获取清晰数据显示到ListView上,每一行可仅显示部分信息。
一 .首先创建一个项目,在创建一个适配器
,一个封装数据的Bean类
导入Gson依赖库
解析assets目录下的数据:
需要把数据放在assets文件夹下
MainActivity中代码------------
···
package com.example.myapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import com.google.gson.Gson;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
public class MainActivity extends AppCompatActivity {
ListView listView;
private String data="";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init() {
listView= (ListView) findViewById(R.id.listView);
try {
InputStream is = getAssets().open("datas.txt");
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
byte buffer[] =new byte[1024];
int len=-1;
while ((len=is.read(buffer))!=-1){
byteArrayOutputStream.write(buffer,0,len);
byteArrayOutputStream.flush();
}
byteArrayOutputStream.close();
data = byteArrayOutputStream.toString();
Gson gson = new Gson();
Bean bean = gson.fromJson(data, Bean.class);
listView.setAdapter(new MyAdapter(this,bean));
is.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
···
适配器的代码 如下:
···
package com.example.myapp;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
/**
- Created by lenovo on 2018/8/1.
*/
public class MyAdapter extends BaseAdapter {
Context context;
Bean bean;
private TextView item_1,item_2,item_3,item_4,item_5,item_6;
public MyAdapter(Context context,Bean bean){
this.context=context;
this.bean=bean;
}
@Override
public int getCount() {
return bean.getResult().size();
}
@Override
public Object getItem(int i) {
return bean.getResult().get(i);
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
view = View.inflate(context, R.layout.item, null);
item_1 = view.findViewById(R.id.item_1);
item_2 = view.findViewById(R.id.item_2);
item_3 = view.findViewById(R.id.item_3);
item_4= view.findViewById(R.id.item_4);
item_5 = view.findViewById(R.id.item_5);
item_6 = view.findViewById(R.id.item_6);
item_1.setText(bean.getResult().get(i).getAirline());
item_2.setText(bean.getResult().get(i).getDepCity());
item_3.setText(bean.getResult().get(i).getArrCity());
item_4.setText(bean.getResult().get(i).getFlightDate()+"------"
+bean.getResult().get(i).getDepTime());
item_5.setText(bean.getResult().get(i).getPEKDate()+"-------"
+bean.getResult().get(i).getArrTime());
item_6.setText(bean.getResult().get(i).getFlightNum());
return view;
}
}
···
main.xml 布局中有一个listView集合
···
<?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="com.example.myapp.MainActivity">
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"></ListView>
</LinearLayout>
···
效果图:
image.png