In ViewPagerAdapter.classI have 2 layout(whereami.xml and mapview.xml), and use LayoutInflater to inflate each view inside instantiateItem(), normally i should set everything(TextView,ImageView etc. according to xml) inside instantiateItem(). But in my case, i really need to setText or Background color at onCreate() of ViewPagerActivity which extend MapActivity
So, Are there anyway to set them in onCreate()?
P.S. I try to debug program and found that the process won’t call instantiateItem() until it get out of the onCreate() method. So,The View won’t create yet, and programe force close every time when i try to setText because View is Null.
ViewPagerActivity
public class ViewPagerActivity extends MapActivity {
private ViewPagerAdapter adapter;
private ViewPager pager;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
adapter = new ViewPagerAdapter(this);
pager = (ViewPager) findViewById(R.id.viewpager);
TitlePageIndicator indicator = (TitlePageIndicator) findViewById(R.id.indicator);
pager.setAdapter(adapter);
indicator.setViewPager(pager);
//LinearLayout bgcolor = (LinearLayout)findViewById(R.id.mainlayout);
//bgcolor.setBackgroundColor(Color.Black);
//TextView text1 = (TextView)findViewById(R.id.text1);
//text1.setText("somevalue");
}
}
ViewPagerAdapter
public class ViewPagerAdapter extends PagerAdapter implements TitleProvider {
private final Context context;
private MapView mapView;
private MapController mapController;
private View view;
public ViewPagerAdapter(Context context) {
}
private static String[] titles = new String[] { "Page1", "Map" };
@Override
public Object instantiateItem(View pager, final int position) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (position == 0) {
view = inflater.inflate(R.layout.whereami, null);
((ViewPager) pager).addView(view, 0);
}
if (position == 1) {
view = inflater.inflate(R.layout.my_map_activity, null);
((ViewPager) pager).addView(view, 0);
mapView = (MapView) view.findViewById(R.id.mapview);
mapController = mapView.getController();
mapController.setZoom(14);
}
return view;
}
}
whereami.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainlayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/text1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="8dp"
android:textSize="16sp" >
</TextView>
</LinearLayout>
If you know what should be in the TextView in
onCreate()why not pass it through the constructor of the Adapter?With:
and: