动态RecyclerView上的空视图

时间:2019-05-21 16:55:33

标签: android android-recyclerview android-volley

我正在从Android的MySQL数据库中检索数据,并且正在使用Recyclerview。我测试了php文件,它工作正常,但活动中没有任何显示。我检查了每件事,似乎还好,我不知道这是什么问题?

等待活动:          

<android.support.v7.widget.RecyclerView
    android:id="@+id/recylcerView"
    android:layout_width="1dp"
    android:layout_height="1dp"
    tools:layout_editor_absoluteX="745dp"
    tools:layout_editor_absoluteY="51dp" />

</RelativeLayout>

list_layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="8dp">

    <TextView
        android:id="@+id/imageView"
        android:layout_width="120dp"
        android:layout_height="90dp"
        android:padding="4dp"
       android:textAppearance="@style/Base.TextAppearance.AppCompat.Small"
        android:text=" Waiting Time :"
        android:textColor="#000000"
        android:textStyle="bold"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="5dp"/>

    <TextView
        android:id="@+id/textViewTime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_toRightOf="@id/imageView"
        android:text="00"
        android:layout_marginTop="5dp"
        android:textAppearance="@style/Base.TextAppearance.AppCompat.Small"
        android:textColor="#000000" />

    <TextView
        android:id="@+id/textViewRating"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/textViewTime"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="5dp"
        android:layout_toRightOf="@id/imageView"
        android:background="@color/colorPrimary"
        android:paddingLeft="15dp"
        android:paddingRight="15dp"
        android:text="Bus Number : "
             android:textAppearance="@style/Base.TextAppearance.AppCompat.Small.Inverse"
        android:textStyle="bold" />
    <TextView
        android:id="@+id/textViewBusNumber"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/textViewRating"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="5dp"
        android:layout_toRightOf="@id/imageView"
        android:text="255"
         android:textAppearance="@style/Base.TextAppearance.AppCompat.Large" />

</RelativeLayout>

</LinearLayout>

WaitingActivity:

public class WaitingActivity extends AppCompatActivity {
private static final String URL_PRODUCTS = "http://192.168.1.2/Android/v1/test1.php";

List<Product> productList;
RecyclerView recyclerView;
ProductsAdapter adapter ;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_waiting);
    recyclerView = (RecyclerView) findViewById(R.id.recylcerView);
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));

}

private void loadProducts() {

    StringRequest stringRequest = new StringRequest(Request.Method.GET, URL_PRODUCTS,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    try {
                        JSONArray products = new JSONArray(response);

                        for (int i = 0; i < products.length(); i++) {

                JSONObject productObject = products.getJSONObject(i);
                  int Temp_dt = productObject.getInt("Temp_dt");
                  int BusNumber =productObject.getInt("BusNumber");
                  Product product = new Product(Temp_dt, BusNumber);

                            productList.add(product);


                        }


    adapter = new  ProductsAdapter(WaitingActivity.this, productList);
                        recyclerView.setAdapter(adapter);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
 Toast.makeText(WaitingActivity.this,  error.getMessage(),Toast.LENGTH_SHORT).show();
                }
            });

    //adding our stringrequest to queue
    Volley.newRequestQueue(this).add(stringRequest);


   }

产品类别:

    public class Product {

private int Temp_dt;
private int BusNumber;


public Product(int Temp_dt, int BusNumber) {
    this.Temp_dt = Temp_dt;
    this.BusNumber = BusNumber ;

}

public int getWaitingTime() {
    return Temp_dt;
}

public int getBusNumber() {
    return BusNumber ;
}

}

ProductAdapter:

 public class ProductsAdapter extends 
   RecyclerView.Adapter<ProductsAdapter.ProductViewHolder> {

   private Context mCtx;
   private List<Product> productList;
    public ProductsAdapter(Context mCtx, List<Product> productList) {
    this.mCtx = mCtx;
    this.productList = productList;
}

   @Override
   public ProductViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    //inflating and returning our view holder
    LayoutInflater inflater = LayoutInflater.from(mCtx);
    View view = inflater.inflate(R.layout.list_layout, null); 
    return new ProductViewHolder(view);
}

@Override
public void onBindViewHolder(ProductViewHolder holder, int position) {
    Product product = productList.get(position);
holder.textViewTime.setText(String.valueOf(product.getWaitingTime()));                                                           
holder.textViewBusNumber.setText(String.valueOf(product.getBusNumber()));

}

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

class ProductViewHolder extends RecyclerView.ViewHolder {

    TextView textViewTime;
    TextView textViewBusNumber;

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

        textViewTime = itemView.findViewById(R.id.textViewTime);
    textViewBusNumber = itemView.findViewById(R.id.textViewBusNumber);

    }
}
}

我希望从php文件显示temp_dt和等待时间,但实际输出只是空的活动。

0 个答案:

没有答案