最近学习中需要将APP中的参数保存到JSON中,还需要读取Json中的内容显示到界面中的ListView中,看了很多大牛的博客,磕磕巴巴的只写出了读取Json中的内容到listView中,后续参数保存模块成功的话,会继续更新。
1. 首先,我们需要在src/main中创建assets的文件夹,与Java、res处于同级目录。创建json文件xxxx.json,我创建的名字为station.json,且内容如下:
2. 由于Listview需要适配器,我是在之前的基础上改动的,并没有改动适配器中的内容,不过为了清晰明了也贴一下适配器中的内容
public class DataAdapter extends BaseAdapter {
List<Item> object = null;
Context context = null;
public DataAdapter(Context context,List<Item> object){
this.context = context;
this.object = object;
}
@Override
public int getCount() {
return object.size();
}
@Override
public Object getItem(int position) {
return object.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
Item item = (Item) getItem(position);
View view = LayoutInflater.from(context).inflate(R.layout.table_body, null);
TextView id = (TextView)view.findViewById(R.id.next_id);
TextView mac = (TextView)view.findViewById(R.id.next_mac);
TextView ip = (TextView)view.findViewById(R.id.next_ip);
id.setText(item.getId());
mac.setText(item.getMac());
ip.setText(item.getIp());
return view;
}
}
3. 接下来需要创建一个工具类,用来获取Json文件中的数据,并将Json转换为String类型
public static String getJson(Context context, String fileName) {
// 将json数据变成字符串
StringBuilder stringBuilder = new StringBuilder();
// 获得assets资源管理器
AssetManager assetManager = context.getAssets();
// 使用IO流读取json文件内容
try {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(
assetManager.open(fileName), "utf-8"));
String line;
while ((line = bufferedReader.readLine()) != null) {
stringBuilder.append(line.trim());
}
} catch (IOException e) {
e.printStackTrace();
}
return stringBuilder.toString();
}
4. mainActivity中加载Adapter的内容如下:
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dhcp_enter);
try {
initData();
} catch (JSONException e) {
e.printStackTrace();
}
DataAdapter dataAdapter = new DataAdapter(DhcpCenter.this, dataList);
ListView lv = (ListView)findViewById(R.id.list);
lv.setAdapter(dataAdapter);
5. 可以看出,从上面的内容中用到了initdate()方法,没有改动之前的initdate方法代码:
Item id = new Item("1","192.168.36.139",“AA:BB:CC:aa:bb:cc”);
dataList.add(id);
这样的内容是死的,现在需要将Json中的数据读到这里,就不能这么写了,下面是改动后的内容:
private void initData() throws JSONException {
//根据工具类中的getJson方法获取
String strJsondata = cacheUtils.getJson(DhcpCenter.this,"station.json");
JSONObject jsonObject = new JSONObject(strJsondata);
//根据activity获取array
JSONArray resultArray = jsonObject.getJSONArray("activity");
for (int i=0;i<resultArray.length();i++){
object = resultArray.getJSONObject(i);
try {
String id = object.getString("id");
d(TAG, "id: "+id);
String ip = object.getString("ip");
d(TAG, "ip: "+ip);
String mac = object.getString("mac");
d(TAG, "mac: "+mac);
Item item = new Item(id,ip,mac);
dataList.add(item);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
这样,json中的数据就可以显示到ListView中了,由于功能没有完善,目前只有读出Json中的内容。