1
我無法在ViewPager中設置TextView的setText()。 這是我的主要活動課。我想更新由ViewPager製作的一系列佈局中的textView。android更改ViewPager中的textView
public class MainActivity extends Activity {
ViewPager myPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyPagerAdapter adapter = new MyPagerAdapter();
myPager = (ViewPager) findViewById(R.id.myfivepanelpager);
myPager.setAdapter(adapter);
myPager.setCurrentItem(3);
}
@Override
protected void onResume() {
super.onResume();
// this code causes the app to crash, because baseLayout is null
View baseLayout = myPager.findViewWithTag(R.layout.hoteladdress);
//TextView bubu = (TextView) baseLayout.findViewById(R.id.bubu);
//bubu.setText("TEST");
}
private class MyPagerAdapter extends PagerAdapter {
public int getCount() {
return 3;
}
public Object instantiateItem(View collection, int position) {
LayoutInflater inflater = (LayoutInflater) collection.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
int resId = 0;
switch (position) {
case 0:
resId = showHotelContact();
break;
case 1:
resId = showHotelAddress();
break;
case 2:
resId = showHotelMap();
break;
}
View view = (View) inflater.inflate(resId, null);
view.setTag(resId);
((ViewPager) collection).addView(view, 0);
return view;
}
}
public int showHotelMap()
{
int resId;
resId = R.layout.hotelmap;
return resId;
}
public int showHotelAddress()
{
int resId;
resId = R.layout.hoteladdress;
return resId;
}
public int showHotelContact()
{
int resId;
resId = R.layout.hotelcontact;
return resId;
}
}
請在onResume方法中找到我的註釋行來解釋問題。
謝謝你,但,渣油沒有定義,並且getViewAtPosition()沒有針對ViewPager對象 – avafab
我不好定義,我說,這是未經測試的:)現在嘗試 – cYrixmorten
不工作,你爲什麼不考發佈之前? – avafab