Я создал приложение, которое отображает заказы на еду в RecyclerView, размещенном во фрагменте.Детали заказа еды получены с помощью AsyncTask.Но как-то ничего не отображается во фрагменте.Это остается пустым.Я должен что-то забыть или что-то упустить.
Это фрагмент заказа:
public class OrdersFragment extends Fragment {
RecyclerView recyclerView;
private OrdersAdapter adapter;
RecyclerView.LayoutManager layoutManager;
private static List<Orders> orderslist;
public static final MediaType JSON = MediaType.parse("application/json;
charset=utf-8");
SharedPreferences sharedPreferences;
View view;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
sharedPreferences = getActivity().getSharedPreferences("UserInfo",0);
view = inflater.inflate(R.layout.fragment_orders, container, false);
recyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
orderslist = new ArrayList<>();
new load_orders().execute();
return view;
}
public class load_orders extends AsyncTask<String, Void, Void> {
@Override
protected Void doInBackground(String... strings) {
try{
OkHttpClient client = new OkHttpClient();
JSONObject json = new JSONObject();
JSONObject datajson = new JSONObject();
datajson.put("command","ecoexpress_orders");
datajson.put("username",
sharedPreferences.getString("Username",null));
json.put("data",datajson);
RequestBody body = RequestBody.create(JSON,
json.toString());
Request request = new Request.Builder()
.url("https://api.watext.com/hook/insert_pagetoken")
.post(body)
.addHeader("content-type", "application/json")
.addHeader("cache-control", "no-cache")
.addHeader("postman-token", "55747e8a-0a9a-1fd5-77b0-8af0027be1f1")
.build();
Response response = client.newCall(request).execute();
JSONObject responseJSON = new JSONObject(response.body().string());
JSONArray messageArray = responseJSON.getJSONArray("message");
for (int i = 0; i < messageArray.length(); i ++ ){
JSONObject order_item = messageArray.getJSONObject(i);
Orders orders = new Orders(order_item.getString("order_id"),
order_item.getString("order_details"));
orderslist.add(orders);
}
}
catch (Exception e){
return null;
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
layoutManager = new LinearLayoutManager(getActivity());
adapter = new OrdersAdapter(getContext(),orderslist);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(adapter);
//adapter.notifyDataSetChanged();
}
}
}
А вот модель Order.class:
public class Orders {
private String order_id;
private String order_details;
public Orders(String order_id, String order_details) {
this.order_id = order_id;
this.order_details = order_details;
}
public String getOrder_id() {
return order_id;
}
public void setOrder_id(String order_id) {
this.order_id = order_id;
}
public String getOrder_details() {
return order_details;
}
public void setOrder_details(String order_details) {
this.order_details = order_details;
}
}
И этоэто Адаптер Orders:
public class OrdersAdapter extends RecyclerView.Adapter<OrdersAdapter.ViewHolder> {
private Context ctx;
private List<Orders> list;
public OrdersAdapter(Context ctx, List<Orders> list) {
this.ctx = ctx;
this.list = list;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.cards_layout, viewGroup, false);
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(ViewHolder viewHolder, int i) {
viewHolder.id.setText(list.get(i).getOrder_id());
viewHolder.details.setText(list.get(i).getOrder_details());
}
@Override
public int getItemCount() {
return list.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
public TextView id, details;
public ViewHolder( View itemView) {
super(itemView);
id = (TextView)itemView.findViewById(R.id.orderID_textview);
details = (TextView)itemView.findViewById(R.id.orderDetails_textview);
}
}
}
Вот макет для фрагмента Orders:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
/>
</RelativeLayout>
И макет для CardView:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<android.support.v7.widget.CardView
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
card_view:cardBackgroundColor="@color/colorPrimary"
card_view:cardCornerRadius="10dp"
card_view:cardElevation="5dp"
card_view:cardUseCompatPadding="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:layout_weight="2"
android:orientation="vertical"
>
<TextView
android:id="@+id/orderID_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:text="Order ID"
/>
<TextView
android:id="@+id/orderDetails_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:text="Order Details"
/>
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
Заранее спасибо!