使用Volley

时间:2016-02-15 20:29:16

标签: java android android-recyclerview android-volley

我真的陷入了我的问题。我有一个回收站视图,但我的适配器没有填充它。请帮忙完成我的任务。这是代码:

SeminarsFragment.java

public class SeminarsFragment extends Fragment {

RecyclerView recyclerView;
private List<Seminar> seminarList = new ArrayList<>();
private SeminarAdapter adapter;
private Seminar s;
ProgressDialog progressDialog;
FrameLayout fabLayout;
private  String seminarsUrl = "************************";

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_seminars, container, false);
    recyclerView = (RecyclerView) rootView.findViewById(R.id.recycler_view1);
    fabLayout = (FrameLayout) rootView.findViewById(R.id.layoutInner);
    fabLayout.setVisibility(View.GONE);

    recyclerView.setHasFixedSize(true);
    LinearLayoutManager llm = new LinearLayoutManager(getActivity());
    llm.setOrientation(LinearLayoutManager.VERTICAL);
    recyclerView.setLayoutManager(llm);

    progressDialog = new ProgressDialog(getActivity());
    progressDialog.setMessage("Loading...");
    progressDialog.show();

    JsonArrayRequest request = new JsonArrayRequest(seminarsUrl,
            new Response.Listener<JSONArray>(){
                @Override
                public void onResponse(JSONArray jsonArray) {
                    hidePDialog();
                    for(int i=0; i<jsonArray.length(); i++){
                        try{
                            String id,seminarName, bannerUrl;
                            JSONObject jsonObject = jsonArray.getJSONObject(i);
                            Seminar seminar;
                            id = jsonObject.getString("sid");
                            seminarName = jsonObject.getString("seminar_title");
                            bannerUrl = jsonObject.getString("seminar_banner");

                            seminar = new Seminar(id, seminarName, bannerUrl);
                            seminarList.add(seminar);

                            Log.d("Active seminar", jsonArray.toString());
                            Log.i("sid", id);
                            Log.i("seminar name", seminarName);
                            Log.i("bannerUrl", bannerUrl);

                        }catch (Exception e){

                        }
                    }
                    adapter.notifyDataSetChanged();
                }
            }, new Response.ErrorListener(){
        @Override
        public void onErrorResponse(VolleyError volleyError) {
            Activity activity = getActivity();
            if(activity != null && isAdded())
                hidePDialog();
            if (volleyError instanceof NoConnectionError) {
                String errormsg = "Check your internet connection";
                Toast.makeText(activity, errormsg, Toast.LENGTH_LONG).show();
            }
        }
    });

    Log.d("padung AppController", "padung appController");

    AppController.getInstance().addToRequestQueue(request);
    adapter = new SeminarAdapter(getActivity(),seminarList);
    recyclerView.setAdapter(adapter);


    return rootView;
}

private void hidePDialog() {
    if (progressDialog != null) {
        progressDialog.dismiss();
        progressDialog = null;
    }
}

}

SeminarAdapter.java

公共类SeminarAdapter扩展了RecyclerView.Adapter {

private List<Seminar> seminars;
private Context context;
private ImageLoader imageLoader = AppController.getInstance().getImageLoader();

public SeminarAdapter(Context context, List<Seminar> seminars){
    this.context = context;
    this.seminars = seminars;
}

@Override
public int getItemCount() {
    return 0;
}

@Override
public void onBindViewHolder(SeminarAdapter.SeminarViewHolder holder, final int position) {

    Log.e("onBindViewHolder", "naabot ko diri sa onbind");
    Seminar s =  seminars.get(position);

    holder.banner.setImageUrl(s.getBannerUrl(), imageLoader);
    holder.seminarName.setText(s.getSeminarName());


}

@Override
public SeminarViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {

    Log.e("onCreateViewHolder", "naabot ko diri sa oncreateviewholder");

    View itemView = LayoutInflater.
            from(viewGroup.getContext()).
            inflate(R.layout.seminar_item, viewGroup, false);

    imageLoader = AppController.getInstance().getImageLoader();

    itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(context, "SHIT", Toast.LENGTH_LONG);
        }
    });
    return new SeminarViewHolder(itemView, context, seminars);
}


public static class SeminarViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

//        public ImageView banner;
    public TextView name;
    public TextView seminarName;
    public Seminar seminar;
    public NetworkImageView banner;

    List<Seminar> seminars = new ArrayList<>();
    Context context;

    public SeminarViewHolder(View v, Context context, List<Seminar> seminars){
        super(v);
        this.seminars = seminars;
        this.context = context;
        v.setOnClickListener(this);

        seminarName = (TextView) v.findViewById(R.id.seminar_name);
        banner = (NetworkImageView) v.findViewById(R.id.banner);
    }


    @Override
    public void onClick(View v) {
        int position = getAdapterPosition();
        Seminar s = this.seminars.get(position);
        Bitmap bitmap = s.getBanner();
        Toast.makeText(context,s.getSeminarName(), Toast.LENGTH_LONG).show();
    }
}

public List<Seminar> getSeminarsList() {
    return seminars;
}

}

0 个答案:

没有答案