2016-08-01 55 views
-1

我一直在我的Android應用程序中顯示解析數據時遇到了一些麻煩。基本上,我有兩個API。第一個包含特定內容的ID。我已經解析並存儲了鏈接ID作爲變量來調用下一個API以獲得一些特定的內容。數據已被解析,但我想在CardView中正確顯示,但目前顯示如下。在RecyclerView中顯示解析數據(使用Volley)

綠色的詞應該在它的相應標題下方。

這是我使用的代碼。我會很感激任何幫助,以解決這個問題。

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

+0

問題出在您的佈局文件中。顯示它 – Piyush

回答

0

讓我假設和猜測,你是下CardView這是錯誤的拋出一切,因爲CardView是的實例0,它將重疊所有東西。我的建議是將您的layout更改爲類似

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

謝謝,我會試試這個!你介意查看我編輯的答案嗎?我已經在那裏發佈了佈局文件。 –

+0

我在你的佈局中看不到cardview。 – k0sh

+0

您的意見是在相對佈局沒有定位可能。使用ide編輯器或使用線性佈局代替 – k0sh