public class One_fragment extends Fragment {
private ListView lv;
String urlString = "http://api.expoon.com/AppNews/getNewsList/type/1/p/1";
ArrayList<JsonBean.DataBean>list=new ArrayList<JsonBean.DataBean>();
private MAdapter mAdapter;
private dao d;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v1 = inflater.inflate(R.layout.one_fra, null);
lv = v1.findViewById(R.id.lv);
mAdapter = new MAdapter(list,getActivity());
lv.setAdapter(mAdapter);
d = new dao(getActivity());
new MAsyncTask().execute(urlString);
return v1;
}
class MAsyncTask extends AsyncTask<String,Void,String>{
@Override
protected String doInBackground(String... strings) {
String json = Gjb.getJson(strings[0]);
return json;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Gson gson=new Gson();
JsonBean jsonBean = gson.fromJson(s, JsonBean.class);
List<JsonBean.DataBean> data = jsonBean.getData();
//dao层添加方法
for (int i=0;i<data.size();i++){
ContentValues values=new ContentValues();
values.put("news_title",data.get(i).getNews_title());
values.put("news_title",data.get(i).getNews_title());
d.add("login",null,values);
}
//一定要转换集合,使用addAll方法;
list.addAll(jsonBean.getData());
mAdapter.notifyDataSetChanged();
}
}
}