Android 看不到我的回收视图
我似乎无法连接适配器。我正在尝试创建一个RecyclerViewCardView。但是,它不会显示在模拟器上。我收到一条错误消息:Android 看不到我的回收视图,android,android-recyclerview,android-adapter,android-cardview,Android,Android Recyclerview,Android Adapter,Android Cardview,我似乎无法连接适配器。我正在尝试创建一个RecyclerViewCardView。但是,它不会显示在模拟器上。我收到一条错误消息: E/RecyclerView:未连接适配器;跳过布局 这里是我设置适配器的地方 madapter = new CardAdapter( itemList, CardviewRecycler.this ); recyclerView.setAdapter(madapter); public class CardviewRecycler ex
E/RecyclerView
:未连接适配器;跳过布局
这里是我设置适配器的地方
madapter = new CardAdapter( itemList, CardviewRecycler.this );
recyclerView.setAdapter(madapter);
public class CardviewRecycler extends AppCompatActivity {
// URL string to call data from myPHPAdmin localhost database.
private static final String URL_ITEMLIST = "http://192.168.240.1/ImageJsonData.php";
public RecyclerView recyclerView;
//private RecyclerView.Adapter adapter;
private CardAdapter madapter;
public List<ItemList> itemList;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cardviewrecycler);
// Initializing ItemList Array.
itemList = new ArrayList<>();
//Calling the Recyclerview
recyclerView = findViewById(R.id.recyclerview);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
//Loading the RecyclerView data.
loadItemListData();
}
public void loadItemListData(){
StringRequest stringRequest = new StringRequest(Request.Method.GET, URL_ITEMLIST,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
//converting the string to json array object
JSONArray array = new JSONArray(response);
//traversing through all the objects
for (int i = 0; i < array.length(); i++) {
//getting item object from json array
JSONObject itemobj = array.getJSONObject(i);
int id = itemobj.getInt("id");
String image_title = itemobj.getString("image_title");
String image_url = itemobj.getString("image_url");
ItemList item = new ItemList(id, image_title, image_url);
itemList.add(item);
}
//creating adapter object and setting it to recyclerview
madapter = new CardAdapter( itemList, CardviewRecycler.this );
recyclerView.setAdapter(madapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
//adding our stringrequest to queue
Volley.newRequestQueue(this).add(stringRequest);
}
}
这是我的cardview布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<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:id="@+id/cardview1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardElevation="5dp"
card_view:contentPadding="5dp"
card_view:cardCornerRadius="5dp"
card_view:cardMaxElevation="5dp">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#ECEFF1">
<ImageView
android:id="@+id/VolleyImageView"
android:layout_width="150dp"
android:layout_height="100dp"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:scaleType="center"
android:src="@mipmap/ic_launcher"
android:contentDescription="TODO" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/VolleyImageView"
android:layout_toEndOf="@+id/VolleyImageView"
android:layout_marginLeft="20dp"
android:layout_marginStart="20dp"
android:textSize="20sp"
android:textColor="#000"
android:text="@string/JSonImage"
android:id="@+id/ImageNameTextView"
android:layout_centerVertical="true"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
您没有正确设置列表项字段
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
ItemList itemlist = itemList.get(position);
holder.ImageTitleTextView.setText(itemlist.getTitle()); // you were setting getImage here
Glide.with(context)
.load(itemlist.getImage())
.into(holder.VollyImageView); // you were setting image on itemView which is not an ImageView.
}
对于列表项视图,应该使用wrap\u content
。如果您将匹配\u parent
,您的一个项目将覆盖所有屏幕
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" //changed this
android:orientation="vertical">
E/RecyclerView:未连接适配器
这只是一个警告,你可以忽略它 在您正在获取的Itemlist中,但还需要为每个get设置-eg-public void setId(int id){this.id=id;}在加载list之前尝试调用setAdapter,在加载数据之后尝试调用adapter.notifydatasetchanged()。我看到了我的混乱。谢谢但我还是有同样的问题。我在方法中添加了adapter.notifydatachange(),并将setAdapter移动到onCreate。我在ItemList类中添加了setter。我对Adapter类中的项目列表进行了更正…并在布局高度上对内容进行了换行。还有其他建议吗?
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
ItemList itemlist = itemList.get(position);
holder.ImageTitleTextView.setText(itemlist.getTitle()); // you were setting getImage here
Glide.with(context)
.load(itemlist.getImage())
.into(holder.VollyImageView); // you were setting image on itemView which is not an ImageView.
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" //changed this
android:orientation="vertical">