android - 数据在布局中创建并占据空白,但看不到

标签 android android-recyclerview

来自 Web 服务的数据被正确接收,我通过在日志中打印所有值来检查它,所以显然我的后端服务没有错误。根据布局中列表的数组大小,数据也占据了空白空间,但我看不到数据,只有带有 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>

这是我的 Activity 布局

<?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>

最佳答案

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

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

这意味着你应该更换

B_OfferProducts b_offerProducts = new B_OfferProducts();

onBindViewHolder() 中获取与列表中当前位置相关的对象,像这样:

B_OfferProducts b_offerProducts = offerproductlist.get(position)

希望对你有所帮助。干杯!

关于android - 数据在布局中创建并占据空白,但看不到,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50179240/

相关文章:

java - 为什么我的正则表达式在 android 的短信接收器上没有被正确解析?

android - 如何获取我用 Intent 选择的目录的路径并将其用作文件路径?

安卓。在 RecyclerView 中使用 fragments 是对还是错?

android - 如何在具有嵌套 RecyclerViews 的多 View 适配器上正确使用 RecycledViewPool?

android - 亚马逊上的更多应用链接

java - Android - 试图实例化一个不是 fragment 的类

android - Android 中的 EditText 提示错误

java - fragment 中的空回收器 View

android - MVVM-包含布局内的 Recyclerview 从未初始化

android - 如何在android中的 View 下获取 View 的点击事件