没有得到JsonArray数据

时间:2016-03-07 07:38:16

标签: java android json android-recyclerview android-cardview

    FeedItem item=new FeedItem();
    protected void parseResult(JSONObject result) {
        try {
            JSONObject jsonObject = result.getJSONObject("category");
            //Iterate the jsonArray and print the brand_name
            JSONArray jsonArray1 = jsonObject.getJSONArray("brand_title");
            for (int i = 0; i < jsonArray1.length(); i++) {
                item.setTitle1(jsonArray1.getString(i));
                feedsList.add(item);System.out.println("dsc==dc=dsc=ds=c=ds======" + item.getTitle1());
            }
JSONArray jsonArray2 = jsonObject.getJSONArray("address");
                    for(int i=0; i < jsonArray2.length(); i++) {
                       FeedItem item2=new FeedItem();
                        item2.setTitle2(jsonArray2.getString(i));
                        feedsList.add(item2);
                     // System.out.println("dsc==dc=dsc=ds=c=ds======" + item2.getTitle2());
                    }

我正在获取上述两个println语句中所需的数据。 getTitle1()&amp; getTtile2()是FeedItem类的方法,如下所示:

    public class FeedItem {
        ArrayList<String> arr=new ArrayList<>();
        public String getTitle1()
        {   String s = "";
            for(int i=0;i<arr.size();i++){
                s=arr.get(i);
        }
            return s;
        }
        public void setTitle1(String title)
        {
            arr.add(title);
        }
public String getTitle2()
    {   String s1 = "";
        for(int i=0;i<arr_address.size();i++){
            s1=arr_address.get(i);
        }
        return s1;
    }
    public void setTitle2(String adres)
    {
        //   arr= new ArrayList<>();
        arr_address.add(adres);
    }

然后执行onPostExecute()方法并为recycler视图设置适配器。 MyRecyclerAdapter类是回收器的适配器,如下所示:

    public class MyRecyclerAdapter extends RecyclerView.Adapter<MyRecyclerAdapter.ViewHolder> {

        private List<FeedItem> feedItemList;
        FeedItem feed;

        public MyRecyclerAdapter() {
          feedItemList=new ArrayList<>();
          feed= new FeedItem();
              feedItemList.add(feed);
        }

        public MyRecyclerAdapter(List<FeedItem> list){
            this.feedItemList=list;
        }

        @Override
                public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
                View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.content_salon, viewGroup, false);
                ViewHolder viewHolder=new ViewHolder(view);
                return viewHolder;
            }

        @Override
            public void onBindViewHolder(ViewHolder holder, int i) {

                feed = feedItemList.get(i);
                 holder.brandName.setText(feed.getTitle1());
                 holder.brandAddress.setText(feed.getTitle2());
                 //  System.out.println("======jjjj===="+feed.getTitle2());
               }


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

        public class ViewHolder extends RecyclerView.ViewHolder {
            public ImageView brandImage;
            public TextView brandName;
            public TextView brandAddress;

            public ViewHolder(View itemView) {
                super(itemView);


brandAddress = (TextView) itemView.findViewById(R.id.person_age);
            brandName = (TextView) itemView.findViewById(R.id.person_name);
      }
        }
      }

问题是onBindViewHolder方法()中的System.out.println语句只打印,但我需要在各个卡中显示数组的每个元素。我同时获取“brandName”但不是“brandAddress”的数据。那么,任何人都能建议或纠正一些对我有用的东西吗?

1 个答案:

答案 0 :(得分:1)

你应该在每个中为lop迭代创建一个新项目,否则它也将覆盖以前的值

 for (int i = 0; i < jsonArray1.length(); i++) {
        item = new FeedItem();
        item.setTitle1(jsonArray1.getString(i));
        feedsList.add(item);System.out.println("dsc==dc=dsc=ds=c=ds======" + item.getTitle1());
 }