如何在二级布局中获取整数json值(回收器视图的结尾)

时间:2016-12-21 09:09:11

标签: android json android-recyclerview

这是我的json:

{
  "status": "ok",
  "count": 10,
  "count_total": 65,
  "pages": 7,
  "posts": [
    {
      "id": 3115,
      "type": "post",
.
.
.
}]}

希望获取我的回收站视图的页面值结束: enter image description here

这是我的AsyncTask函数来获取json:

 if (jsonStr != null) {
                    try {

                        JSONObject jsonObj = new JSONObject(jsonStr);


                        int id=jsonObj.getInt("pages");


                        JSONArray posts = jsonObj.getJSONArray("posts");
                        for (int i = 0; i < posts.length(); i++) {
                            JSONObject c = posts.getJSONObject(i);

                            jsonContent jsonContent=new jsonContent();

                            jsonContent.title=c.getString("title");

                            //img
                           JSONObject post_img=c.getJSONObject("thumbnail_images");
                            for (int j=0;j<post_img.length();j++)
                            {
                                JSONObject v=post_img.getJSONObject("mom-portfolio-two");
                                jsonContent.imgurl=v.getString("url");
                            }
                            jsonContent.pages=id;
                            listcontent.add(jsonContent);

                        }

这是我的适配器:

  public class DataAdapter extends RecyclerView.Adapter<DataAdapter.ViewHolder> {
        private Context context;
        List<jsonContent> jcontent;



        public DataAdapter(Context context,List<jsonContent> jcontent) {

            this.context=context;
            this.jcontent=jcontent;

        }



        @Override
        public DataAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {

           View view ;
            if(i == R.layout.card_row) {
                view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.card_row, viewGroup, false);
            }else {
                view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.button_card, viewGroup, false);
            }
            return new ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(final DataAdapter.ViewHolder viewHolder,int i) {


            if(i == jcontent.size()) {
                viewHolder.buttonnext.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Toast.makeText(context, "next", Toast.LENGTH_SHORT).show();
                    }
                });
                viewHolder.buttonprev.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(context, "pre", Toast.LENGTH_SHORT).show();
                    }
                });

                viewHolder.cardView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                    }
                });

                viewHolder.pages.setText(Integer.toString(jcontent.get(i).pages));

            }
            else {

                viewHolder.title.setText(jcontent.get(i).title);

                Picasso.with(context).load(jcontent.get(i).imgurl).resize(300, 400).into(viewHolder.imageView);

            }
        }

        @Override
        public int getItemCount() {
                return jcontent.size()+1;
        }

        @Override
        public int getItemViewType(int position) {
            return (position == jcontent.size()) ? R.layout.button_card : R.layout.card_row;
        }


        public class ViewHolder extends RecyclerView.ViewHolder{

            private TextView title,pages;

            private ImageView imageView;

            private Button buttonnext,buttonprev;

            private CardView cardView;


            public ViewHolder(final View view) {
                super(view);


               title = (TextView)view.findViewById(R.id.post_title);
                imageView=(ImageView)view.findViewById(R.id.img);
                buttonnext =(Button)view.findViewById(R.id.next);
                buttonprev=(Button)view.findViewById(R.id.prev);
                cardView=(CardView)view.findViewById(R.id.cvv);

                pages=(TextView)view.findViewById(R.id.number_pages);
    }
    }
    }

但是当启动应用程序并向下滚动到回收站视图结束时,到达回收站应用程序崩溃结束时出现此错误:

 FATAL EXCEPTION: main
java.lang.IndexOutOfBoundsException: Invalid index 10, size is 10

从这一行:

 viewHolder.pages.setText(Integer.toString(jcontent.get(i).pages));

和pages是我的文本视图,用于显示页面在回收站视图的二级布局结束中的值

2 个答案:

答案 0 :(得分:2)

我找到了代码中的代码段

    @Override
    public int getItemCount() {
            return jcontent.size()+1;
    }

将其更改为

    @Override
    public int getItemCount() {
            return jcontent.size();
    }

它的beacouse列表从 0 开始,而非 1

如果你试图从10个大小的列表中获得 10 。它将抛出数组超出绑定的异常。

你应该得到9来获得第10个元素。

if(i == jcontent.size()) {
            viewHolder.buttonnext.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Toast.makeText(context, "next", Toast.LENGTH_SHORT).show();
                }
            });
            viewHolder.buttonprev.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(context, "pre", Toast.LENGTH_SHORT).show();
                }
            });

            viewHolder.cardView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                }
            });

            viewHolder.pages.setText(Integer.toString(jcontent.get(i).pages));

        }
        else {

            viewHolder.title.setText(jcontent.get(i).title);

            Picasso.with(context).load(jcontent.get(i).imgurl).resize(300, 400).into(viewHolder.imageView);

        }
    }

在此代码中你得到 jcontent.get(i)这会产生问题

编辑2

对于每个回收商项似乎是常量,然后从主JSON对象中提取它并作为单独的东西传递,或者从任何其他对象中获取

喜欢

  if(jcontent.size()>0){
      pages =jcontent.get(0).pages;
      viewHolder.pages.setText
  }

由于

答案 1 :(得分:0)

你的List jcontent size是10(0到9) 在onBindViewHolder中检查这一行是

S1: 99,78,67,60,75
S2: 66,76,56,70,80
S3: 89,76,81,70,90