1

在下面的代码中,我有一个选项菜单,当我选择菜单选项时,我想更新textview。设置文本初始工作正常,不会产生错误。这里是settext不起作用的代码。我已经加入了调试语句,代码越来越受欢迎,但文本没有更新。我将箭头放在未更新的代码旁边。查看其他的textview示例,但无法弄清楚。设置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> 

+0

除此之外,您的应用还能继续正常吗?没有错误信息,冻结等...? – codeMagic

+0

@codeMagic是的,应用程序继续正常。没有错误或冻结。我假设textview没有被关注或不可更新。我发现在处理碎片时很难更新文本视图。我只需要用户知道选择了哪个菜单选项。也许有更好的方法来做到这一点。 – sowens

+0

我不确定你在做什么,但是“Toast”就足够了吗? – codeMagic

回答

0

我也没弄明白为什么我不能't setText,但在用户选择菜单项后显示字幕对我的目的很有效。