我有以下问题。我有一个PagerAdapter
,应该在后台动态加载页面(图片)的内容...首先,viewpager加载一个页面。然后asynctask获取特定UserID所有来自用户拥有的图片的URL ...然后PagerAdapter应该创建尽可能多的默认页面,因为图片将要加载..最后图片应该通过asynctask加载..
MainActivity:
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:
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;
}
}
我认为一切都应该正常工作......但它并没有...应用程序崩溃了一个&#34; IllegalStateException ... Pagerscontent在没有调用.notifyDataSetChanged&#34; 但我确实在&#34; savePic&#34;方法...我不知道我的错误在哪里...至少还有其他方法可以获得一个viewpager,它在后台添加默认页面时动态加载图片吗? 在此先感谢!!
答案 0 :(得分:0)
你在哪里调用savePic()?
如果从ASyncTask的doInBackground()调用它,则可能是问题所在。错误消息有点奇怪,但我认为当从非UI线程更改适配器时,我看到了此消息。
如果是这种情况,请在doInBackground()中使用publishProgress(),并在AsyncTask的onProgressUpdate()中调用savePic。 (或收集doInbackground中的所有图片并处理onPostExecute中的添加)
如果不是这种情况,请告诉我你在哪里调用savePic