Arraylist未填满

时间:2018-02-04 16:26:25

标签: java android android-recyclerview

我不明白为什么大小为null。因此,数据未加载到recyclerview中。

我从我的数据库中的图像加载URL,它应该显示在recyclerview中。我测试了,我得到了URL,但它们没有添加到列表中。

SharedPreferences USERNAMEsp;
String USERNAME;

RecyclerView recyclerViewImageSlider;
List<ImageGetter> imageList = new ArrayList<>();
ImageAdapter imageAdapter;
ImageGetter imageGetter;
PullData pullData = new PullData(Profil.this);


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_profil);

    USERNAMEsp = getSharedPreferences("DATAUSERNAME",MODE_PRIVATE);
    USERNAME = USERNAMEsp.getString("DATAUSERNAME","");

    pullData.getData(USERNAME, "3", new Pullcallback() {
        @Override
        public void getSingleData(String data) {
            imageGetter = new ImageGetter(data);
            imageList.add(imageGetter);
        }

        @Override
        public void getMultipleData(String[] multipledatas) {

        }

        @Override
        public void onError(String errormessage) {

        }
    });

    recyclerViewImageSlider = findViewById(R.id.recyclerViewImages);
    imageAdapter = new ImageAdapter(Profil.this, imageList, new AdapterCallback() {
        @Override
        public void onLoaded() {

        }
    });
    RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(Profil.this,LinearLayoutManager.HORIZONTAL,false);
    recyclerViewImageSlider.setLayoutManager(layoutManager);
    recyclerViewImageSlider.setHasFixedSize(true);
    recyclerViewImageSlider.setAdapter(imageAdapter);
    recyclerViewImageSlider.setOnFlingListener(new PagerSnapHelper());
    SnapHelper snapHelper = new PagerSnapHelper();
    snapHelper.attachToRecyclerView(recyclerViewImageSlider);

    imageAdapter.notifyDataSetChanged();

}

1 个答案:

答案 0 :(得分:1)

imageAdapter.notifyDataSetChanged();之后调用imageList.add(imageGetter);,因为您正在修改arraylist并且您必须让适配器知道重新加载数据集

 pullData.getData(USERNAME, "3", new Pullcallback() {
    @Override
    public void getSingleData(String data) {
        imageGetter = new ImageGetter(data);
        imageList.add(imageGetter);
        imageAdapter.notifyDataSetChanged();
    }

    @Override
    public void getMultipleData(String[] multipledatas) {

    }

    @Override
    public void onError(String errormessage) {

    }
});