简体   繁体   中英

get data from public class from static java

I try to create dynamic ViewPager content output. I complete this with hardcoded data, but can't with variable.

First I create activity and get data from previous activity and resources.

public class result extends FragmentActivity {

private MyAdapter mAdapter;
private ViewPager mPager;
int[] uan;
String [] categoryQuest;

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

    mAdapter = new MyAdapter (getSupportFragmentManager());
    mPager = (ViewPager)findViewById(R.id.pager);
    mPager.setAdapter(mAdapter);

    Resources testCat = getResources();
    categoryQuest = testCat.getStringArray(R.array.category);

    Bundle bndResult = this.getIntent().getExtras();
    uan = bndResult.getIntArray("strDef");

}

Secondary, I try to get uan array data and put it into fragment.

public static class MyAdapter extends FragmentPagerAdapter {

public MyAdapter (FragmentManager fm) {
        super (fm);
    }

    @Override
    public int getCount()  {
        return 2;
    }

    @Override
    public Fragment getItem(int position) {
       switch (position) {
           case 0:
               return new detail(R.string.A1, uan[0]);
           case 1:
               return new detail(R.string.B1, uan[1]);
           default:
               return null;
       }
    }

Explain me, how to get access to UAN array from public void onCreate from public static class MyAdapter.

Create a method on your adapter and pass the data into it:

private int[] mUan;

public void setUan(int[] uan) {
    mUan = uan;
}

Then change getItem to use mUan and populate the data in your activity's onCreate.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM