在我的应用中,我正在片段中使用回收站视图。该回收者视图数据来自数据库。我可以完美地获取所有数据,但所有数据都相互重叠,并在日志目录中出现错误。我正在使用卡视图创建回收者视图的每一行,并使用凌空库来获取数据。
fragment.java :
public class HomeFragment extends Fragment {
private static final String CATEGORY_URL = "http://192.168.0.101/cart/category/get_all_category.php";
List<Category> categoryList;
RecyclerView recyclerView;
CustomCategoryList customCategoryList;
public HomeFragment() {
// Required empty public constructor
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_home, container,false);
//getting the recyclerview from xml
recyclerView = (RecyclerView) rootView.findViewById(R.id.recylcerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this.getActivity()));
recyclerView.setHasFixedSize(true);
categoryList = new ArrayList<>();
loadCategory();
//customCategoryList = new CustomCategoryList(getActivity(),categoryList);
return rootView;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
private void loadCategory(){
StringRequest stringRequest= new StringRequest(Request.Method.GET, CATEGORY_URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject obj = new JSONObject(response);
JSONArray array = obj.getJSONArray("category");
for (int i = 0; i < array.length(); i++){
//getting the user from the response
JSONObject userJson = array.getJSONObject(i);
categoryList.add(new Category(
userJson.getInt("categoryid"),
userJson.getString("categoryname")
));
}
customCategoryList = new CustomCategoryList(getActivity(),categoryList);
recyclerView.setAdapter(customCategoryList);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
Volley.newRequestQueue(getActivity()).add(stringRequest);
}
}
fragment.xml :
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="@+id/header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CATEGORIES : -"
android:textSize="20sp"
android:textStyle="bold"
android:textColor="@color/blue"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/recylcerView"
android:layout_below="@+id/header"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="@dimen/activity_horizontal_margin">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
row.xml :
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_marginBottom="2dp"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_marginTop="2dp"
android:orientation="vertical"
app:cardBackgroundColor="@color/white"
app:cardCornerRadius="5dp">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/top_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:orientation="horizontal"
android:padding="5dp">
<ImageView
android:layout_width="100dp"
android:layout_height="90dp"
android:layout_gravity="center_vertical"
android:src="@drawable/home_appliances"
android:layout_marginLeft="5dp"
android:gravity="center_vertical"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_gravity="center_vertical"
android:layout_marginLeft="5dp"
android:orientation="vertical">
<TextView
android:id="@+id/catname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="HOME DECORE"
android:textStyle="bold"
android:textSize="20sp"
android:textColor="@color/black"
android:padding="@dimen/nav_header_vertical_spacing"/>
</RelativeLayout>
</LinearLayout>
错误:
错误:09-28 20:08:27.991 19249-19249 / com.example.arpan.e_cart E / RecyclerView:未连接适配器;跳过布局
下面的适配器类:
public class CustomCategoryList extends RecyclerView.Adapter<CustomCategoryList.ViewHolder>{
private Context mCtx;
private List<Category> categoryList;
public class ViewHolder extends RecyclerView.ViewHolder {
private TextView category;
public ViewHolder (View view) {
super(view);
category = view.findViewById(R.id.catname);
}
}
public CustomCategoryList(Context mCtx, List<Category> categoryList) {
this.mCtx = mCtx;
this.categoryList = categoryList;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(mCtx);
View view = inflater.inflate(R.layout.custom_category_list,null);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Category category = categoryList.get(position);
holder.category.setText(category.getCategoryname());
}
@Override
public int getItemCount() {
return categoryList.size();
}
}
答案 0 :(得分:0)
我相信会发生此错误,因为在渲染视图时尚未将适配器添加到recyclerview中。
您是否尝试过在初始化recyclerview之后而不是在onResponse方法内部放置这两行?
customCategoryList = new CustomCategoryList(getActivity(),categoryList);
recyclerView.setAdapter(customCategoryList);
然后您实际上会在onResponse中执行
customCategoryList.notifyDataSetChanged();
将所有数据添加到列表中
答案 1 :(得分:0)
创建此文件。
CategoryListAdapter.java
public class CategoryListAdapter extends RecyclerView.Adapter<CategoryListAdapter.ItemHolder> {
private List<Category> categoryList;
private Context mContext;
public CategoryListAdapter(List<Category> categoryList, Context mContext) {
this.categoryList = categoryList;
this.mContext = mContext;
}
@NonNull
@Override
public ItemHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row, parent, false);
return new ItemHolder(view);
}
@Override
public void onBindViewHolder(@NonNull final ItemHolder holder, final int position) {
// Get each category, one by one.
Category singleCategory = categoryList.get(position);
// Set the category content to the layout
holder.name.setText(singleCategory.getName());
// Do something with the image. You can load the url using GlideApp
holder.imageView ...
@Override
public int getItemCount() { return categoryList.size(); }
// --------------------------------------------------------------------------------
// ViewHolder
// --------------------------------------------------------------------------------
public class ItemHolder extends RecyclerView.ViewHolder {
public ImageView imageView;
public TextView name;
public ItemHolder(View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.image_view);
name = itemView.findViewById(R.id.catname);
}
}
}
最后设置回收者视图适配器,(将其设置为Macmist答案)
mRecyclerView.setAdapter(new CategoryListAdapter(categoryList, getActivity()));