首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PagerAdapter notifyDataSetChanged

PagerAdapter notifyDataSetChanged
EN

Stack Overflow用户
提问于 2018-10-13 15:47:52
回答 1查看 57关注 0票数 0

应用程序的PagerAdapter在没有调用PagerAdapter#notifyDataSetChanged的情况下更改了适配器的内容!预期适配器项目计数: 0,找到:3寻呼机id

我的Android项目中有这个错误。在这里,我使用了一个视图分页程序。我从webservices得到了字节码形式的图片。

代码语言:javascript
复制
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;
    }
}
EN

回答 1

Stack Overflow用户

发布于 2018-10-13 16:52:31

GetContacts是一个AsyncTask,在它的doInBackground方法中,您可以在下面一行中输入:

代码语言:javascript
复制
alListImage.add(sImageByte); // This makes the adapter item number equal to 3

为了避免这种情况,您可以在使用postDelayed一段时间后执行GetContacts

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

https://stackoverflow.com/questions/52790727

复制
相关文章

相似问题

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