public class Frag1 extends Fragment {
private View view;
private ListView listView;
private String dataUrl = "http://mock.eoapi.cn/success/cjTiF8H7R6Dw4fmRsuUS3H7ZPaVUJzRW"; private SQLiteDatabase database;
private List> list;
@Nullable @Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment, null);
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState);
initView();
//判断网络连接
boolean bo= NetWorkUtils.isWifi(getActivity());
if(bo){
getData();
}else{
final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("通过移动数据访问?");
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
builder.create().dismiss();
}
});
builder.setPositiveButton("继续", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//通过隐式意图开启
Intent intent= null;
if(Build.VERSION.SDK_INT>10){
intent = new Intent(Settings.ACTION_WIRELESS_SETTINGS);
}else{
intent = new Intent();
intent.setClassName("com.android.settings","com.android.settings.WirelessSettings");
intent.setAction("android.intent.action.VIEW");
} startActivity(intent);
} });
builder.create().show();
} DBHelper helper = new DBHelper(getActivity());
database = helper.getWritableDatabase();
}
private void getData() {
MyTask task = new MyTask();
task.execute();
}
//异步任务 private class MyTask extends AsyncTask{
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... params) {
HttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(dataUrl);
try {
HttpResponse response = client.execute(httpGet);
if (response.getStatusLine().getStatusCode() == 200) {
publishProgress(10);
InputStream inputStream = response.getEntity().getContent();
JsonUtils utils = new JsonUtils();
String stream = utils.stream(inputStream);
return stream;
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Gson gson = new Gson();
Info info = gson.fromJson(s, Info.class);
list = info.getData().get(1).getBody();
//向数据库中添加数据
for (int i = 0; i < list.size(); i++) {
String sql = "insert into mytable(url,detectionItem,categoryTitle) values(?,?,?)";
database.execSQL(sql, new Object[]{list.get(0).get(i).getUrl(), list.get(0).get(i).getDetectionItem(), list.get(0).get(i).getCategoryTitle()});
}
MyAdapter adapter = new MyAdapter();
listView.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
}
private void initView() {
listView = (ListView) view.findViewById(R.id.listView);
}
private class MyAdapter extends BaseAdapter {
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = View.inflate(getActivity(), R.layout.list_item, null);
holder = new ViewHolder();
holder.image_title = (ImageView) convertView.findViewById(R.id.image_title);
holder.text_categoryTitle = (TextView) convertView.findViewById(R.id.text_categoryTitle);
holder.text_detectionItem = (TextView) convertView.findViewById(R.id.text_detectionItem);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
ImageLoader.getInstance().displayImage(list.get(0).get(position).getUrl(), holder.image_title, options(R.mipmap.ic_launcher));
holder.text_categoryTitle.setText(list.get(0).get(position).getCategoryTitle());
holder.text_detectionItem.setText(list.get(0).get(position).getDetectionItem());
return convertView;
}
class ViewHolder {
ImageView image_title;
TextView text_categoryTitle;
TextView text_detectionItem;
}
}
private DisplayImageOptions options(int imageId) {
DisplayImageOptions options = new DisplayImageOptions.Builder()
.cacheOnDisk(true)
.cacheInMemory(true)
.showImageForEmptyUri(imageId)
.showImageOnLoading(imageId).build();
return options;
}
}