图片Gridview没有显示在fregment android

时间:2017-09-29 20:29:54

标签: java android android-gridview

我的图片网格视图没有显示,我正在使用Picasso通过使用从数据库获取的URL在网格上显示图像我试图在导航栏中的片段内显示它,当我向片段添加任何其他内容时它会显示它,问题是只有网格我没有得到任何错误,虽然你能帮助请求 这是我的fragment.java代码

public class event extends Fragment {
int userid ;
String START ;
private String GET_IMAGES_URL = 
"http://192.168.1.107/android/v1/getimages.php";
public static final String TAG_IMAGE_URL = "URL";
private GridView gridView;
private ArrayList<String> images;
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
private String mParam1;
private String mParam2;
private OnFragmentInteractionListener mListener;
public event() {
      }
 public static event newInstance(String param1, String param2) {
    event fragment = new event();
    Bundle args = new Bundle();
    args.putString(ARG_PARAM1, param1);
    args.putString(ARG_PARAM2, param2);
    fragment.setArguments(args);
    return fragment;
 }

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
   if (getArguments() != null) {
        mParam1 = getArguments().getString(ARG_PARAM1);
        mParam2 = getArguments().getString(ARG_PARAM2);
    }
    userid = SharedPrefManager.getInstance(getActivity()).getUserid();
    images = new ArrayList<>();
    START = "";
 }

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_event, container, false);
    gridView = (GridView) view.findViewById(R.id.gridview);
    gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v,
                                int position, long id) {
            Toast.makeText(getActivity(), "" + position,
                    Toast.LENGTH_SHORT).show();
        }
    });
    getData();
    return view;
  }
  public void onButtonPressed(Uri uri) {
    if (mListener != null) {
        mListener.onFragmentInteraction(uri);
    }
  }
  @Override
  public void onAttach(Context context) {
    super.onAttach(context);
    if (context instanceof OnFragmentInteractionListener) {
        mListener = (OnFragmentInteractionListener) context;
    } else {
   }
  }
  @Override
  public void onDetach() {
    super.onDetach();
    mListener = null;
  }
  public interface OnFragmentInteractionListener {
    // TODO: Update argument type and name
    void onFragmentInteraction(Uri uri);
  }
  private void getData() {
    final ProgressDialog loading = ProgressDialog.show(getActivity(), 
   "Please wait...", "Fetching data...", false, false);
    StringRequest stringRequest = new StringRequest(
            Request.Method.POST, GET_IMAGES_URL,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                   try {
                       JSONArray array = new JSONArray(response);
                            loading.dismiss();
                            showGrid(array);
                    } catch (JSONException e) {

                        loading.dismiss();
                        e.printStackTrace();
                    }
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    loading.dismiss();

                }
            }
      ){
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String, String> params = new HashMap<>();
            params.put("userid", String.valueOf(userid));

            return params;
        }

     };;


    RequestQueue requestQueue = Volley.newRequestQueue(getActivity());


    requestQueue.add(stringRequest);
   }
    private void showGrid(JSONArray array){

    for(int i = 0; i<array.length(); i++){

        JSONObject obj = null;
        try {

            images.add(START + array.getString(i)+START);




        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    gridView.setAdapter(new 
     GridViewAdapter(getActivity().getApplicationContext(),images));
   }

    }

我的适配器类是这个

  public class GridViewAdapter extends BaseAdapter {
  private final Context context;
  ArrayList<String>  urls;
  // private  String[] url ;
  public GridViewAdapter(Context context, ArrayList<String> images) {
    this.context = context;
     urls =images;
   }

   @Override public View getView(int position, View convertView, ViewGroup 
   parent) {
    ImageView view = (ImageView) convertView;
    if (view == null) {
        view = new ImageView(context);

        view.setLayoutParams(new GridView.LayoutParams(85, 85));
        view.setScaleType(ImageView.ScaleType.CENTER_CROP);
        view.setPadding(8, 8, 8, 8);
    }
    else {
        view = (ImageView) convertView;
    }

    // Get the image URL for the current position.
    String url = getItem(position);
    //

    // Trigger the download of the URL asynchronously into the image view.
    Picasso.with(context) //
            .load(url) //
            .fit() //
            .tag(context) //
            .into(view);


     return view;
     }

    @Override public int getCount() {
    return urls.size();
    } 

   @Override public String getItem(int position) {
    return urls.get(position);
   }

    @Override public long getItemId(int position) {
    return position;
    }

    }

1 个答案:

答案 0 :(得分:0)

Picasso仅在您的清单中具有Internet权限时才有效。