您的位置:首页 > 移动开发 > Android开发

android studio app 前端获取json数据以对象的形式来解析展示

2017-11-20 17:58 671 查看
        一般正常情况下使用http网络请求得到的后台数据为json格式,例如:得到json数据为:data = {"id":"11","title":"标题","name":"小明"}。

//先写好对象bean文件

public class DataBean{

public String id;

public String title;

public String name;

public String getId() {

    return id;
}

public void setId(String id) {

    this.id = id;
}

String getTitle() {

    return title;
}

public void setTitle(String title) {

    this.title = title;
}

public String getName() {

    return name;

}

public void setName(String name) {

    this.name = name;

}

}

//获取得到json数据,并进行json解析处理

JSONObject  object = new JSONObject(data);

   Gson gson = new Gson();

    new DataBean() = gson.fromJson(String.valueOf(object),DataBean.class);

   adapter.update(obdDataBean);//刷新数据

//如果是单个数据就直接进行赋值,如果是gridview数据,需要借助adapter进行处理

public class MyObdAdapter2 extends BaseAdapter {

    DecimalFormat df = new DecimalFormat("#.00");

    private Context context;

    private ObdDataBean obdDataBean;

    public MyObdAdapter2(Context context, ObdDataBean obdDataBean) {

        this.context = context;

        this.obdDataBean = obdDataBean;

    }

    @Override

    public int getCount() {

        return 16;//声明自己定死了只能是16条数据

    }

    public void update(ObdDataBean obdDataBean) {

        this.obdDataBean = null;

        this.obdDataBean = obdDataBean;

        this.notifyDataSetChanged();

    }

    @Override

    public Object getItem(int position) {

        return position;

    }

    @Override

    public long getItemId(int position) {

        return position;

    }

    @Override

    public View getView(int position, View convertView, ViewGroup parent) {

        MyViiewHolder holder = null;

        if (convertView == null) {

            holder = new MyViiewHolder();

            //把vlist layout转换成View【LayoutInflater的作用】

       

            holder.id = (TextView) convertView.findViewById(R.id.id);

            holder.title = (TextView) convertView.findViewById(R.id.title);

            holder.name = (TextView) convertView.findViewById(R.id.name);

            

            convertView.setTag(holder);

        } else {

            holder = (MyViiewHolder) convertView.getTag();

        }

        

            holder.id.setText(obdDataBean.getId() + "");

            holder.title.setText(obdDataBean.getTitle() + "");

            holder.name.setText(obdDataBean.getName() + "");

         

           

        return convertView;

    }

    class MyViiewHolder {

        private TextView id

        private TextView title

        private TextView name

       

    }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  json gridview gson adapter
相关文章推荐