TextView文本未更新

时间:2014-01-01 10:28:38

标签: android textview

我正在尝试将TextView中的fragment文字从另一个fragment更改为TextView。 我试图通过与活动沟通来做到这一点。 但是,我注意到ProductFragment文本已更改,但新文本中没有显示。

我的listview包含onItemClick()个产品类别。当列表中的SecondFragment被执行时,我会尝试显示仅包含TextView的{​​{1}}。然后,我尝试更新listviewProductFragment中点击的类别的文本。 MainActivityFragmentActivity

这是我的代码:

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());
}

并在MainActivity中:

public class MainActivity extends FragmentActivity implements OnKeyListener, OnDataPass{

    private FragmentManager fragmentManager;
    private FragmentTransaction ft;


    private ProductsFragment productsFragment;
    private SecondFragment secondFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        fragmentManager = getSupportFragmentManager();
        ft = fragmentManager.beginTransaction();

        productsFragment = ProductsFragment.newInstance(products);
        secondFragment = SecondFragment.newInstance();
        ft.replace(R.id.content_frame, secondFragment).commit();

    @Override
    public void loadItemsForCategory(String category) {
        secondFragment.setTextViewText("Requseted to show items for " + category);
        ft = fragmentManager.beginTransaction();
        ft.replace(R.id.content_frame, secondFragment).addToBackStack(TAG).commit();
    }

}

2 个答案:

答案 0 :(得分:1)

替换片段后尝试执行此操作:

@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();
}

答案 1 :(得分:1)

尝试以下

您需要确保附加了片段。

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");  

呼叫

setTextViewText(strtext)