如何从ArrayList检索数据并将其设置为ViewPager

时间:2019-01-26 15:35:22

标签: android arraylist android-viewpager

如何从ArrayList将数据设置为Viewpager, 现在工作正常, 任何人都可以毫无顾忌地使用, 谢谢,所有来这里的人, 并支持我解决问题; 其经过编辑且完全有效的代码;;; 我也看到了 这是我的ViewPagerAdapter代码;

public class ViewPagerAdapter extends PagerAdapter  {
Context mContext;
LayoutInflater mLayoutInflater;
private Intent intent;
ArrayList<WholeModel> wholeModels;
int int_position=1;




public ViewPagerAdapter(Context context, ArrayList<WholeModel> wholeModels) {
    this.mContext = context;
   this.wholeModels=wholeModels;

    mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}

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

@Override
public boolean isViewFromObject(View view, Object object) {

    return view ==  object;
}

@Override
public Object instantiateItem(ViewGroup container, int position) {




    View itemView = mLayoutInflater.inflate(R.layout.whole_model,container,  false);
    TextView modelText = itemView.findViewById(R.id.modelText);
    TextView modelTitle=itemView.findViewById( R.id.modelTitle );
    ImageView modelImage=itemView.findViewById( R.id.modelImage );

      modelText.setText( wholeModels.get( int_position ).getWholeString() );
       modelTitle.setText(  wholeModels.get( int_position ).getUserTitle() );
     //  modelImage.setImageResource( wholeModels.get( position ) );
    modelImage.setImageResource( wholeModels.get( int_position ).getImageId() );


     container.add(itemView);
    return itemView;
}


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

这是我的活动代码

public class UserScreen extends AppCompatActivity {


private ViewPager viewPager;
private ViewPagerAdapter mViewPagerAdapter;
ArrayList<WholeModel> wholeModels;
private Button back,next,share,whatsapp;
private Context mContext;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate( savedInstanceState );
    setContentView( R.layout.activity_user_screen );
    mContext=this;
   /////////wholeModels
    wholeModels= new ArrayList<>();

    ///
    wholeModels.add( new WholeModel( R.string.hamad,R.drawable.kaba,R.string.poetry ) );
    wholeModels.add( new WholeModel( R.string.hamad,R.drawable.kaba,R.string.poetry ) );
    wholeModels.add( new WholeModel( R.string.hamad,R.drawable.kaba,R.string.poetry ) );
    wholeModels.add( new WholeModel( R.string.hamad,R.drawable.kaba,R.string.poetry ) );
    wholeModels.add( new WholeModel( R.string.hamad,R.drawable.kaba,R.string.poetry ) );
    wholeModels.add( new WholeModel( R.string.hamad,R.drawable.kaba,R.string.poetry ) );
    wholeModels.add( new WholeModel( R.string.hamad,R.drawable.kaba,R.string.poetry ) );
    wholeModels.add( new WholeModel( R.string.hamad,R.drawable.kaba,R.string.poetry ) );
    wholeModels.add( new WholeModel( R.string.hamad,R.drawable.kaba,R.string.poetry ) );
    wholeModels.add( new WholeModel( R.string.hamad,R.drawable.kaba,R.string.poetry ) );
    wholeModels.add( new WholeModel( R.string.hamad,R.drawable.kaba,R.string.poetry ) );
    wholeModels.add( new WholeModel( R.string.hamad,R.drawable.kaba,R.string.poetry ) );
    wholeModels.add( new WholeModel( R.string.hamad,R.drawable.kaba,R.string.poetry ) );
    wholeModels.add( new WholeModel( R.string.hamad,R.drawable.kaba,R.string.poetry ) );

    mViewPagerAdapter = new ViewPagerAdapter(this,wholeModels);

    viewPager=findViewById( R.id.viewPager );

     viewPager.setAdapter( mViewPagerAdapter );
     back=findViewById( R.id.back );

}

@Override
public void onBackPressed() {
    finish();
}}

这是WholeModel

public class WholeModel {
private int wholeString;
private int imageId;
private int userTitle;

public  WholeModel( int userTitle, int imageId,int mWholeString){
    this.userTitle = userTitle;
    this.wholeString=mWholeString;
    this.imageId = imageId;

}

public int getWholeString() {
    return wholeString;
}

public void setWholeString(int wholeString) {
    this.wholeString = wholeString;
}

public int getImageId() {
    return imageId;
}

public void setImageId(int imageId) {
    this.imageId = imageId;
}

public int getUserTitle() {
    return userTitle;
}

public void setUserTitle(int userTitle) {
    this.userTitle = userTitle;
}}

0 个答案:

没有答案