设置TextView不会更改

时间:2013-05-21 18:22:01

标签: android android-fragments textview android-listfragment

在下面的代码中,我有一个选项菜单,当我选择菜单选项时,我想更新textview。最初设置文本工作正常,不会生成错误。这是settext不起作用的代码。我已经放入调试语句,代码被命中,但文本没有更新。我把箭头放在没有更新的代码旁边。查看其他textview示例,但无法弄清楚。

        @Override
public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {
        case R.id.menu_item_Bflat:
            if (VERBOSE) Log.v(TAG, "+++ menu_item_Bflat +++");
            // BFLAT = 1
            mKeySelected = BFLAT;
    >>>>        mChapterSelected.setText("B Flat"); 
            return true;
        case R.id.menu_item_Eflat:
            // EFLAT = 2
            mKeySelected = EFLAT;
    >>>>        mChapterSelected.setText("E Flat");
            return true;
        default:
            return super.onOptionsItemSelected(item);
    } // switch
} // onOptionsItemSelected

如果需要,以下是我的其余代码:

    public class ChapterListFragment extends ListFragment {
private static final boolean VERBOSE = true;
private ArrayList<Chapters> mChapters;
private static final String TAG = "ChapterListFragment";
private static final int BFLAT = 1;
private static final int EFLAT = 2;

private TextView mChapterSelected;
private View mView;
private int mKeySelected;   

@Override
public void onCreate(Bundle savedInstanceState) {
    if (VERBOSE) Log.v(TAG, "+++ onCreate +++");
    super.onCreate(savedInstanceState); 
    // Must explicitly tell Fragment Manager that the fragment should receive a call to
    // onCreateOptionsMenu().
    setHasOptionsMenu(true);

    // Get the view for the Fields
    if (mView == null) {
        mView = getActivity().getLayoutInflater()
                .inflate(R.layout.activity_improvisation, null);
    }

    getActivity().setTitle(R.string.chapters_title);
    mChapters = ChapterList.get(getActivity()).getChapters();

    ChapterAdapter adapter = new ChapterAdapter(mChapters);
    setListAdapter(adapter);        

    mChapterSelected = (TextView) mView.findViewById(R.id.chapter_selected);
    mChapterSelected.setText("Chapter Selected Here");          
}

@Override
public void onListItemClick(ListView l, View v, int position, long id) {             
    // Get the chapter from the adapter
    Chapters c = ((ChapterAdapter)getListAdapter()).getItem(position);

    //Start ImprovisationActivity
    Intent i = new Intent(getActivity(), ImprovisationActivity.class);
    i.putExtra(ChapterFragment.EXTRA_CHAPTER_ID, c.getId());
    i.putExtra(ChapterFragment.EXTRA_KEYSELECTED, mKeySelected);
    startActivity(i);
}   


@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    inflater.inflate(R.menu.fragment_chapters_list, menu);
}

这是XML文件

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<LinearLayout 
    android:layout_weight="2" 
    android:layout_height="fill_parent" 
    android:layout_width="match_parent" 
    >
<VideoView 
    android:id="@+id/video_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    />
</LinearLayout> 

<LinearLayout 
    android:layout_weight="1" 
    android:layout_height="fill_parent" 
    android:layout_width="fill_parent"
    android:orientation="vertical"  >  
    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:orientation="horizontal" >

        <TextView 
           android:id="@+id/chapter_selected"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:gravity = "center"
           android:textStyle="bold"
           android:paddingLeft="4dp"
           android:paddingRight="4dp"
           android:text="Chapter Selection"
    />                      

    </LinearLayout>

<FrameLayout 
          android:id="@+id/fragmentContainer"
          android:layout_height="match_parent"
          android:layout_width="match_parent" 
          />
</LinearLayout>

1 个答案:

答案 0 :(得分:0)

我没有找到为什么我无法设置文本,但在用户选择菜单项后显示字幕非常适合我的目的。