我正在使用SlidingMenu,我想更新菜单片段中包含的TextView。
我的问题是,当我调用setText()时,文本不会改变,但如果我调用Log.i(“”。getText())之后它会打印新文本!
我尝试调用mytextview.invalidate()而没有任何结果
public class SlidingMenuListFragment extends SherlockListFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
this.container = container;
return inflater.inflate(R.layout.menu_slide_menu, container, false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
adapter = new SlidingMenuListAdapter(getActivity(),
R.layout.menu_slide_menu_item, MenuItems);
Username = (TextView) container.findViewById(R.id.username);
Username.setText("NEW VALUE");
Log.i("ASD", Username.getText().toString()); // prints the new value!
setListAdapter(adapter);
}
}
布局:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/sliding_menu_list"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f2f2f2"
android:orientation="vertical" >
...
<RelativeLayout
android:id="@+id/username_layout"
android:layout_width="match_parent"
android:layout_height="@dimen/abs__action_bar_default_height"
android:background="@drawable/actionbar" >
...
<TextView
android:id="@+id/username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/textView1"
android:text="asd"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#ffffff"
android:textStyle="bold" />
</RelativeLayout>
<ListView
android:id="@+id/menu_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="#f2f2f2"
android:dividerHeight="0dip"
android:textSize="32sp" />
</LinearLayout>
</FrameLayout>
答案 0 :(得分:0)
改变如下:
Username = (TextView) getView().findViewById(R.id.username);