代码之家  ›  专栏  ›  技术社区  ›  Masoom Badi

数据在布局中创建并占用空白,但无法看到

  •  1
  • Masoom Badi  · 技术社区  · 8 年前

    来自web服务的数据被正确接收,我通过在日志中打印所有值来检查它,因此我的后端服务显然没有错误。根据布局中列表的数组大小,数据也占据了空白,但我看不到数据,只有带有文本视图的白色屏幕。有 LogCat中没有错误 .我不知道我做错了什么,如果没有什么帮助,我将不胜感激。谢谢

    这是我的 主要活动 其中 异步任务 获取数据。

     swaop=(SwipeRefreshLayout)contentview.findViewById(R.id.layout_offerproducts);
        rvofferproducts = (RecyclerView)findViewById(R.id.rv_offerproducts);
    
        oparraylist = new ArrayList<B_OfferProducts>();
    
        opadapter = new adapter_offerproducts(getApplicationContext(), oparraylist);
    
        final RecyclerView.LayoutManager manager = new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false);
        rvofferproducts.setLayoutManager(manager);
        rvofferproducts.setItemAnimator(new DefaultItemAnimator());
        rvofferproducts.setAdapter(opadapter);
    
        isConnected = ConnectivityReceiver.isConnected();
    
        if(isConnected)
        {
            new loadallofferproducts().execute();
        }
           //ASYNC TASK
        public class loadallofferproducts extends AsyncTask<String, String, String>
    {
        @Override
        protected String doInBackground(String... strings) {
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("OfferID", OfferID));
    
            JSONObject jsonObject = jsonParser.makeHttpRequest(url_offerproducts, "POST", params);
    
            try {
                int success = jsonObject.getInt("success");
    
                if(success == 1)
                {
                    ophasharraylist = new ArrayList<HashMap<String, String>>();
                    offerproductsarray = jsonObject.getJSONArray("offerproducts");
    
                    for(int i = 0; i < offerproductsarray.length(); i++)
                    {
                        JSONObject opobj =offerproductsarray.getJSONObject(i);
                        String ProductId = opobj.getString("ProductID");
                        String ProductName = opobj.getString("ProductName");
                        String ProductPrice = "₹ " + opobj.getString("ProductPrice");
                        String ProductDP = "₹ " +opobj.getString("ProductDiscountP");
                        String ProductURL = opobj.getString("ProductPicURL");
                        String ProductDescription = opobj.getString("ProductDescription");
    
                        HashMap<String, String> map = new HashMap<String, String>();
                       B_OfferProducts b_offerProducts = new B_OfferProducts();
    
                        map.put("ProductID", ProductId);
                        map.put("ProductName", ProductName);
                        map.put("ProductPrice", ProductPrice);
                        map.put("ProductDiscountP", ProductDP);
                        map.put("ProductPicURL",ProductURL);
                        map.put("ProductDescription", ProductDescription);
    
                        ophasharraylist.add(map);
    
                        b_offerProducts.setProductPicURL(ophasharraylist.get(i).get("ProductPicURL").trim());
                        b_offerProducts.setProductName(ophasharraylist.get(i).get("ProductName").trim());
                        b_offerProducts.setProductPrice(ophasharraylist.get(i).get("ProductPrice").trim());
                        b_offerProducts.setProductDiscountP(ophasharraylist.get(i).get("ProductDiscountP").trim());
                        b_offerProducts.setProductDescription(ophasharraylist.get(i).get("ProductDescription").trim());
    
                        Log.d("OfferProducts",b_offerProducts.getProductName());
                        oparraylist.add(b_offerProducts);
    
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
    
            return null;
        }
    
        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            opadapter.notifyDataSetChanged();
        }
      }
    }
    

    这是 我的适配器

    public class adapter_offerproducts extends RecyclerView.Adapter<adapter_offerproducts.MyViewHolder>{
    
    private List<B_OfferProducts> offerproductlist;
    Context mcontext;
    
    public class MyViewHolder extends  RecyclerView.ViewHolder
    {
        public TextView pname, pprice, pdpricce, pdescription;
        public ImageView pimg;
    
        public MyViewHolder(View view)
        {
            super(view);
    
            pname = (TextView)view.findViewById(R.id.rv_op_pname);
            pprice = (TextView)view.findViewById(R.id.rv_op_pprice);
            pdpricce = (TextView)view.findViewById(R.id.rv_op_pdprice);
            pdescription = (TextView)view.findViewById(R.id.rv_op_pdescription);
    
            pimg = (ImageView)view.findViewById(R.id.rv_op_img);
        }
    }
    
    public adapter_offerproducts(Context context, List<B_OfferProducts> oplist )
    {
        mcontext = context;
        this.offerproductlist = oplist;
    }
    
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemview = LayoutInflater.from(parent.getContext()).inflate(R.layout.rv_container_offerproducts,parent,false);
    
        return new MyViewHolder(itemview);
    }
    
    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
    
       B_OfferProducts b_offerProducts = new B_OfferProducts();
        Picasso.get()
                .load(b_offerProducts.getProductPicURL())
                .into(holder.pimg);
    
        holder.pname.setText(b_offerProducts.getProductName());
        holder.pprice.setText(b_offerProducts.getProductPrice());
        holder.pdpricce.setText(b_offerProducts.getProductDiscountP());
        holder.pdescription.setText(b_offerProducts.getProductDescription());
    }
    
    @Override
    public int getItemCount() {
        return offerproductlist.size();
      }
    }
    

    这是 我的容器 对于RecyclerView

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    
    <ImageView
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:id="@+id/rv_op_img"
        android:contentDescription="ProductImage"/>
    
    <TextView
        android:id="@+id/rv_op_pname"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:text="TextView"
        app:layout_constraintStart_toEndOf="@+id/rv_op_img"
        tools:layout_editor_absoluteY="16dp" />
    
    <TextView
        android:id="@+id/rv_op_pprice"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:text="TextView"
        app:layout_constraintStart_toEndOf="@+id/rv_op_img"
        tools:layout_editor_absoluteY="56dp" />
    
    <TextView
        android:id="@+id/rv_op_pdprice"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:text="TextView"
        app:layout_constraintStart_toEndOf="@+id/rv_op_img"
        tools:layout_editor_absoluteY="96dp" />
    
    <TextView
        android:id="@+id/rv_op_pdescription"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:text="TextView"
        app:layout_constraintStart_toEndOf="@+id/rv_op_img"
        tools:layout_editor_absoluteY="131dp" />
    
     </android.support.constraint.ConstraintLayout>
    

    这是 我的活动布局

    <?xml version="1.0" encoding="utf-8"?>
     <android.support.v4.widget.SwipeRefreshLayout 
      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:id="@+id/layout_offerproducts"
      tools:context="com.test.OfferProducts">
    
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_marginTop="60dp"
            android:layout_gravity="center"
            android:layout_height="wrap_content"
            android:orientation="vertical" >
            <android.support.v7.widget.RecyclerView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/rv_offerproducts">
    
            </android.support.v7.widget.RecyclerView>
            <TextView
                android:id="@+id/textView4"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Blah This IS TEXT" />
        </LinearLayout>
    </ScrollView>
    

    1 回复  |  直到 8 年前
        1
  •  1
  •   Gotiasits    8 年前

    你可能应该 B_OfferProducts 从列表传递给适配器,即从列表 offerproductlist ,而不是每次都创建对象。

    无论您在 B\U提供的产品 类时,您的项将不会绑定到列表,这可能是数据加载不正确的原因。创建的这些对象将仅位于ViewHolder内部。

    这意味着您应该替换

    B_OfferProducts b_offerProducts = new B_OfferProducts();
    

    在里面 onBindViewHolder() 获取与列表中当前位置相关的对象,如下所示:

    B_OfferProducts b_offerProducts = offerproductlist.get(position)
    

    我希望这有帮助。干杯