从API调用时为什么回收器视图不显示

时间:2018-04-12 06:07:14

标签: android android-recyclerview

我想在片段中显示从API调用回收器视图的信息,但回收器视图不显示信息。 Api显示GET方法,需要在Recycler View中显示列表。

这是我的模特

public class PhoneModel {

    public PhoneModel() {
    }

   private int amount;
   private String amount_display;

   public int getAmount() {
       return amount;
   }

   public void setAmount(int amount) {
       this.amount = amount;
   }

   public String getAmount_display() {
       return amount_display;
   }

   public void setAmount_display(String amount_display) {
       this.amount_display = amount_display;
   }
}

这是我的适配器。我只想在Recycler View中显示一个信息。

public class PhoneAdapter extends RecyclerView.Adapter<PhoneAdapter.PhoneViewHolder> {

   private ArrayList<PhoneModel> listItems;
   private Context context;
   LayoutInflater inflater;

   public PhoneAdapter(Context context) {
       this.listItems = new ArrayList<>();
       this.context = context;
       inflater = LayoutInflater.from(context);
   }

   @Override
   public PhoneViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
       View v = inflater.from(parent.getContext())
            .inflate(R.layout.view_holder_phone,parent,false);
       return new PhoneViewHolder(v);
   }

   @Override
   public void onBindViewHolder(PhoneViewHolder holder, int position) {

       holder.test.setText(listItems.get(position).getAmount_display());
   }


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

   public void add(PhoneModel model) {
      listItems.add(model);
      notifyDataSetChanged();
   }

   public void clear() {
       listItems.clear();
       notifyDataSetChanged();
   }


public class PhoneViewHolder extends RecyclerView.ViewHolder {

    private TextView test;
    private TextView currentBal;

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

        test = itemView.findViewById(R.id.tv_deposit);
        currentBal = itemView.findViewById(R.id.tv_current_bal);

    }
   }
}

这是我与手机型号和手机适配器连接的活动。

public class HistoryFragment extends Fragment {

private final static String TAG = HistoryFragment.class.getSimpleName();

private PhoneAdapter phoneAdapter;
private RecyclerView recyclerView;

public HistoryFragment() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_history, container, false);

    recyclerView = view.findViewById(R.id.recycler_history);
    LinearLayoutManager mLinearLayoutManager = new LinearLayoutManager(getActivity(),
            LinearLayoutManager.VERTICAL, false);
    recyclerView.setLayoutManager(mLinearLayoutManager);
    phoneAdapter = new PhoneAdapter(getContext());
    recyclerView.setAdapter(phoneAdapter);

    phoneHistory();

    return view;
}

private void phoneHistory(){

    StringRequest request = new StringRequest(
            Request.Method.GET,
            ServerApi.URL_PHONE,

            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {

                    Gson gson = new GsonBuilder().create();
                    JSONObject jsonObject = null;

                    if(response != null) {

                        try {
                            jsonObject = new JSONObject(response);

                            PhoneModel phoneModel = gson.fromJson(jsonObject.getJSONObject("value").toString(), PhoneModel.class);
                            phoneAdapter.add(phoneModel);

                        } catch (JSONException e) {
                            Log.d(TAG, e.getLocalizedMessage());
                        }
                    }
                }
            },

            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText(getActivity(), error.getLocalizedMessage(), Toast.LENGTH_LONG).show();

                }
            }
    ) {

        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {

            HashMap<String, String> headers = new HashMap<>();
            headers.put("Content-Type", "application/x-www-form-urlencoded");
            return headers;
        }

    };

    Volley.newRequestQueue(getContext()).add(request);
}


}

3 个答案:

答案 0 :(得分:0)

试试这个

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                     Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_history, container, false);



recyclerView = view.findViewById(R.id.recycler_history);
LinearLayoutManager mLinearLayoutManager = new 
LinearLayoutManager(getActivity(),
        LinearLayoutManager.VERTICAL, false);
recyclerView.setLayoutManager(mLinearLayoutManager);   

 phoneHistory(); 

phoneAdapter = new PhoneAdapter(getContext());
recyclerView.setAdapter(phoneAdapter);



return view;
}

答案 1 :(得分:0)

您忘记在adapter. notifyDataSetChanged()方法中致电phoneHistory()

此外,您未在PhoneHolder班级

中初始化可变电话

答案 2 :(得分:0)

您获取空数据,因为在适配器构造函数中您创建了新的arraylist()

改变这个:

public PhoneAdapter(Context context) {
    this.listItems = new ArrayList<>();
    this.context = context;
    inflater = LayoutInflater.from(context);
}

public PhoneAdapter(Context context,Arraylist list) {
    this.listItems = list;
    this.context = context;
    inflater = LayoutInflater.from(context);
}

也改变片段:

phoneAdapter = new PhoneAdapter(getContext());
    recyclerView.setAdapter(phoneAdapter,list);

phoneHistory()方法

if(response != null) {

     try {
           jsonObject = new JSONObject(response);

         PhoneModel phoneModel = gson.fromJson(jsonObject.getJSONObject("value").toString(), PhoneModel.class);
         phoneModel .add(phoneModel);
         list.add(phoneModel )
         phoneAdapter.notifyDataSetChanged()
         } catch (JSONException e) {
           Log.d(TAG, e.getLocalizedMessage());
        }
     }

希望它能帮到你!!