2016-03-03 114 views
0

從類的onListItemClick ListFragment方法更改TextView的文本。 這是該方法:從onListItemClick更改「TextView」的文本

@Override 
    public void onListItemClick(ListView l, View v, int position, long id) { 

     String selectedValue = (String) getListAdapter().getItem(position); 

     Descripcion_Noticias myDetailFragment = new Descripcion_Noticias(); 

      FragmentTransaction fragmentTransaction = 
        getActivity().getFragmentManager().beginTransaction(); 

     myDetailFragment.setText(v,selectedValue); 

     fragmentTransaction.replace(R.id.fragment, myDetailFragment); 
     fragmentTransaction.addToBackStack(null); 
      fragmentTransaction.commit(); 


    } 

XML是:

在XML是在新的片段,延伸片段。


的XML:

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


    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="New Text" 
     android:id="@+id/ss" 
     android:layout_gravity="center_horizontal" /> 
</LinearLayout> 

ListFragment的方法:

@Override 
    public void onListItemClick(ListView l, View v, int position, long id) { 

     String selectedValue = (String) getListAdapter().getItem(position); 
     CharSequence s =selectedValue; 
     TextView t = (TextView) getActivity().findViewById(R.id.ss); 
     t.setText(s); 

     Descripcion_Noticias myDetailFragment = new Descripcion_Noticias(); 

      FragmentTransaction fragmentTransaction = 
        getActivity().getFragmentManager().beginTransaction(); 

     fragmentTransaction.replace(R.id.fragment, myDetailFragment); 
     fragmentTransaction.addToBackStack(null); 
      fragmentTransaction.commit(); 


    } 

The class primary of XMl: 

@Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     View v = inflater.inflate(R.layout.descripcion_noticias, null); 
     return v; 

    } 

方法:

@Override 
public void onListItemClick(ListView l, View v, int position, long id) { 

    String selectedValue = (String) getListAdapter().getItem(position); 
    CharSequence s =selectedValue; 
    mTextView.setText(s); 

    Descripcion_Noticias myDetailFragment = new Descripcion_Noticias(); 

    FragmentTransaction fragmentTransaction = 
      getActivity().getFragmentManager().beginTransaction(); 

    fragmentTransaction.replace(R.id.fragment, myDetailFragment); 
    fragmentTransaction.addToBackStack(null); 
    fragmentTransaction.commit(); 

} 

}

類片段:

public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    View v = inflater.inflate(R.layout.descripcion_noticias, null); 
    mTextView = (TextView) getActivity().findViewById (R.id.ss); 
    return v; 

} 

的三三兩兩類有private TextView mTextView;

+0

沒有功能:TextView t =(TextView)v.findViewB YID(R.id.ss); t.setText(s); – rovi

+0

如果你把錯誤的堆棧跟蹤輸出,這將是很好的。 –

+0

錯誤是:java.lang.NullPointerException:試圖調用空對象引用的虛擬方法'void android.widget.TextView.setText(java.lang.CharSequence)' – rovi

回答

0

裏面的onCreateView方法,保存的全局引用您的TextView:

mTextView = (TextView) v.findViewById(R.id.ss); 

,然後訪問它您的onListItemClick爲:

mTextView.setText(s); 

編輯

在類的頂部,你就把:

private TextView mTextView; 

在你的監聽方法:

@Override 
public void onListItemClick(ListView l, View v, int position, long id) { 

    String selectedValue = (String) getListAdapter().getItem(position); 
    CharSequence s =selectedValue; 
    mTextView.setText(s); 

    Descripcion_Noticias myDetailFragment = new Descripcion_Noticias(); 

     FragmentTransaction fragmentTransaction = 
       getActivity().getFragmentManager().beginTransaction(); 

    fragmentTransaction.replace(R.id.fragment, myDetailFragment); 
    fragmentTransaction.addToBackStack(null); 
     fragmentTransaction.commit(); 


} 

的XML類初級:

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
    View v = inflater.inflate(R.layout.descripcion_noticias, null); 
    mTextView = (TextView)v.findViewById(R.id.ss); 
    return v; 

} 
+0

添加行:TextView mTextView =(TextView)getActivity()。findViewById(R.id.ss); And methdo「onListItemClick」: TextView t =(TextView)getActivity()。findViewById(R.id.ss); t。的setText(一個或多個); – rovi

+0

將其添加爲全局參考。在頂部你需要有這樣的: 私人TextView mTextView –

+0

@rovi看到我的編輯。 –