从类的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