1

在下面的代码中,我有一个选项菜单,当我选择菜单选项时,我想更新 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>

4

1 回答 1

0

我不知道为什么我不能 setText,但是在用户选择一个菜单项后显示字幕对我来说效果很好。

于 2013-05-22T01:36:22.223 回答