Android 文本视图文本未更新
我正在尝试将一个Android 文本视图文本未更新,android,textview,Android,Textview,我正在尝试将一个片段中的文本视图从另一个片段中更改。 我试图通过与活动沟通来做到这一点。 但是,我注意到,TextView文本已更改,但没有与新文本一起显示 我有ProductFragment,其中包含产品类别的listview。当执行列表中的onItemClick()时,我尝试显示只包含TextView的SecondFragment。然后,我尝试更新在ProductFragment中的listview中单击的类别的文本main活动是片段活动 这是我的密码: public class Secon
片段
中的文本视图
从另一个片段
中更改。
我试图通过与活动沟通来做到这一点。
但是,我注意到,TextView
文本已更改,但没有与新文本一起显示
我有ProductFragment
,其中包含产品类别的listview
。当执行列表中的onItemClick()
时,我尝试显示只包含TextView
的SecondFragment
。然后,我尝试更新在ProductFragment
中的listview
中单击的类别的文本main活动
是片段活动
这是我的密码:
public class SecondFragment extends Fragment {
private TextView tv;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.second_fragment, container, false);
tv = (TextView) view.findViewById(R.id.tv_second_fragment);
return view;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
public void setTextViewText(CharSequence text){
tv.setText(text);
Toast.makeText(getActivity(), tv.getText(), Toast.LENGTH_LONG).show();
}//The toast shows the correct text but its not updated in the fragment (on the emulator)
ProductFragment中的相关代码
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
this.onDataActivity = (OnDataPass)activity;
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//Toast.makeText(getActivity(), products.get(position).getName().toString(), Toast.LENGTH_LONG).show();
onDataActivity.loadItemsForCategory(products.get(position).getName().toString());
}
在替换片段后尝试执行此操作:
@Override
public void loadItemsForCategory(String category) {
ft = fragmentManager.beginTransaction();
ft.replace(R.id.content_frame, secondFragment).addToBackStack(TAG).commit();
secondFragment.setTextViewText("Requseted to show items for " + category);
}
编辑
public class SecondFragment extends Fragment {
private TextView tv;
private String text="";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.second_fragment, container, false);
tv = (TextView) view.findViewById(R.id.tv_second_fragment);
return view;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
tv.setText(text);
}
public void setTextViewText(CharSequence text){
this.text=text;
//Toast.makeText(getActivity(), tv.getText(), Toast.LENGTH_LONG).show();
}
试试下面的方法
您需要确保片段已附着
在loadItemsForCategory
Bundle bundle = new Bundle();
bundle.putString("key",category);
FragmentManager manager=getSupportFragmentManager();
secondFragment = SecondFragment.newInstance();
newFragment.setArguments(bundle);
FragmentTransaction transaction=manager.beginTransaction();
transaction.replace(R.id.container,secondFragment);
transaction.commit();
然后在第二个片段的onCreateview中
String strtext = getArguments().getString("key");
召唤
在
SecondFragment
的onCreateView
中调用setTextViewText
,首先需要检查片段是否可见,然后设置文本try my edited ans Raghunandan说的是正确的,需要等待视图出现。
String strtext = getArguments().getString("key");
setTextViewText(strtext)