如何使用ArrayList在viewpager中设置数据?

时间:2017-03-30 12:41:07

标签: java android arraylist android-viewpager

这里,在这个viewpageradapter类用于设置数据,如何在viewpager中设置数据。我可以尝试实现,但我不知道如何实现这一点。我还添加了我的PhotoviewAdater和PhotoActivity。 PLZ解决了这个问题。

HAL_LCD_Init(&hlcd);
uint32_t counter = 0;
for(counter = LCD_RAM_REGISTER0; counter <= LCD_RAM_REGISTER15; counter++)
{
    HAL_LCD_Write(&hlcd,counter,RegMask,Data);
}
HAL_LCD_UpdateDisplayRequest(&hlcd);

光活性:

public class PhotoViewAdapter extends PagerAdapter {

Context context;
ArrayList<Model_image> all_folder = new ArrayList<>();
int int_position;
LayoutInflater inflater;

public PhotoViewAdapter(Context context, ArrayList<Model_image> all_folder, int int_position) {
    this.context=context;
    this.all_folder=all_folder;
    this.int_position=int_position;
}

@Override
public int getCount() {
    return all_folder.get(int_position).getAll_imagepath().size();
}

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

@Override
public Object instantiateItem(ViewGroup container, int position) {
    ImageView imageView;
    inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View viewLayout = inflater.inflate(R.layout.activity_photo_view_adapter, container,false);

    imageView = (ImageView) container.findViewById(R.id.im_page);


     try {
        imageView.setImageResource(Integer.parseInt((all_folder.get(position).getAll_imagepath().get(int_position))));
    } catch (Exception e) {
        e.printStackTrace();
    }
    return viewLayout;
}
}

MODEL_IMAGE:

public class PhotoActivity extends AppCompatActivity {

ViewPager viewPager;
int int_position;
PhotoViewAdapter photoViewAdapter;

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

    int_position = getIntent().getIntExtra("pos",0);
    viewPager = (ViewPager) findViewById(R.id.imagepage);
    photoViewAdapter = new PhotoViewAdapter(getApplicationContext(), ImageActivity.all_images, int_position);
    viewPager.setAdapter(photoViewAdapter);
}
}

enter image description here

2 个答案:

答案 0 :(得分:0)

更改此

 imageView.setImageResource(all_folder[position]);

 imageView.setImageResource(all_folder.get(position));

all_folder被定义为Arraylist而不是array

all_folder.get(position)返回一个Model_image对象。 要在imageView中设置图像,您需要一个图像参考。

我不知道你要设置哪个图像。

如果要从all_imagepath设置图像,请尝试执行

imageView.setImageResource(all_folder.get(position).getAll_imagepath.get(<position of the required image>));

答案 1 :(得分:0)

setImageResource用于设置apk资源中的图像。

而是使用imageView.setImageBitmap(BitmapFactory.decodeFile(all_folder.get(position).getPath()));

目前尚不清楚Model_image类的唯一路径是什么,因此您需要定义一个getPath()方法。