Java 错误:如何将cardview插入回收器片段布局
大家好,我目前正在研究如何在Android中解析JSON并使用RecyclerView和CardView显示。我不明白为什么cardview没有显示在recycler视图布局上。以前有人遇到过这个问题吗?谢谢 news\u layout.xml(片段布局)Java 错误:如何将cardview插入回收器片段布局,java,android,json,android-studio,android-fragments,Java,Android,Json,Android Studio,Android Fragments,大家好,我目前正在研究如何在Android中解析JSON并使用RecyclerView和CardView显示。我不明白为什么cardview没有显示在recycler视图布局上。以前有人遇到过这个问题吗?谢谢 news\u layout.xml(片段布局) <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/and
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical" 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:scrollbars="vertical"
android:id="@+id/recyclerview"
android:clickable="true"
android:focusable="true"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
数据库结构
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical" 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:scrollbars="vertical"
android:id="@+id/recyclerview"
android:clickable="true"
android:focusable="true"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
{
"server_response": [
{
"news_id": "25",
"path_image": "http://localhost/ibmcoe_la/images/lonesky.jpg",
"news_title": "lonesky.jpg",
"news_description": "testttttt",
"news_timepost": "17.03.01 15:34:53"
},
{
"news_id": "23",
"path_image": "http://localhost/ibmcoe_la/images/ump-ios-01.jpg",
"news_title": "ump-ios-01.jpg",
"news_description": "5455454",
"news_timepost": "17.02.22 15:24:07"
},
{
"news_id": "22",
"path_image": "http://localhost/ibmcoe_la/images/fccc9dc81940317b7881c2be7f5c94c4.jpg",
"news_title": "fccc9dc81940317b7881c2be7f5c94c4.jpg",
"news_description": "21",
"news_timepost": "17.02.22 15:23:10"
},
{
"news_id": "19",
"path_image": "http://localhost/ibmcoe_la/images/logo-rasmi-ump-(logo-sahaja).png",
"news_title": "logo-rasmi-ump-(logo-sahaja).png",
"news_description": "qqqq",
"news_timepost": "17.02.22 15:10:04"
},
{
"news_id": "5",
"path_image": "http://localhost/ibmcoe_la/images/Koala.jpg",
"news_title": "Koala.jpg",
"news_description": "e",
"news_timepost": "17.02.22 10:40:32"
},
{
"news_id": "4",
"path_image": "http://localhost/ibmcoe_la/images/Tulips.jpg",
"news_title": "Tulips.jpg",
"news_description": "c",
"news_timepost": "17.02.22 10:22:54"
},
{
"news_id": "2",
"path_image": "http://localhost/ibmcoe_la/images/Chrysanthemum.jpg",
"news_title": "Chrysanthemum.jpg",
"news_description": "d",
"news_timepost": "17.02.22 10:16:23"
},
{
"news_id": "18",
"path_image": "http://localhost/ibmcoe_la/images/fccc9dc81940317b7881c2be7f5c94c4.jpg",
"news_title": "fccc9dc81940317b7881c2be7f5c94c4.jpg",
"news_description": "q",
"news_timepost": "17.02.22 03:08:28"
},
{
"news_id": "17",
"path_image": "http://localhost/ibmcoe_la/images/galaxy-pictures-6.jpg",
"news_title": "galaxy-pictures-6.jpg",
"news_description": "1",
"news_timepost": "17.02.22 02:39:56"
},
{
"news_id": "16",
"path_image": "http://localhost/ibmcoe_la/images/PicIBM.png",
"news_title": "PicIBM.png",
"news_description": "2",
"news_timepost": "17.02.22 02:37:56"
},
{
"news_id": "15",
"path_image": "http://localhost/ibmcoe_la/images/PicIBM.png",
"news_title": "PicIBM.png",
"news_description": "2",
"news_timepost": "17.02.22 02:37:45"
},
{
"news_id": "14",
"path_image": "http://localhost/ibmcoe_la/images/PicIBM.png",
"news_title": "PicIBM.png",
"news_description": "2",
"news_timepost": "17.02.22 02:33:38"
},
{
"news_id": "13",
"path_image": "http://localhost/ibmcoe_la/images/testin123.jpg",
"news_title": "testin123.jpg",
"news_description": "1",
"news_timepost": "17.02.22 02:30:08"
},
{
"news_id": "1",
"path_image": "http://localhost/ibmcoe_la/images/Penguins.jpg",
"news_title": "Penguins.jpg",
"news_description": "testing",
"news_timepost": "17.02.21 04:49:32"
}
]
}
我想你可以这样试试:
JSONObject jsonObject= new JSONObject(response.body().string());
JSONArray array = jsonObject.getJSONArray("server_response");
for(int i=0; i<array.length(); i++)
{
JSONObject object = array.getJSONObject(i);
listnews data = new listnews(object.getInt("news_id"),object.getString("path_image"),object.getString("news_image")
,object.getString("news_title"),object.getString("news_description")
);
data_list.add(data);
}
JSONObject-JSONObject=newjsonobject(response.body().string());
JSONArray数组=jsonObject.getJSONArray(“服务器响应”);
对于(int i=0;i尝试此操作,尝试在CardView上方插入一个线性布局,并在CardView上放置一些高程,以查看它是否会出现:
<LinearLayout
xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:android2="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:card_view="https://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:orienatation="vertical"
android2:layout_height="wrap_content"
>
<android.support.v7.widget.CardView
android:id="@+id/card_view"
android:layout_width="match_parent"
android2:layout_marginTop="5dp"
android2:layout_marginLeft="5dp"
android2:layout_marginRight="5dp"
android2:layout_gravity="center|top"
card_view:cardPreventCornerOverlap="false"
card_view:cardCornerRadius="20dp"
app:elevation="5dp"
android2:layout_width="match_parent"
android2:layout_height="wrap_content">
<FrameLayout
android2:layout_width="match_parent"
android2:layout_height="400dp"
app:cardElevation="0dp"
android2:background="@drawable/cardviewstring">
<LinearLayout
android2:orientation="vertical"
android2:layout_width="380dp"
android2:layout_height="match_parent"
android2:weightSum="1"
android2:layout_marginRight="20dp">
<LinearLayout
android2:orientation="vertical"
android2:layout_width="match_parent"
android2:layout_weight="1"
android2:layout_height="250dp">
<ImageView
android2:layout_width="match_parent"
android2:layout_height="match_parent"
app:srcCompat="@mipmap/ic_launcher"
android2:id="@+id/imageView4" />
</LinearLayout>
<LinearLayout
android2:orientation="vertical"
android2:layout_width="match_parent"
android2:layout_height="wrap_content"
android2:paddingTop="25dp">
<ScrollView
android2:layout_width="match_parent"
android2:layout_height="84dp"
android2:background="@drawable/screen_background_dark_transparent"
android2:layout_marginLeft="3dp">
<LinearLayout
android2:layout_width="match_parent"
android2:layout_height="wrap_content"
android2:orientation="vertical" >
<TextView
android2:text="TextView"
android2:layout_width="match_parent"
android2:layout_height="wrap_content"
android2:id="@+id/textView4" />
<TextView
android2:text="TextView"
android2:layout_width="match_parent"
android2:layout_height="35dp"
android2:id="@+id/textView3" />
</LinearLayout>
</ScrollView>
</LinearLayout>
<LinearLayout
android2:orientation="vertical"
android2:layout_marginTop="10dp"
android2:layout_width="match_parent"
android2:layout_height="42dp"
android:layout_alignParentBottom="true">
<LinearLayout
android2:orientation="horizontal"
android2:layout_width="match_parent"
android2:layout_height="match_parent">
<ImageView
android2:layout_width="wrap_content"
android2:layout_height="wrap_content"
app:srcCompat="@drawable/ic_share"
android2:id="@+id/imageView3"
android2:layout_weight="1" />
<ImageView
android2:layout_width="wrap_content"
android2:layout_height="wrap_content"
app:srcCompat="@drawable/ic_like"
android2:id="@+id/imageView2"
android2:layout_weight="1" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</FrameLayout>
</android.support.v7.widget.CardView>
<LinearLayout/>
你的代码看起来很好。你检查了服务器端的数据了吗?有行吗。@LuongDinh这里,我想pb正在解析JSOn,你能这样试试吗:`JSONObject JSONObject=new JSONObject(test);JSONObject.getJSONArray(“服务器\响应”)`我认为您正在将响应解析为JSON数组,而它是JSON对象。请尝试记录您的响应jsonarray@LuongDinh先生,我可以收到您的电子邮件或联系方式吗?我目前有此错误。错误:(239,45)错误:找不到适合JSONObject(int)构造函数JSONObject的构造函数。JSONObject(Map)不适用(参数不匹配;int无法转换为Map)构造函数JSONObject.JSONObject(JSONTokener)不适用(参数不匹配;int无法转换为JSONTokener)构造函数JSONObject.JSONObject(字符串)不适用(参数不匹配;int无法转换为字符串)
**localhost Server Response (get JSON)**
{
"server_response": [
{
"news_id": "25",
"path_image": "http://localhost/ibmcoe_la/images/lonesky.jpg",
"news_title": "lonesky.jpg",
"news_description": "testttttt",
"news_timepost": "17.03.01 15:34:53"
},
{
"news_id": "23",
"path_image": "http://localhost/ibmcoe_la/images/ump-ios-01.jpg",
"news_title": "ump-ios-01.jpg",
"news_description": "5455454",
"news_timepost": "17.02.22 15:24:07"
},
{
"news_id": "22",
"path_image": "http://localhost/ibmcoe_la/images/fccc9dc81940317b7881c2be7f5c94c4.jpg",
"news_title": "fccc9dc81940317b7881c2be7f5c94c4.jpg",
"news_description": "21",
"news_timepost": "17.02.22 15:23:10"
},
{
"news_id": "19",
"path_image": "http://localhost/ibmcoe_la/images/logo-rasmi-ump-(logo-sahaja).png",
"news_title": "logo-rasmi-ump-(logo-sahaja).png",
"news_description": "qqqq",
"news_timepost": "17.02.22 15:10:04"
},
{
"news_id": "5",
"path_image": "http://localhost/ibmcoe_la/images/Koala.jpg",
"news_title": "Koala.jpg",
"news_description": "e",
"news_timepost": "17.02.22 10:40:32"
},
{
"news_id": "4",
"path_image": "http://localhost/ibmcoe_la/images/Tulips.jpg",
"news_title": "Tulips.jpg",
"news_description": "c",
"news_timepost": "17.02.22 10:22:54"
},
{
"news_id": "2",
"path_image": "http://localhost/ibmcoe_la/images/Chrysanthemum.jpg",
"news_title": "Chrysanthemum.jpg",
"news_description": "d",
"news_timepost": "17.02.22 10:16:23"
},
{
"news_id": "18",
"path_image": "http://localhost/ibmcoe_la/images/fccc9dc81940317b7881c2be7f5c94c4.jpg",
"news_title": "fccc9dc81940317b7881c2be7f5c94c4.jpg",
"news_description": "q",
"news_timepost": "17.02.22 03:08:28"
},
{
"news_id": "17",
"path_image": "http://localhost/ibmcoe_la/images/galaxy-pictures-6.jpg",
"news_title": "galaxy-pictures-6.jpg",
"news_description": "1",
"news_timepost": "17.02.22 02:39:56"
},
{
"news_id": "16",
"path_image": "http://localhost/ibmcoe_la/images/PicIBM.png",
"news_title": "PicIBM.png",
"news_description": "2",
"news_timepost": "17.02.22 02:37:56"
},
{
"news_id": "15",
"path_image": "http://localhost/ibmcoe_la/images/PicIBM.png",
"news_title": "PicIBM.png",
"news_description": "2",
"news_timepost": "17.02.22 02:37:45"
},
{
"news_id": "14",
"path_image": "http://localhost/ibmcoe_la/images/PicIBM.png",
"news_title": "PicIBM.png",
"news_description": "2",
"news_timepost": "17.02.22 02:33:38"
},
{
"news_id": "13",
"path_image": "http://localhost/ibmcoe_la/images/testin123.jpg",
"news_title": "testin123.jpg",
"news_description": "1",
"news_timepost": "17.02.22 02:30:08"
},
{
"news_id": "1",
"path_image": "http://localhost/ibmcoe_la/images/Penguins.jpg",
"news_title": "Penguins.jpg",
"news_description": "testing",
"news_timepost": "17.02.21 04:49:32"
}
]
}
JSONObject jsonObject= new JSONObject(response.body().string());
JSONArray array = jsonObject.getJSONArray("server_response");
for(int i=0; i<array.length(); i++)
{
JSONObject object = array.getJSONObject(i);
listnews data = new listnews(object.getInt("news_id"),object.getString("path_image"),object.getString("news_image")
,object.getString("news_title"),object.getString("news_description")
);
data_list.add(data);
}
<LinearLayout
xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:android2="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:card_view="https://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:orienatation="vertical"
android2:layout_height="wrap_content"
>
<android.support.v7.widget.CardView
android:id="@+id/card_view"
android:layout_width="match_parent"
android2:layout_marginTop="5dp"
android2:layout_marginLeft="5dp"
android2:layout_marginRight="5dp"
android2:layout_gravity="center|top"
card_view:cardPreventCornerOverlap="false"
card_view:cardCornerRadius="20dp"
app:elevation="5dp"
android2:layout_width="match_parent"
android2:layout_height="wrap_content">
<FrameLayout
android2:layout_width="match_parent"
android2:layout_height="400dp"
app:cardElevation="0dp"
android2:background="@drawable/cardviewstring">
<LinearLayout
android2:orientation="vertical"
android2:layout_width="380dp"
android2:layout_height="match_parent"
android2:weightSum="1"
android2:layout_marginRight="20dp">
<LinearLayout
android2:orientation="vertical"
android2:layout_width="match_parent"
android2:layout_weight="1"
android2:layout_height="250dp">
<ImageView
android2:layout_width="match_parent"
android2:layout_height="match_parent"
app:srcCompat="@mipmap/ic_launcher"
android2:id="@+id/imageView4" />
</LinearLayout>
<LinearLayout
android2:orientation="vertical"
android2:layout_width="match_parent"
android2:layout_height="wrap_content"
android2:paddingTop="25dp">
<ScrollView
android2:layout_width="match_parent"
android2:layout_height="84dp"
android2:background="@drawable/screen_background_dark_transparent"
android2:layout_marginLeft="3dp">
<LinearLayout
android2:layout_width="match_parent"
android2:layout_height="wrap_content"
android2:orientation="vertical" >
<TextView
android2:text="TextView"
android2:layout_width="match_parent"
android2:layout_height="wrap_content"
android2:id="@+id/textView4" />
<TextView
android2:text="TextView"
android2:layout_width="match_parent"
android2:layout_height="35dp"
android2:id="@+id/textView3" />
</LinearLayout>
</ScrollView>
</LinearLayout>
<LinearLayout
android2:orientation="vertical"
android2:layout_marginTop="10dp"
android2:layout_width="match_parent"
android2:layout_height="42dp"
android:layout_alignParentBottom="true">
<LinearLayout
android2:orientation="horizontal"
android2:layout_width="match_parent"
android2:layout_height="match_parent">
<ImageView
android2:layout_width="wrap_content"
android2:layout_height="wrap_content"
app:srcCompat="@drawable/ic_share"
android2:id="@+id/imageView3"
android2:layout_weight="1" />
<ImageView
android2:layout_width="wrap_content"
android2:layout_height="wrap_content"
app:srcCompat="@drawable/ic_like"
android2:id="@+id/imageView2"
android2:layout_weight="1" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</FrameLayout>
</android.support.v7.widget.CardView>
<LinearLayout/>