如何显示从我的Web服务到回收站视图的详细信息

时间:2015-10-15 09:41:40

标签: android android-recyclerview android-volley recycler-adapter

您好我尝试从我的网络服务到回收者视图显示一些细节,我可以将除图像之外的其他细节带到我的recyclerview.Pls帮助我

我的片段是:

     public class MainActivityFragment extends Fragment {
      private static final String TAG = "RecyclerViewExample";
      private final List<Person> mPersonList = new ArrayList<>();
      public MainActivityFragment() {
      }

      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_main, container, false);

        Context context = getActivity();


        RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
        recyclerView.setHasFixedSize(true);

        LinearLayoutManager layoutManager = new LinearLayoutManager(context);
        recyclerView.setLayoutManager(layoutManager);

        final RVAdapter rvAdapter = new RVAdapter(mPersonList);
        recyclerView.setAdapter(rvAdapter);

        RequestQueue requestQueue = Volley.newRequestQueue(context);
        String url = "http://onam.leah.in/new/item_details.php";

        JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(url, new    Response.Listener<JSONArray>() {
            @Override
            public void onResponse(JSONArray response) {
                try {


                        for (int i = 0; i < response.length(); i++) {
                            Log.d(TAG, response.toString());
                            JSONObject jsonObject = response.getJSONObject(i);



                            String phone_name = jsonObject.getString("Phone");


                         String phone_price = jsonObject.getString("Price");
                            String thumbnail=jsonObject.getString("Image");

                            Person person=new Person(phone_name,phone_price,thumbnail);

                            mPersonList.add(i, person);
                        }
                        rvAdapter.notifyDataSetChanged();

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e("VOLLEY", error.toString());
            }
        });

        requestQueue.add(jsonArrayRequest);


        return view;
    }
}

我的班级是

 class Person {
        public String phone_name;
        public String phone_price;
        public String thumbnail;

         Person(String phone_name, String phone_price,String thumbnail) {
           this.phone_name = phone_name;
           this.phone_price = phone_price;
           this.thumbnail=thumbnail;

       }
       }

适配器类

public class RVAdapter extends
    RecyclerView.Adapter<RVAdapter.PersonViewHolder> {
        List<Person> persons; //    private Context mContext;
        private ImageLoader mImageLoader;

        RVAdapter(List<Person> persons) {
            this.persons = persons;
        }

        @Override
        public PersonViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview,
    parent, false);
            PersonViewHolder pvh = new PersonViewHolder(v);
            return pvh;
        }

        @Override
        public void onBindViewHolder(PersonViewHolder holder, int position) {

            mImageLoader = AppController.getInstance().getImageLoader(); //       
    holder.thumbnail.setImageUrl(persons.get(position).thumbnail,
    mImageLoader);
            holder.phoneName.setText(persons.get(position).phone_name);
            holder.phonePrice.setText(persons.get(position).phone_price);
            if (mImageLoader == null)
                mImageLoader = AppController.getInstance().getImageLoader();
            holder.thumbnail.setImageUrl(persons.get(position).thumbnail,
    mImageLoader);
        }

        @Override
        public int getItemCount() {
            if (persons != null) {
                return persons.size();
            }
            return 0;
        }

        @Override
        public void onAttachedToRecyclerView(RecyclerView recyclerView) {
            super.onAttachedToRecyclerView(recyclerView);
        }

        public static class PersonViewHolder extends RecyclerView.ViewHolder {
            CardView cv;
            TextView phoneName;
            TextView phonePrice;
          NetworkImageView thumbnail;

            PersonViewHolder(View itemView) {
                super(itemView);
                cv = (CardView) itemView.findViewById(R.id.card_view);
                phoneName = (TextView) itemView.findViewById(R.id.phone_name);
                phonePrice = (TextView) itemView.findViewById(R.id.phone_price);
                thumbnail=(NetworkImageView) itemView.findViewById(R.id.thumbnail);
            }
        }

当我运行应用程序时出现错误:

 java.lang.NullPointerException: Attempt to invoke virtual method 'com.android.volley.toolbox.ImageLoader harico.mylastrecycle.AppController.getImageLoader()' on a null object reference
            at harico.mylastrecycle.RVAdapter.onBindViewHolder(RVAdapter.java:39)
            at harico.mylastrecycle.RVAdapter.onBindViewHolder(RVAdapter.java:20)
            at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:5138)
            at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4433)
            at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4326)
            at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1955)
            at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1364)
            at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1327)

1 个答案:

答案 0 :(得分:0)

首先,不要在任何时候使用Volley.newRequestQueue()进行新的调用。每次使用时都会添加一个新的RequestQueue,这将降低app的性能。相反使用一个RequestQueue并添加所有请求队列如下:

  AppController.getInstance().addToRequestQueue(jsonArrayRequest , "SOITEM");

将图像添加到Recycler View的最佳和最简单的方法是使用Picasso库。 (要么) 使用方法:

  imageLoader = MySingleton.getInstance().getImageLoader(getActivity());


 NetworkImageView niv_networkimageview = (NetworkImageView)convertView.findViewById(R.id.niv_networkimageview);

            niv_networkimageview.setImageUrl(al_ImageUrls.get(position), imageLoader);
            niv_networkimageview.setPadding(5, 0, 5, 0);

在getView()方法中编写上面的代码。 还可以在MySingleton中创建一个ImageLoader类,如下所示:

 public RequestQueue getRequestQueue(Context context){
    mRequestQueue = Volley.newRequestQueue(context);
    return this.mRequestQueue;
}

public ImageLoader getImageLoader(Context context){

    mImageLoader = new ImageLoader(getRequestQueue(context), new ImageLoader.ImageCache() {
        private final LruCache<String, Bitmap> mCache = new LruCache<String, Bitmap>(10);
        public void putBitmap(String url, Bitmap bitmap) {
            mCache.put(url, bitmap);
        }
        public Bitmap getBitmap(String url) {
            return mCache.get(url);
        }
    });
    return this.mImageLoader;
}