如何在android中的CardView Adapter中使用Database Helper类

时间:2016-10-20 06:51:31

标签: java android android-viewpager adapter android-cardview

我使用ViewPagerViewPager设置CardView显示有关人名和人物图片的卡片,以便我可以在适配器中使用帮助程序类并将人物图像设置为卡也名,所以怎么用这个.. 。我是Android编程新手,请解决此问题。

CardPagerAdapter

 private List<CardView> mViews;
private List<String> mData;
private float mBaseElevation;

public CardPagerAdapter() {

    mData = new ArrayList<>();
    mViews = new ArrayList<>();

    for (int i = 0; i < 5; i++) {
        mData.add("");
        mViews.add(null);
    }
}

public float getBaseElevation() {
    return mBaseElevation;
}

@Override
public CardView getCardViewAt(int position) {
    return mViews.get(position);
}

@Override
public int getCount() {
    return mData.size();
}

@Override
public boolean isViewFromObject(View view, Object object) {
    return view == object;
}

@Override
public Object instantiateItem(ViewGroup container, int position) {
    View view = LayoutInflater.from(container.getContext())
            .inflate(R.layout.viewpager_adapter, container, false);
    container.addView(view);
    CardView cardView = (CardView) view.findViewById(R.id.cv);
    ImageView imageView = (ImageView) view.findViewById(R.id.ivPeopelImage);
    imageView.setImageResource(R.drawable.ak_image);

    if (mBaseElevation == 0) {
        mBaseElevation = cardView.getCardElevation();
    }

    cardView.setMaxCardElevation(mBaseElevation * MAX_ELEVATION_FACTOR);
    mViews.set(position, cardView);
    return view;
}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
    container.removeView((View) object);
    mViews.set(position, null);
}

浏览类

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_alertme);

    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

    mViewPager = (ViewPager) findViewById(R.id.viewPager);
    mCardAdapter = new CardPagerAdapter();
    mCardShadowTransformer = new ShadowTransformer(mViewPager, mCardAdapter);
    mViewPager.setAdapter(mCardAdapter);
    mViewPager.setPageTransformer(false, mCardShadowTransformer);
    mViewPager.setOffscreenPageLimit(3);
}

1 个答案:

答案 0 :(得分:0)

1)创建一个POJO类(作为您的要求)示例:yourclass

2)为您的类创建对象并存储在列表中,并将其存储在列表中。 如下。

ArrayList listObject = new ArrayList();

3)mCardAdapter = new CardPagerAdapter(listObject);