数据在布局中创建并占用空白区域但看不到它

时间:2018-05-04 16:15:59

标签: android android-recyclerview

来自Web服务的数据被正确接收,我通过打印Log中的所有值来检查它,所以很明显我的后端服务没有错误。根据布局中列表的数组大小,数据也占据空白区域,但是我看不到数据,只有带有TextView的白色屏幕。 LogCat中没有错误。我不知道我做错了什么,很少得到帮助。感谢

这是 MainActivity ,其中 AsyncTask 用于获取数据。

 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 个答案:

答案 0 :(得分:1)

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

无论您在B_OfferProducts类的构造函数中放置什么,您的项目都不会绑定到列表,这可能是数据未正确加载的原因。创建的这些对象仅存在于ViewHolder中。

这意味着您应该替换

B_OfferProducts b_offerProducts = new B_OfferProducts();
onBindViewHolder()中的

使对象与列表中的当前位置相关,如下所示:

B_OfferProducts b_offerProducts = offerproductlist.get(position)

我希望它有所帮助。干杯!