首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在PageAdapter中添加页面

问在PageAdapter中添加页面
EN

Stack Overflow用户
提问于 2013-11-13 20:59:59
回答 1查看 144关注 0票数 0

我有以下问题。我有一个应该在background...first中动态加载页面(图片)内容的PagerAdapter,viewpager加载了单个页面。然后,异步任务从图片中获取所有UserID,用户have...then PagerAdapter应该创建与图片要发送到load..and一样多的默认页面,最后图片应该通过异步任务加载。

MainActivity:

代码语言:javascript
复制
public class MainActivity extends Activity {
String[] urls;
ArrayList<Bitmap> pics = new ArrayList<Bitmap>();
GalleryPagerAdapter mAdapter = new GalleryPagerAdapter(this, urls, pics);

public void savePicUrls(String result){
    String[] urls = result.toString().split(",");

    for(int i=0; i<urls.length; i++){

// This asyncTask starts in his onPostExecute method, the method "savePic" in the MainActivity with a single Bitmap
        List_getImage test = new List_getImage(urls[i], this);
        test.execute();
    }
}

public void savePic(Bitmap result){
    pics.add(result);
    //mAdapter = new GalleryPagerAdapter(this, urls, pics);
    mAdapter.notifyDataSetChanged();    
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.viewpager_layout_activity);
    GalleryPagerAdapter adapter = new GalleryPagerAdapter(this, urls, pics);
    ViewPager myPager = (ViewPager) findViewById(R.id.pager);
    myPager.setAdapter(adapter);
    myPager.setCurrentItem(0);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
}

PagerAdapter:

代码语言:javascript
复制
public class GalleryPagerAdapter extends PagerAdapter {
String USID = "2";
int count=1;
String url="http://193.171.131.105/prein/bluepeople/SucheFotos_PREIN.php";
MainActivity actRef;
String[] urls;
ArrayList<Bitmap> pics;

public GalleryPagerAdapter(MainActivity actRef, String[] urls, ArrayList<Bitmap> pics) {
    this.actRef = actRef;
    this.urls = urls;
    this.pics = pics;
}

public void setCount(int PageCount){
    count = count + PageCount;
    System.out.println(count);
}

public int getCount() {
    return pics.size() + 1;
}

public Object instantiateItem(View collection, int pos) {

    LayoutInflater inflater = (LayoutInflater) collection.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View page = inflater.inflate(R.layout.fragment_screen_slide_page, null);
    ImageView iv = (ImageView) page.findViewById(R.id.up_pub_iv_viewpager);
    if(pos == 0){
        iv.setImageResource(R.drawable.ic_launcher);
    }
        List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>();
        nameValuePair.add(new BasicNameValuePair("USID", USID));

// This asyncTask starts in his onPostExecute method, the method "savePicUrls" in the     MainActivity with a String with all the URLs of the pictures
        UserProfilePub_getImageUrl upp_getImageUrls = new UserProfilePub_getImageUrl(nameValuePair, url, actRef, iv);
        upp_getImageUrls.execute();

    if(pics.size() != 0){
        iv.setImageBitmap(pics.get(pos));
    }

    ((ViewPager) collection).addView(page, 0);
    return page;


}

@Override
public void destroyItem(View arg0, int arg1, Object arg2) {
    ((ViewPager) arg0).removeView((View) arg2);
}

@Override
public boolean isViewFromObject(View arg0, Object arg1) {
    return arg0 == ((View) arg1);
}

@Override
public Parcelable saveState() {
    return null;
}
}

我认为一切都应该工作的doesnt..the应用程序崩溃与一个"IllegalStateException...the pages的内容得到更改而不调用.notifyDataSetChanged“,但我确实是在"savePic”方法做这件事...我不知道我的错误在哪里is...at最少有任何其他方法来获得一个viewpager,它动态加载图片在后台添加默认页面之前?提前感谢!!

EN

回答 1

Stack Overflow用户

发布于 2013-11-13 21:33:46

在哪里调用savePic()?

如果您从ASyncTask的doInBackground()调用它,这可能就是问题所在。错误消息有点奇怪,但我想我在适配器从非UI线程更改时看到了这条消息。

如果是这种情况,请在doInBackground()中使用publishProgress(),并在异步任务的onProgressUpdate()中调用savePic。(或在doInbackground中收集所有图片,并在onPostExecute中处理添加)

如果不是这样,请让我知道您在哪里调用savePic

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19954489

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档