我无法设置起始位置寻呼机

时间:2014-11-02 09:50:51

标签: android android-viewpager

我想设置起始位置我的寻呼机。当我使用Asynctask读取ArrayList for Adapter时,我无法设置Adaper的起始位置。我得到了第一页。 当我不使用asynctask读取ArrayList代码运行正常并且开始位置设置正确。 谢谢你的帮助。

PagerActivity.java

public class PagerActivity extends Activity implements OnPagerItemSelected  {

ViewPager viewPager;
ArrayList<Bitmap> bitmapArray=null;
ArrayList<String> textArray=null;
ArrayList<String> urlArray=null;  
int pagexi;


  @Override
  public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_reklamapager);

        viewPager = (ViewPager) findViewById(R.id.view_pager);

        new readAsync().execute();

        int pagexi = 2;
        viewPager.setCurrentItem(pagexi);


        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
            int stranai = position + 1;
            String strana=stranai + "";

            Toast.makeText(PagerActivity.this, strana, Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        }

        @Override
        public void onPageScrollStateChanged(int state) {
        }
    });

  }//end oncreate

  public void pagerItemSelected() {
        finish();
    }

  /**
     * Background Async Task to  nacitaj textyoffline
     * */
    class readAsync extends AsyncTask<String, String, String> {


        @Override
        protected void onPreExecute() {
            super.onPreExecute();

        }


        protected String doInBackground(String... args) {


                textArray = new ArrayList<String>();
                textArray.add("item 0;page 1");
                textArray.add("item 1;page 2");
                textArray.add("item 2;page 3");

            return null;
        }


        protected void onPostExecute(String file_url) {

            MyPagerAdapter adapter = new MyPagerAdapter(PagerActivity.this, textArray, urlArray, bitmapArray, PagerActivity.this );
            viewPager.setAdapter(adapter);

        }
        //end onpost
    }
    //end async

}//end activity

MyPagerAdapter.java

public class MyPagerAdapter extends PagerAdapter {


    Context context;
    OnPagerItemSelected mListener;
    ArrayList<String> textArray;
    ArrayList<String> urlArray;
    ArrayList<Bitmap> bitmapArray;
    ImageView imageView;
    TextView textreklama1;
    Button btnZoznam;
    Button btnUprav;
    public Activity activity;
    String firmax;
    String adresarx;


    MyPagerAdapter(Context context, ArrayList<String> textArray, ArrayList<String> urlArray, ArrayList<Bitmap> bitmapArray, OnPagerItemSelected listener){
        this.context=context;
        this.textArray=textArray;
        this.urlArray=urlArray;
        this.bitmapArray=bitmapArray;

        this.mListener = listener;
    }

    @Override
    public int getCount() {

        return textArray.size();
    }

    public Object instantiateItem(ViewGroup collection, int position) {
         LayoutInflater inflater = (LayoutInflater) collection.getContext()
         .getSystemService(Context.LAYOUT_INFLATER_SERVICE);


         View view = inflater.inflate(R.layout.reklamator_new, null);


            String indexx = textArray.get(position).toString();
            String delims2 = "[;]+";
            String[] riadokxxx = indexx.split(delims2);
            String ucex =  riadokxxx[0].trim();
            String dokx =  riadokxxx[1].trim();



            EditText inputUce = (EditText) view.findViewById(R.id.inputUce);
            inputUce.setText(ucex);                 
            EditText inputDok = (EditText) view.findViewById(R.id.inputDok);
            inputDok.setText(dokx);                 



         ((ViewGroup) collection).addView(view, 0);
         return view;
         }

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

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

         public interface OnPagerItemSelected {
             void pagerItemSelected();
         }



//end of adapter
}

1 个答案:

答案 0 :(得分:1)

Set Cureent page in post after you have set adapter


 protected void onPostExecute(String file_url) {

                MyPagerAdapter adapter = new MyPagerAdapter(PagerActivity.this, textArray, urlArray, bitmapArray, PagerActivity.this );
                viewPager.setAdapter(adapter);
     viewPager.setCurrentItem(pagexi);
            }