從類的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;
沒有功能:TextView t =(TextView)v.findViewB YID(R.id.ss); t.setText(s); – rovi
如果你把錯誤的堆棧跟蹤輸出,這將是很好的。 –
錯誤是:java.lang.NullPointerException:試圖調用空對象引用的虛擬方法'void android.widget.TextView.setText(java.lang.CharSequence)' – rovi