2014-11-02 64 views
0

我想設置起始位置我的尋呼機。當我使用Asynctask讀取適配器ArrayList時,我無法設置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
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); 
      }