android_判断网络/添加Fragment显示listview

权限:

 


   
 

 

 

 

 

布局控件:

activity_main:  写fragment时  除了要写name外 还要写id 不然会报错因为id是fragment唯一标识

 




    


fragment:

 

 




    


MainActivity:
Fragment:
 import android.app.Activity; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.os.AsyncTask; import android.os.Bundle; import android.provider.Settings; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ListView; import android.widget.TextView; import com.google.gson.Gson; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.util.ArrayList; import java.util.List; /**  * author:Created by on 2017/9/14.  */  public class BeaseFragmrnt extends Fragment { private List lists; private View view; private ListView lv; private StringBuilder builder; private List> result; ArrayList list = new ArrayList<>(); @Nullable  @Override  public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { view = inflater.inflate(R.layout.fragment, container, false); return view; } @Override  public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); //初始化控件  lv = (ListView) view.findViewById(R.id.lv); //获取判断网络返回值  boolean b = isNetworkConnected(getActivity()); Log.e("gsyfdgvsydhfbsjh", b + ""); //判断是否连接网络  if (b) { //如果返回true则加载数据  new MAsticy().execute("https://suggest.taobao.com/sug?code=utf-8&q=%E6%98%BE%E7%A4%BA%E5%99%A8"); } else { //否则调用此方法进行下一步操作  checkNetwork(getActivity()); } } public class MAsticy extends AsyncTask { @Override  protected String doInBackground(String... strings) { try { //获取路径  URL url = new URL(strings[0]); //获取 HttpURLConnection对象  HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); //获取请求码  int responseCode = urlConnection.getResponseCode(); //判断请求码  if (responseCode == 200) { //将路径转换字节流获取  InputStream inputStream = urlConnection.getInputStream(); //使用bufferReader读取  BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); //拼接字符串  builder = new StringBuilder(); //定义变量  String str = ""; //循环读取  while ((str = bufferedReader.readLine()) != null) { //拼接  builder.append(str); } } //返回  return builder.toString(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } @Override  protected void onPreExecute() { super.onPreExecute(); } @Override  protected void onPostExecute(String s) { //创建gson对象  Gson gson = new Gson(); //解析gson串  GsonBean gsonBean = gson.fromJson(s, GsonBean.class); //获取数据  result = gsonBean.getResult(); //遍历集合  for (int i = 0; i < result.size(); i++) { lists = result.get(i); //添加到新集合  list.addAll(lists); } //设置适配器  lv.setAdapter(new MAdapter()); super.onPostExecute(s); } @Override  protected void onProgressUpdate(Void... values) { super.onProgressUpdate(values); } } private class MAdapter extends BaseAdapter { @Override  public int getCount() { return list.size(); } @Override  public Object getItem(int i) { return list.get(i); } @Override  public long getItemId(int i) { return i; } @Override  public View getView(int i, View view, ViewGroup viewGroup) { ViewHolder holder; String s = list.get(i); if (view == null) { view = View.inflate(getActivity(), android.R.layout.simple_list_item_1, null); holder = new ViewHolder(); holder.text1 = (TextView) view.findViewById(android.R.id.text1); view.setTag(holder); } else { holder = (ViewHolder) view.getTag(); } holder.text1.setText(s); return view; } class ViewHolder { TextView text1; } } //////////////////////////////////////  //创建网络请求方法  public boolean isNetworkConnected(Context context) { if (context != null) { ConnectivityManager mConnectivityManager = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo(); if (mNetworkInfo != null) { return mNetworkInfo.isAvailable(); } } return false; } /////////////////////////////////////// // 如果没有网络,则弹出网络设置对话框  public void checkNetwork(final Activity activity) { if (!isNetworkConnected(activity)) { TextView msg = new TextView(activity); msg.setText("当前没有可以使用的网络,请设置网络!"); AlertDialog.Builder builder = new AlertDialog.Builder(activity); builder.setTitle("网络状态提示") .setView(msg) .setPositiveButton("确定", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { // 跳转到设置界面  activity.startActivityForResult(new Intent( Settings.ACTION_WIRELESS_SETTINGS), 0); } }).setNegativeButton("取消", null).create().show(); } return ; } } 

 

 

 

 

 

 

 

你可能感兴趣的:(android_判断网络/添加Fragment显示listview)