我在android中使用Master Detail Flow模板。我想在应用程序启动时以编程方式访问列表中的项目,以便设置字体大小和那里的TextViews的颜色。如果我在onResume(ItemListActivity或ItemListFragment)中编码,则ListView不为null,但3 TextViews(ListView的子项)为空。
public void updateActivity( )
{
ViewGroup leftPane = ( ViewGroup ) findViewById( R.id.item_list );
FrameLayout frameLayout = ( FrameLayout ) leftPane.getChildAt( 0 );
FrameLayout frameLayout2 = ( FrameLayout ) frameLayout.getChildAt( 1 );
ListView listView = ( ListView ) frameLayout2.getChildAt( 1 ); // listView NOT null
TextView item0 = ( TextView ) listView.getChildAt( 0 ); // item0 is NULL !!!!!
}
我在ItemListActivity中提出了这个代码作为(脏)修复,我不确定它是否会一直工作并且与设备无关:
public View onCreateView( String name, Context context, AttributeSet attrs )
{
// started is an int instance variable, initialized at 0
Log.w( "MA", "Inside Activity:onCreateView, started = " + started );
// if started <= 40 ==> item0 is NULL inside updateActivity
if( started == 41) // ==> item0 NOT null inside updateActivity
updateActivity( );
started++;
return super.onCreateView( name, context, attrs );
}
这有效但我正在寻找一个(更好)更好的解决方案;这是LogCat中的输出(它确实停在48):
似乎涉及各种生命周期方法的交错执行。必须有一种在方法内编码的方法,该方法保证在该方法执行之前已经实例化了所有的视图;我试图从Activity,FragmentActivity,ListFragment和Fragment类中覆盖很多方法;到目前为止没有运气,TextViews(ListView的子节点)在所有这些方法中都是NULL。顺便说一句,其他TextViews在启动时仍为NULL等于41,第二个在启动时等于43时变为NOT null,而当启动时等于45时第三个变为NOT null。 任何的想法?谢谢你的帮助。
......
05-16 19:27:36.460: W/MA(3155): Inside Activity:onCreateView, started = 37
05-16 19:27:36.460: W/MA(3155): Inside Activity:onCreateView, started = 38
05-16 19:27:36.460: W/MA(3155): Inside ItemListActivity:onResume
05-16 19:27:36.460: W/MA(3155): Inside ItemListFragment:onResume
05-16 19:27:36.484: W/MA(3155): Inside Activity:onCreateView, started = 39
05-16 19:27:36.484: W/MA(3155): Inside Activity:onCreateView, started = 40
05-16 19:27:36.632: W/MA(3155): Inside Activity:onCreateView, started = 41
05-16 19:27:36.632: W/MA(3155): Inside checkActivity
05-16 19:27:36.632: W/MA(3155): Inside Activity:onCreateView, started = 42
05-16 19:27:36.640: W/MA(3155): Inside Activity:onCreateView, started = 43
05-16 19:27:36.640: W/MA(3155): Inside Activity:onCreateView, started = 44
05-16 19:27:36.648: W/MA(3155): Inside Activity:onCreateView, started = 45
05-16 19:27:36.656: W/MA(3155): Inside Activity:onCreateView, started = 46
05-16 19:27:36.742: W/MA(3155): Inside Activity:onCreateView, started = 47
05-16 19:27:36.742: W/MA(3155): Inside Activity:onCreateView, started = 48
答案 0 :(得分:0)
要实现此目的,您必须实现自定义适配器。从您的onCreate
活动中创建CustomAdapter
的对象,并在list of data
中传递constructor
。
在CustomAdapter
内,你必须在每行的膨胀布局中覆盖getView()
(在这里哟)你必须为findViewById(id)
调用textView()
并根据你的数据setFont()
的{{1}}和setColor()
。