应用程序的PagerAdapter
更改了适配器的内容,而没有调用PagerAdapter#notifyDataSetChanged
!预期的适配器项目数:0,找到的:3 Pager
ID
我的Android项目中出现此错误。在这里,我为此使用视图寻呼机。我从网络服务获得图像作为字节码。
public class MainActivity extends AppCompatActivity {
private String TAG = MainActivity.class.getSimpleName();
private ProgressDialog pDialog;
ArrayList<String> alListImage;
private static ViewPager mPager;
private static int currentPage = 0;
private static int NUM_PAGES = 0;
private static final Integer[] IMAGES= {R.drawable.one,R.drawable.two,R.drawable.three,R.drawable.one,R.drawable.two,R.drawable.three,};
private ArrayList<String> ImagesArray = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
alListImage=new ArrayList<String>();
new GetContacts().execute();
init();
}
private void init() {
mPager = (ViewPager) findViewById(R.id.pager);
mPager.setAdapter(new Adapter(MainActivity.this,alListImage));
// Auto start of viewpager
final Handler handler = new Handler();
final Runnable Update = new Runnable() {
public void run() {
if (currentPage == alListImage.size()) {
currentPage = 0;
}
mPager.setCurrentItem(currentPage++, true);
}
};
Timer swipeTimer = new Timer();
swipeTimer.schedule(new TimerTask() {
@Override
public void run() {
handler.post(Update);
}
}, 3000, 3000);
}
private class GetContacts extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Void doInBackground(Void... arg0) {
HttpHandler sh = new HttpHandler();
String url = "http://webapirestro.appro.com.sg/api/Category/GetCategory?CompanyId=771416A2-56C6-46A5-8EB5-655E4361A22A";
//String url = "http://webapirestro.appro.com.sg/api/TvImage/Gettv_image?CompanyId=771416A2-56C6-46A5-8EB5-655E4361A22A";
// Making a request to URL and getting response
final String jsonStr = sh.makeServiceCall(url);
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
// Getting JSON Array node
JSONArray ListData = jsonObj.getJSONArray("ListData");
// Looping through All Contacts
for (int i = 0; i < ListData.length(); i++) {
JSONObject c = ListData.getJSONObject(i);
String sImageByte = c.getString("ImageByte");
String sCatname = c.getString("CategoryName");
if(sImageByte.equals(false)){
sImageByte = "ABC";
}
// Adding contact to contact list
alListImage.add(sImageByte);
}
}
catch (final JSONException e) {
Log.e(TAG, "JSON parsing error: " + e.getMessage());
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
"Json parsing error: " + e.getMessage(),
Toast.LENGTH_LONG)
.show();
}
});
}
}
else {
Log.e(TAG, "Couldn't get json from server.");
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
"Couldn't get json from server. Check LogCat for possible errors!",
Toast.LENGTH_LONG)
.show();
}
});
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// Dismiss the progress dialog
if (pDialog.isShowing())
pDialog.dismiss();
/**
* Updating parsed JSON data into ListView
* */
}
}
}
public class Adapter extends PagerAdapter {
private ArrayList<String> IMAGES;
private LayoutInflater inflater;
private Context context;
public Adapter(Context context,ArrayList<String> IMAGES) {
this.context = context;
this.IMAGES=IMAGES;
inflater = LayoutInflater.from(context);
notifyDataSetChanged();
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
@Override
public int getCount() {
return IMAGES.size();
}
@Override
public Object instantiateItem(ViewGroup view, int position) {
View imageLayout = inflater.inflate(R.layout.single_pager, view, false);
assert imageLayout != null;
final ImageView imageView = (ImageView) imageLayout
.findViewById(R.id.image);
for(int i=0;i<IMAGES.size();i++) {
byte[] decode = Base64.decode(IMAGES.get(i), Base64.DEFAULT);
Bitmap bitmap = BitmapFactory.decodeByteArray(decode, 0, decode.length);
imageView.setImageBitmap(bitmap);
}
view.addView(imageLayout, 0);
return imageLayout;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view.equals(object);
}
@Override
public void restoreState(Parcelable state, ClassLoader loader) {
}
@Override
public Parcelable saveState() {
return null;
}
}
答案 0 :(得分:0)
GetContacts
是一个AsyncTask,在其doInBackground
方法中,您输入了以下行:
alListImage.add(sImageByte); // This makes the adapter item number equal to 3
为避免这种情况,您可以在一段时间后使用postDelayed执行GetContacts
。