【问题标题】:Displaying parsed data (using Volley) in RecyclerView在 RecyclerView 中显示解析的数据(使用 Volley)
【发布时间】:2016-08-01 06:40:42
【问题描述】:

我在我的 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;
}

卡片视图布局:

<?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>

【问题讨论】:

  • 问题出在您的布局文件中。显示它

标签: android json android-recyclerview android-volley


【解决方案1】:

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

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

【讨论】:

  • 谢谢,我试试看!你介意看看我编辑的答案吗?我已经在那里发布了布局文件。
  • 我在你的布局中看不到卡片视图。
  • 您的相对布局中的视图可能没有定位。使用 ide 编辑器或使用线性布局代替
  • 抱歉,没有正确输入代码。它应该在那里。对,将尝试您提到的线性布局。
猜你喜欢
  • 2019-09-29
  • 2021-04-13
  • 1970-01-01
  • 1970-01-01
  • 2018-05-13
  • 1970-01-01
  • 2019-01-09
  • 1970-01-01
  • 2020-01-09
相关资源
最近更新 更多