我想在Recyclerview中显示Mysql数据库中的数据,但我已经使用了以下代码,但白页显示了没有任何内容,没有附加适配器的错误,跳过了布局,请有人帮助我
当我检查数据的json格式以在浏览器上显示时,它运作良好并且可以看到一些数据,我还更改了recyclerview的高度以匹配父级,但没有任何改变
这是我的活动代码
private static final String URL_PRODUCTS = BASE_URL + "2nksacco/task/DisplayTicket.php";
List<DateAdapter>itemList;
RecyclerView recyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ticket_history);
//getting the recyclerview from xml
recyclerView = findViewById(R.id.recyclerView1);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
//RecyclerViewAdapter DateAdapter = new RecyclerViewAdapter(TicketHistory.this, itemList);
//recyclerView.setAdapter(DateAdapter);
//initializing the productlist
itemList = new ArrayList<>();
//this method will fetch and parse json
//to display it in recyclerview
loadProducts();
}
private void loadProducts() {
StringRequest stringRequest = new StringRequest(Request.Method.GET, URL_PRODUCTS,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
//converting the string to json array object
JSONArray array = new JSONArray(response);
for (int i = 0; i < array.length(); i++) {
//getting product object from json array
JSONObject product = array.getJSONObject(i);
itemList.add(new DateAdapter(
product.getString("pname"),
product.getString("depdate"),
product.getString("route"),
product.getString("seats"),
product.getString("amount"),
product.getString("email")
));
}
recyclerview
RecyclerViewAdapter adapter = new RecyclerViewAdapter(TicketHistory.this, itemList);
recyclerView.setAdapter( adapter);
adapter.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
//adding our stringrequest to queue
Volley.newRequestQueue(this).add(stringRequest);
}
}
这是我的适配器代码
private String pname;
private String depdate;
private String route;
private String seats;
private String amount;
private String email;
public DateAdapter( String pname, String depdate, String route, String seats, String amount,String email) {
this.pname = pname;
this.depdate = depdate;
this.route = route;
this.seats = seats;
this.amount = amount;
this.email = email;
}
public String getPname() {
return pname;
}
public String getDepdate() {
return depdate;
}
public String getRoute() {
return route;
}
public String getSeats() {
return seats;
}
public String getAmount() {
return amount;
}
public String getEmail() {
return email;
}
}
这是我的recylervierview适配器代码
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.DateAdapterViewHolder> {
private Context mCtx;
//private SharedPreferences pref;
private List<DateAdapter> itemList;
public RecyclerViewAdapter(Context mCtx,List<DateAdapter>itemList){
this.mCtx=mCtx;
this.itemList=itemList;
}
@Override
public DateAdapterViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(mCtx);
View view = inflater.inflate(R.layout.item_view, null);
return new DateAdapterViewHolder(view);
}
@Override
public void onBindViewHolder(DateAdapterViewHolder holder, int position) {
DateAdapter product = itemList.get(position);
holder.TextViewName.setText(product.getPname());
holder.TextViewDate.setText(product.getDepdate());
holder.TextViewRoute.setText(product.getRoute());
holder.TextViewSeats.setText(product.getSeats());
holder.TextViewEmail.setText(product.getEmail());
holder.TextViewMoney.setText(String.valueOf(product.getAmount()));
}
@Override
public int getItemCount() {
return itemList.size();
}
class DateAdapterViewHolder extends RecyclerView.ViewHolder {
TextView TextViewName, TextViewDate, TextViewRoute, TextViewSeats,TextViewMoney, TextViewEmail;
public DateAdapterViewHolder(View itemView) {
super(itemView);
TextViewName = itemView.findViewById(R.id.textViewName) ;
TextViewDate = itemView.findViewById(R.id.textViewDate) ;
TextViewRoute = itemView.findViewById(R.id.textViewRoute);
TextViewSeats = itemView.findViewById(R.id.textViewSeats);
TextViewMoney = itemView.findViewById(R.id.textViewMoney);
TextViewEmail = itemView.findViewById(R.id.textViewEmail);
}
}
}
这是我的xml代码
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_ticket_history"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".TicketHistory">
<TextView
android:id="@+id/tv_month"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
android:hint="Month and User"
android:text="TextView"
android:textAlignment="center"
android:textStyle="bold"
android:visibility="invisible" />
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView1"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_above="@+id/progressBar"
android:layout_alignParentLeft="true"
android:layout_marginTop="60dp"
android:layout_alignParentStart="true" />
<ProgressBar
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:id="@+id/progressBar"
/>
</RelativeLayout>
列出要显示的项目
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/cardview1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="7dp"
android:padding="10dp"
card_view:cardBackgroundColor="#009688"
card_view:cardCornerRadius="6dp"
card_view:cardElevation="6dp"
card_view:contentPadding="7dp">
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TableRow>
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name : "
android:textColor="#ffff"
android:textSize="20sp" />
<TextView
android:id="@+id/textViewName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name Show Here"
android:textColor="#ffff"
android:textSize="20sp"
tools:text="@string/Name" />
</TableRow>
<TableRow>
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Date : "
android:textColor="#ffff"
android:textSize="20sp" />
<TextView
android:id="@+id/textViewDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Date Show Here"
android:textColor="#ffff"
android:textSize="20sp"
tools:text="@string/Ddate" />
</TableRow>
<TableRow>
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="route: "
android:textColor="#ffff"
android:textSize="20sp" />
<TextView
android:id="@+id/textViewEmail"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Route Travelled"
android:textColor="#ffff"
android:textSize="20sp"
tools:text="@string/Route" />
</TableRow>
<TableRow>
<TextView
android:id="@+id/textView9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Email: "
android:textColor="#ffff"
android:textSize="20sp" />
<TextView
android:id="@+id/textViewRoute"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Route Show"
android:textColor="#ffff"
android:textSize="20sp"
tools:text="Email used" />
</TableRow>
<TableRow>
<TextView
android:id="@+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Number of seats : "
android:textColor="#ffff"
android:textSize="20sp" />
<TextView
android:id="@+id/textViewSeats"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Seats Show"
android:textColor="#ffff"
android:textSize="20sp"
tools:text="@string/Seats" />
</TableRow>
<TableRow>
<TextView
android:id="@+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Amount Paid : "
android:textColor="#ffff"
android:textSize="20sp" />
<TextView
android:id="@+id/textViewMoney"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Amount Show"
android:textColor="#ffff"
android:textSize="20sp"
tools:text="Amount paid" />
</TableRow>
</TableLayout>
</android.support.v7.widget.CardView>
我希望能够查看屏幕上显示的项目