在RecyclerView中显示已解析的数据(使用Volley)

时间:2016-08-01 06:40:43

标签: android json android-recyclerview android-volley

我在Android应用中显示解析数据时遇到了一些麻烦。基本上,我有两个API。第一个包含特定内容的ID。我已经解析并存储链接ID作为变量来调用下一个API以获取一些特定内容。数据已经过解析,但我想在CardView中正确显示,但目前显示如下。

绿色的单词应该低于它各自的标题。

这是我正在使用的代码。我会感谢任何帮助以某种方式解决这个问题。

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    final View rootView = inflater.inflate(R.layout.fragment_newsfeed, container, false);


    Context context = getActivity();


    cardView = (CardView) rootView.findViewById(R.id.cv);
    rv = (RecyclerView) rootView.findViewById(R.id.rv);


    mSwipe = (SwipeRefreshLayout) rootView.findViewById(R.id.swipeRefreshLayout);
    mSwipe.setColorSchemeColors(R.color.primary, R.color.accent);
    mSwipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {


        @Override
        public void onRefresh() {


            refreshContent();
        }
    });


    final LinearLayoutManager llm = new LinearLayoutManager(context);
    rv.setLayoutManager(llm);


    final RVNewsAdapter adapter = new RVNewsAdapter(newsList);
    rv.setAdapter(adapter);


    requestQueue = Volley.newRequestQueue(context);
    String url = "http://api.jchui.me/minerva/?apikey=JCu8p8P0wqZhN8kH8F9lwdzZBOrtDl&phase=2b";


    JsonArrayRequest firstRequest = new JsonArrayRequest(url, new Response.Listener<JSONArray>() {


        @Override
        public void onResponse(JSONArray response) {
            try {
                if (response.length() > 0) {
                    newsList.clear();
                    for (int i = 0; i < response.length(); i++) {
                        JSONObject jsonObject = response.getJSONObject(i);
                        News news = new News();


                        if (!jsonObject.isNull("title")) {
                            news.title = jsonObject.getString("title");


                        }
                        if (!jsonObject.isNull("date")) {
                            news.date = jsonObject.getString("date");


                        }
                        if (!jsonObject.isNull("link")) {
                            String link = news.link = jsonObject.getString("link");
                            String url1 = "https://jchui.xyz/athena/data/getNewsContent.php?id=" + link;




                            JsonArrayRequest secondRequest = new JsonArrayRequest(url1, new Response.Listener<JSONArray>() {


                                @Override
                                public void onResponse(JSONArray response) {
                                    try {
                                        if (response.length() > 0) {
                                            for (int i = 0; i < response.length(); i++) {
                                                JSONObject jsonObject1 = response.getJSONObject(i);
                                                News news = new News();
                                                if (!jsonObject1.isNull("content")) {
                                                    news.preview = jsonObject1.getString("content");
                                                }


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


                            });
                            requestQueue.add(secondRequest);




                }


                        newsList.add(i, news);
            }
                    adapter.notifyDataSetChanged();


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


    });


    requestQueue.add(firstRequest);




    return rootView;
}

CardView布局:

<?xml version="1.0" encoding="utf-8"?>

<android.support.v7.widget.CardView         xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/cv"
card_view:contentPaddingTop="15dp"
card_view:cardUseCompatPadding="true"
>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:id="@+id/heading"
        android:textSize="18sp"
        android:fontFamily="sans-serif-light"
        android:textColor="@color/primarytext"
        android:layout_centerVertical="true"
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/preview"
        android:fontFamily="sans-serif-light"
        android:textColor="@color/primarydark"
        android:layout_below="@+id/heading"
        android:layout_centerVertical="true"
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/date"
        android:fontFamily="sans-serif-light"
        android:textColor="@color/primarydark"
        android:layout_below="@+id/heading"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="275dp"
        android:layout_centerVertical="true"
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/minerva"
        android:layout_below="@+id/heading"
        android:layout_marginTop="10dp"
        android:layout_centerVertical="true"
        android:fontFamily="sans-serif-light"
        android:textStyle="italic"
        android:textColor="@color/secondarytext"
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/link"
        android:layout_below="@+id/date"
        android:fontFamily="sans-serif-light"
        android:textColor="@color/text"
        android:layout_centerVertical="true"
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/today"
        android:layout_below="@+id/heading"
        android:fontFamily="sans-serif-light"
        android:textColor="@color/secondarytext"
        android:layout_centerVertical="true"
        />


</RelativeLayout>

</android.support.v7.widget.CardView>

片段布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="athena.sentineljs.com.athena.NewsfeedFragment"
tools:showIn="@layout/activity_newsfeed">

<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipeRefreshLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">

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

</android.support.v4.widget.SwipeRefreshLayout>

1 个答案:

答案 0 :(得分:0)

让我假设并猜测您将CardView下的所有内容都归咎于错误,因为CardViewFrameLayout的一个实例,它将重叠所有内容。我的建议是将layout更改为

CardView>LinearLayout{(Vertical)[views within]}