java—尝试使用recyclerview创建片段时获取“无适配器连接”消息

eufgjt7s  于 2021-07-06  发布在  Java
关注(0)|答案(0)|浏览(215)

我想创建一个片段并在一个显示产品的recyclerview中显示,但是当我遇到以下错误时:
recyclerview:未连接适配器;跳过布局
此代码适用于我的适配器:

private ArrayList<Products> ProductsList;
private Context context;

public Adapter(ArrayList<Products> productsList, Context context) {
    ProductsList = productsList;
    this.context = context;
}

@Override
public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(context).inflate(R.layout.row_layout, parent, false);
    return new MyHolder(v);
}

@Override
public void onBindViewHolder(MyHolder holder, final int position) {
    Products products = ProductsList.get(position);

    holder.txtName.setText(products.getName());
    holder.txtPrice.setText("$ " + products.getPrice());
    Picasso.get().load(Config.ipValue + "/images/" + products.getPhoto()).into(holder.imgV);
    holder.imgV.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            v.startAnimation(AnimationUtils.loadAnimation(context, android
                    .R.anim.slide_in_left));
        }
    });

}

@Override
public int getItemCount() {
    return ProductsList.size();
}

class MyHolder extends RecyclerView.ViewHolder {

    TextView txtName;
    TextView txtPrice;
    ImageView imgV;

    public MyHolder(View itemView) {
        super(itemView);

        txtName = itemView.findViewById(R.id.rowTxtProductName);
        txtPrice = itemView.findViewById(R.id.rowTxtPrice);
        imgV = itemView.findViewById(R.id.rowImgProduct);
    }
}

此代码用于我的web api类:

public class WebApiHandler {

Context context;
String apiLink = "";
ArrayList<Products> products = new ArrayList<>();

public WebApiHandler(Context context) {
    this.context = context;
}

void apiConnect(String type) {
    switch (type) {
        case "getproducts": {
            apiLink = Config.getProductsWebApi;
            break;
        }
    }

    ProgressDialog dialog = ProgressDialog.show(context, "Connecting...",
            "please wait", false, false);
    StringRequest request = new StringRequest(Request.Method.POST,
            apiLink, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            dialog.dismiss();
            showJson(response);
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            dialog.dismiss();
        }
    });
    RequestQueue queue = Volley.newRequestQueue(context);
    queue.add(request);
}

private void showJson(String response) {
    products.clear();
    try {
        JSONObject jsonObject = new JSONObject(response);
        JSONArray jsonArray = jsonObject.getJSONArray("response");
        for (int i = 0; i < jsonArray.length(); i++) {
            JSONObject object = jsonArray.getJSONObject(i);
            String id = object.getString("id");
            String name = object.getString("name");
            String description = object.getString("description");
            String price = object.getString("price");
            String photo = object.getString("photo");
            Products p = new Products(id, name, description, price, photo);
            products.add(p);
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
    VerticalFragment.productsArrayList = products;
    IData iData = (IData) context;
    iData.sendData();
}}

此代码用于我的片段:

public class VerticalFragment extends Fragment implements IData {

RecyclerView rcVertical;
WebApiHandler webApiHandler;
static ArrayList<Products> productsArrayList = new ArrayList<>();

public VerticalFragment() {}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_vertical, container, false);
    rcVertical = view.findViewById(R.id.rcVertical);
    webApiHandler = new WebApiHandler(getContext());
    webApiHandler.apiConnect("getproducts");
    rcVertical.addOnItemTouchListener(new RecyclerTouchListener(getContext(), rcVertical,
            new RecyclerTouchListener.ClickListener() {
                @Override
                public void onClick(View view, int position) {
                    ProductActivity.products = productsArrayList.get(position);
                    startActivity(new Intent(getContext(), ProductActivity.class));
                }

                @Override
                public void onLongClick(View view, int position) {

                }
            }));

    return view;
}

@Override
public void sendData() {
    Adapter adapter = new Adapter(productsArrayList, getContext());
    rcVertical.setLayoutManager(new LinearLayoutManager((getContext())));
    rcVertical.setItemAnimator(new DefaultItemAnimator());
    rcVertical.setAdapter(adapter);
}}

请注意 IData.sendData() 接口方法。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题