ArrayList大小在RecyclerView中为零,并且没有数据显示

时间:2018-09-13 17:56:49

标签: android android-studio

我正在尝试在recyclerview中获得响应。我在日志中得到响应,但是当我将其添加到recyclerView时,没有数据显示。我无法理解我所缺少的。这让我非常恼火,请帮助

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder> {
ArrayList<Model> arrayList;

@NonNull
@Override
public MyViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
    View itemView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item,viewGroup,false);
    return new MyViewHolder(itemView);
}

public RecyclerViewAdapter(ArrayList<Model> arrayList) {
    this.arrayList = arrayList;
    System.out.println("size 111  "+arrayList.size());
}

@Override
public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, int i) {
    Model model = arrayList.get(i);

    myViewHolder.tvName.setText(model.getName());
    myViewHolder.tvEmail.setText(model.getEmail());
    myViewHolder.tvMobile.setText(model.getMobile());

}

@Override
public int getItemCount() {
    System.out.println("size "+arrayList.size());
    return arrayList.size();
}

public class MyViewHolder extends RecyclerView.ViewHolder {
    TextView tvName, tvEmail, tvMobile;

    public MyViewHolder(@NonNull View itemView) {
        super(itemView);
        tvName = (TextView) itemView.findViewById(R.id.tvName);
        tvEmail = (TextView) itemView.findViewById(R.id.tvEmail);
        tvMobile = (TextView) itemView.findViewById(R.id.tvMobile);
    }
}

}

这是我的活动班

public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
Model model;
ArrayList<Model> arrayList;
RecyclerView recyclerView;
RecyclerViewAdapter recyclerViewAdapter;
String url = "https://api.androidhive.info/volley/person_array.json";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    arrayList = new ArrayList<>();
    recyclerView = (RecyclerView)findViewById(R.id.recyclerView);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));


    recyclerViewAdapter = new RecyclerViewAdapter(arrayList);
    recyclerView.setAdapter(recyclerViewAdapter);
    getData();



}

protected void getData() {
    final ProgressDialog dialog = new ProgressDialog(MainActivity.this);
    dialog.setMessage("Loading......");
    dialog.show();
    JsonArrayRequest arrayRequest = new JsonArrayRequest(url, new Response.Listener<JSONArray>() {
        @Override
        public void onResponse(JSONArray response) {
            Log.e(TAG, "onResponse: "+response.toString() );
            dialog.hide();
            for (int i=0;i<response.length();i++){
                try {
                    JSONObject object = (JSONObject) response.get(i);
                    String name = object.getString("name");
                    System.out.println("name "+name);
                    String email = object.getString("email");
                    JSONObject object1 = object.getJSONObject("phone");
                    String mobile = object1.getString("mobile");
                    System.out.println("mobile "+mobile);
                    model = new Model(name,email,mobile);
                    arrayList.add(model);
                    System.out.println("activity sizee"+arrayList.size());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

尝试了一切,但没有任何效果,请帮我解决这个问题。我在哪里犯错。请帮助

1 个答案:

答案 0 :(得分:3)

在for循环之后,您需要使用notifyDataSetChanged通知更改,以告知适配器更新列表视图。适配器观察者将在内部收到数据源已被更改的通知,它需要遍历它并重新将数据与布局绑定

@Override
    public void onResponse(JSONArray response) {
        Log.e(TAG, "onResponse: "+response.toString() );
        dialog.hide();
        for (int i=0;i<response.length();i++){
            try {
                JSONObject object = (JSONObject) response.get(i);
                String name = object.getString("name");
                System.out.println("name "+name);
                String email = object.getString("email");
                JSONObject object1 = object.getJSONObject("phone");
                String mobile = object1.getString("mobile");
                System.out.println("mobile "+mobile);
                model = new Model(name,email,mobile);
                arrayList.add(model);
                System.out.println("activity sizee"+arrayList.size());
            } catch (JSONException e) {
                e.printStackTrace();
            }
           recyclerViewAdapter.notifyDataSetChanged();
           //^^^^^^^^^^^^^^^^^^^^^
        }