Android 在RecyclerView中显示解析数据(使用截取)
我在Android应用程序中显示解析数据时遇到了一些问题。基本上,我有两个API。第一个包含特定内容的ID。我已经解析了链接ID并将其存储为一个变量,以调用下一个API来获取一些特定的内容。数据已被解析,但我想在CardView中正确显示它,但它当前显示如下 绿色的单词应该就在它各自的标题下面。 这是我正在使用的代码。如果有人能帮我解决这个问题,我将不胜感激Android 在RecyclerView中显示解析数据(使用截取),android,json,android-recyclerview,android-volley,Android,Json,Android Recyclerview,Android Volley,我在Android应用程序中显示解析数据时遇到了一些问题。基本上,我有两个API。第一个包含特定内容的ID。我已经解析了链接ID并将其存储为一个变量,以调用下一个API来获取一些特定的内容。数据已被解析,但我想在CardView中正确显示它,但它当前显示如下 绿色的单词应该就在它各自的标题下面。 这是我正在使用的代码。如果有人能帮我解决这个问题,我将不胜感激 @Override public View onCreateView(LayoutInflater inflater, ViewGrou
@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;
}
@覆盖
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
最终视图rootView=充气机。充气(R.layout.fragment\u新闻提要,容器,false);
Context=getActivity();
cardwiew=(cardwiew)rootView.findviewbyd(R.id.cv);
rv=(RecyclerView)rootView.findviewbyd(R.id.rv);
mSwipe=(SwipeRefreshLayout)rootView.findviewbyd(R.id.SwipeRefreshLayout);
mSwipe.setColorSchemeColors(R.color.primary,R.color.accent);
mSwipe.setOnRefreshListener(新的SwipeRefreshLayout.OnRefreshListener(){
@凌驾
公共void onRefresh(){
刷新内容();
}
});
最终LinearLayoutManager llm=新的LinearLayoutManager(上下文);
rv.setLayoutManager(llm);
最终RVNewsAdapter=新RVNewsAdapter(新闻列表);
rv.设置适配器(适配器);
requestQueue=Volley.newRequestQueue(上下文);
字符串url=”http://api.jchui.me/minerva/?apikey=JCu8p8P0wqZhN8kH8F9lwdzZBOrtDl&phase=2b";
JsonArrayRequest firstRequest=newjsonarrayrequest(url,new Response.Listener()){
@凌驾
公共void onResponse(JSONArray响应){
试一试{
if(response.length()>0){
newsList.clear();
对于(int i=0;i0){
对于(int i=0;i
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>
让我假设并猜测,您将所有内容都放在
CardView
下,这是错误的,因为CardView
是FrameLayout
的一个实例,它将所有内容重叠在彼此之上。我的建议是将您的布局更改为类似
CardView>LinearLayout{(Vertical)[views within]}
问题出在布局文件中。给我看看谢谢,我来试试!你介意看看我编辑过的答案吗?我已经在那里发布了布局文件。我在你的布局中看不到cardview。你在relativelayout中的视图可能没有定位。使用ide编辑器或使用线性布局代替抱歉,没有正确地输入代码。它应该在那里。好的,我将尝试你提到的线性布局。