Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/215.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 文本视图文本未更新_Android_Textview - Fatal编程技术网

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)