RecyclerView没有填充项目

时间:2016-05-28 22:33:48

标签: android android-recyclerview

我尝试发送一个截击请求以填充recyclerviews但由于某种原因我可以理解,但是没有填充Recyclerview。数据提取得很好,我可以从logcat中看到。

加载后我只看到一个空白页,空白页和空白页。

这些是我的代码:

示例jsoup

{
  "found": 4,
  "site_ID": 1,
  "comments": [
    {
      "ID": 26934,
      "post": {
        "ID": 194784,
        "type": "post",
        "title": "Lorem Ipsum is simply dummy text",
      },
      "author": {
        "email": false,
        "avatar_URL": "http://1.gravatar.com/avatar/af61ad05da322fccae2bd02f7062e357?s=96&d=wavatar&r=g",
      },
      "date": "2016-05-28T02:54:35+01:00",
      "content": "<p>It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout</p>\n",
      "status": "approved",

    },
    {
          "ID": 26934,
          "post": {
            "ID": 194784,
            "type": "post",
            "title": "Contrary to popular belief, Lorem Ipsum",
          },
          "author": {
            "email": false,
            "avatar_URL": "http://1.gravatar.com/avatar/af61ad05da322fccae2bd02f7062e357?s=96&d=wavatar&r=g",
          },
          "date": "2016-05-28T02:54:35+01:00",
          "content": "<p>Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia</p>\n",
          "status": "approved",

    },
    {
          "ID": 26934,
          "post": {
            "ID": 194784,
            "type": "post",
            "title": "Lorem Ipsum is simply dummy text",
          },
          "author": {
            "email": false,
            "avatar_URL": "http://1.gravatar.com/avatar/af61ad05da322fccae2bd02f7062e357?s=96&d=wavatar&r=g",
          },
          "date": "2016-05-28T02:54:35+01:00",
          "content": "<p>It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout</p>\n",
          "status": "approved",

        },
        {
              "ID": 26934,
              "post": {
                "ID": 194784,
                "type": "post",
                "title": "Lorem ipsum dolor sit amet, consectetur adipiscing elit",
              },
              "author": {
                "email": false,
                "avatar_URL": "http://1.gravatar.com/avatar/af61ad05da322fccae2bd02f7062e357?s=96&d=wavatar&r=g",
              },
              "date": "2016-05-28T02:54:35+01:00",
              "content": "<p>Sed ut porttitor nunc. Cras scelerisque lobortis diam, nec placerat lacus aliquam eu. Ut a eros non libero porta commodo. Nulla odio lectus, vestibulum ut ultrices eget</p>\n",
              "status": "approved",

        },
  ]
}

CommentItem

public class CommentItem {
    private String comt_name;
    private String comt_if_auth;
    private String comt_cont;
    private String comt_timest;

    public String getComt_imageUrl() {
        return comt_image_url;
    }

    public void setComt_imageUrl(String comt_image) {
        this.comt_image_url = comt_image;
    }

    public String getComt_name() {
        return comt_name;
    }

    public void setComt_name(String comt_name) {
        this.comt_name = comt_name;
    }

    public String getComt_if_auth() {
        return comt_if_auth;
    }

    public void setComt_if_auth(String comt_if_auth) {
        this.comt_if_auth = comt_if_auth;
    }

    public String getComt_cont() {
        return comt_cont;
    }

    public void setComt_cont(String comt_cont) {
        this.comt_cont = comt_cont;
    }

    public String getComt_timest() {
        return comt_timest;
    }

    public void setComt_timest(String comt_timest) {
        this.comt_timest = comt_timest;
    }
}

CommentFragment

public class CommentFragment extends Fragment {

    private final String TAG = "CommentFragment";
    private ProgressBar mProgressBar;
    private TextView comtHeader;

    //Creating a list of comments
    private List<CommentItem> mCommentItems;

    //Creating views
    RecyclerView mRecyclerView;
    private RecyclerView.Adapter mAdapter;
    LinearLayoutManager mLayoutManager;


    private String comtUrl;
    public CommentFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        Log.d(TAG, "onCreate View called");
        comtUrl = getArguments().getString("commentUrl");
        Log.d(TAG, comtUrl);
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_comment, container, false);
        mRecyclerView = (RecyclerView) view.findViewById(R.id.comment_recyclerm);
        mProgressBar = (ProgressBar) view.findViewById(R.id.comt_prog);
        comtHeader = (TextView) view.findViewById(R.id.comt_head);

        mLayoutManager = new LinearLayoutManager(getActivity());
        mRecyclerView.setLayoutManager(mLayoutManager);

        loadComment();

        mCommentItems = new ArrayList<>();
        mRecyclerView.setAdapter(mAdapter);
        mAdapter = new CommentAdapter(mCommentItems, getActivity());

        return view;
    }

    private void loadComment() {
        Log.d(TAG, "loadComment called");
        mProgressBar.setVisibility(View.VISIBLE);

        JsonObjectRequest comments = new JsonObjectRequest(comtUrl, null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        Log.d(TAG, "onResponse for loadComment called");
                        parseComment(response);
                        if (mProgressBar != null) {
                            mProgressBar.setVisibility(View.GONE);
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        if (mProgressBar != null) {
                            mProgressBar.setVisibility(View.GONE);
                        }

                    }
                });
        int socketTimeOut = 10000;
        RetryPolicy policy = new DefaultRetryPolicy(socketTimeOut, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
        comments.setRetryPolicy(policy);

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

        requestQueue.add(comments);


    }

    public void parseComment(JSONObject object) {
        Log.d(TAG,  "Parsing comments");
        try {
            String found = object.getString("found");
            if (found.equals("1")) {
                comtHeader.setText(getString(R.string.comment, found));
            } else {
                comtHeader.setText(getString(R.string.comments, found));
            }

            JSONArray commentArray = object.getJSONArray("comments");

            for(int i = 0; i<commentArray.length(); i++) {
                CommentItem commentItem = new CommentItem();
                JSONObject jsonObject;
                try {
                    jsonObject = commentArray.getJSONObject(i);

                    JSONObject author = jsonObject.getJSONObject("author");
                    String name = author.getString("name");
                    commentItem.setComt_name(name);

                    commentItem.setComt_imageUrl(author.getString("avatar_URL"));

                    SimpleDateFormat formatDate = new SimpleDateFormat("dd MMM, yy", Locale.getDefault());
                    SimpleDateFormat formatTime = new SimpleDateFormat("HH:mm", Locale.getDefault());
                    SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
                    String inputDateStr = jsonObject.getString("date");
                    Log.d(TAG, "comment date is " + inputDateStr);
                    try {
                        Date inputDate = inputFormat.parse(inputDateStr);
                        String commDateStr = formatDate.format(inputDate);

                        String commTime = formatTime.format(inputDate);

                        commentItem.setComt_timest(String.format(getResources().getString(R.string.com_time_stamp), commDateStr, commTime));
                    } catch (ParseException e) {
                        Log.d(TAG, "Error in Parsing date");
                    }

                    String content = jsonObject.getString("content");
                    commentItem.setComt_cont(content);

                    mCommentItems.add(commentItem);

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

            }


        } catch (JSONException e) {
            e.printStackTrace();
            comtHeader.setText(R.string.comment_no);
        }

        mAdapter.notifyItemRangeChanged(0, mAdapter.getItemCount());
    }



}

CommentAdapter

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

    private ImageLoader mImageLoader;
    private Context sContext;

    //List of comments


    @Override
    public CommentAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.comment_item, parent, false);
        ViewHolder viewHolder = new ViewHolder(v);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        CommentItem commentItem = mCommentItems.get(position);

        mImageLoader = VolleyRequest.getInstance(sContext).getImageLoader();
        mImageLoader.get(commentItem.getComt_imageUrl(), ImageLoader.getImageListener(holder.mImageView, R.drawable.comt_image, R.drawable.comt_name_error));

        holder.mImageView.setImageUrl(commentItem.getComt_imageUrl(), mImageLoader);
        holder.comtName.setText(commentItem.getComt_name());
        holder.comtContent.setText(commentItem.getComt_cont());
        holder.comtTimeStamp.setText(commentItem.getComt_timest());


    }




    public class  ViewHolder extends RecyclerView.ViewHolder {
        public CircularNetworkImageView mImageView;
        public TextView comtName;
        public  TextView comtContent;
        public TextView comtTimeStamp;

        public ViewHolder(View view) {
            super(view);
            mImageView = (CircularNetworkImageView) view.findViewById(R.id.comt_img);
            comtName = (TextView) view.findViewById(R.id.comt_name);
            comtContent = (TextView) view.findViewById(R.id.comt_content);
            comtTimeStamp = (TextView) view.findViewById(R.id.comt_timestamp);


        }
    }

    private List<CommentItem> mCommentItems;

    public CommentAdapter(List<CommentItem> commentItems, Context context) {
        super();
        //Getting all comments
        this.mCommentItems = commentItems;
        this.sContext = context;
    }

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

comment_item

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:id="@+id/comt_item_recy"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

    <com.Abdullq.commenter.CircularNetworkImageView
        android:layout_width="40dp"
        android:layout_height="40dp"
        android.alignParentLeft="true"
        android:id="@+id/comt_img"
        android.scaleType="centerCrop"
        android:background="@drawable/round_button"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/comt_name"
        android:layout_toRightOf="@+id/comt_img"
        android:textStyle="bold"/>



    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/comt_content"
        android:layout_below="@+id/comt_name"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/comt_timestamp"
        android:textStyle="italic"
        android:layout_below="@+id/comt_content"/>

</RelativeLayout>

frament_comment

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:id="@+id/comment_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:id="@+id/comt_head"/>


    <ProgressBar
        style="?android:attr/progressBarStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/comt_prog"
        android:indeterminate="true"/>

    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/comment_recyclerm"/>
</LinearLayout>

最令人困惑的是,mProgressBarcomtHeader正在显示为什么是recyclerview,也不显示提取的项目。

拜托,你能告诉我哪里弄错了吗?

1 个答案:

答案 0 :(得分:1)

可能在这里:

   <div class='row'>
  <a href="/posts/3"><img id="imagen3" src="/system/posts/images/000/000/003/original/frankie-mannings-102nd-birthday-5160522641047552-hp.gif?1464448829" alt="Frankie mannings 102nd birthday 5160522641047552 hp" /></a>
  <p>caption</p>
</div>

在第一行之前移动第二行:) 在第二行mAdapter之后是新对象,所以附加到recyclerview一个指向旧的。