0

我正在尝试将现有的一组功能从另一个应用程序移植到新应用程序向导中的操作栏/滑动格式。

我有单独的片段布局(除了名称之外都是相同的)并且包含所有按钮并且所有通过 toast 语句工作(和测试)。

我似乎不能做的是更新当前片段的文本视图。我已经尝试了我能想到的一切。我无法决定是否需要进行片段交易,或者我是否遗漏了一些小东西并追逐自己的尾巴。我头晕了,有人能阻止这个旋转木马吗?

这是片段编码的一部分

    public static class DetailFragment extends Fragment {
public static final String ARG_SECTION_NUMBER = "section_number";
    LayoutInflater _inflater;
    ViewGroup _container;

    public DetailFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        int position;
        _inflater = inflater;
        _container = container;
        position = getArguments().getInt(ARG_SECTION_NUMBER)-1;

        View rootView;
        TextView dummyTextView;
        // Location Database Handler
        //LocationDatabaseHandler locationDatabaseHandler = new LocationDatabaseHandler(getActivity());

        switch (position){
        case 0:

            rootView = inflater.inflate(R.layout.fragment_main_player,container, false);
            dummyTextView= (TextView) rootView .findViewById(R.id.section_label);
            // Player
            Button btnTruncateDBPlayerTable = (Button) rootView.findViewById(R.id.btnTruncateDBPlayerTable);
            Button btnPlayerCount = (Button) rootView.findViewById(R.id.btnPlayerCount);
            Button btnShowDBPlayerList = (Button) rootView.findViewById(R.id.btnShowDBPlayerList);
            Button btnShowPlayer = (Button) rootView.findViewById(R.id.btnShowPlayer);
            Button btnAddContacts = (Button) rootView.findViewById(R.id.btnAddContacts);
            Button btnAddContact = (Button) rootView.findViewById(R.id.btnAddContact);

            btnTruncateDBPlayerTable.setOnClickListener(new OnClickListener() {
            @Override               
            public void onClick(View v) {
                Activity activity = getActivity();
                //android.app.FragmentManager fm = getActivity().getFragmentManager();

                ////DummySectionFragment currFragment = (DummySectionFragment) fm.findFragmentById(R.id.fragment_main_player);
                //View x =  _inflater.inflate(R.layout.fragment_main_player, _container , false);
                //v = _inflater.inflate(R.layout.fragment_main_player, _container , false);
                //TextView myTextView = (TextView) x .findViewById(R.id.section_label);

                if (activity != null) {

                    Toast.makeText(activity, "toast_you_just_clicked_a_fragment btnTruncateDBPlayerTable button", Toast.LENGTH_LONG).show();
                    DetailFragment updateFragment = dFragmentList.get(0);
                    //updateFragment.recieveUpdate("Holy Crap Batman");
                    //myTextView.append("WHOOT");
                }
            }
            });
            btnPlayerCount.setOnClickListener(new OnClickListener() {
                @Override               
                public void onClick(View v) {
                    Activity activity = getActivity();

                    if (activity != null) {
                        Toast.makeText(activity, "toast_you_just_clicked_a_fragment btnPlayerCount button", Toast.LENGTH_LONG).show();
                    }
                }
                });
            btnShowDBPlayerList.setOnClickListener(new OnClickListener() {
                @Override               
                public void onClick(View v) {
                    Activity activity = getActivity();

                    if (activity != null) {
                        Toast.makeText(activity, "toast_you_just_clicked_a_fragment btnShowDBPlayerList button", Toast.LENGTH_LONG).show();
                    }
                }
                });
            btnShowPlayer.setOnClickListener(new OnClickListener() {
                @Override               
                public void onClick(View v) {
                    Activity activity = getActivity();

                    if (activity != null) {
                        Toast.makeText(activity, "toast_you_just_clicked_a_fragment btnShowPlayer button", Toast.LENGTH_LONG).show();
                    }
                }
                });
            btnAddContacts.setOnClickListener(new OnClickListener() {
                @Override               
                public void onClick(View v) {
                    Activity activity = getActivity();

                    if (activity != null) {
                        Toast.makeText(activity, "toast_you_just_clicked_a_fragment btnAddContacts button", Toast.LENGTH_LONG).show();
                    }
                }
                });
            btnAddContact.setOnClickListener(new OnClickListener() {
                @Override               
                public void onClick(View v) {
                    Activity activity = getActivity();

                    if (activity != null) {
                        Toast.makeText(activity, "toast_you_just_clicked_a_fragment btnAddContact button", Toast.LENGTH_LONG).show();
                    }
                }
                });


            break;
4

1 回答 1

1

假设dummyTextView在此之前已成功找到rootView它将起作用:

btnTruncateDBPlayerTable.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
    dummyTextView.setText("Whatever");
    // Toast stuff
});

如果您出于某种原因想延迟查找该 TextView,则可以通过 Activity 进行搜索:

TextView textView = (TextView) getActivity().findViewById(R.id.section_label);
textView.setText("Whatever");

在注释掉的代码中执行以下操作将不起作用

// in onClick
View x = _inflater.inflate(R.layout.fragment_main_player, _container , false);
TextView myTextView = (TextView) x.findViewById(R.id.section_label);
myTextView.setText("Whatever");

通过扩充新布局,您将创建一个新的 TextView 作为它的一部分。然后,您可以找到并设置该 TextView 的文本,但由于它不在实际显示的布局中,因此效果相当小。

于 2013-11-06T01:11:29.013 回答