How do you change the textview of the fragment depending on the case menu?



  • There's a side menu Expandable Navigation Drawer. I used something to press the element. switch/case.

    case 1:
                        switch (childPosition) {
                            case 0:
    
                            break;
                        case 1:
    
                            break;
                        case 2:
    
                            break;
                        case 3:
    
                            break;
                        default:
                            break;
                    }
                    break;
                default:
                    break;
    

    Where? case 1 - it's an open element of the menu. case 0; case 1; case 2; case 3 - the menu's sublime is open.

    There's a fragment with the marking in which textview

        <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <TextView
    android:id="@+id/selected_item"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/frag1_text">
    </TextView>
    </LinearLayout>

    This one. textview The text shall be removed from string depending on the compressed sub-element (i)case) Because I don't have one fragment, I'm calling it in case first.

        case 1:
    switch (childPosition) {
    case 0:

                                fTrans.replace(R.id.frgmCont, frag1);
    
                                break;                          
                            default:
                                break;
                        }
                        break;
                    default:
                        break;
    

    In the fragment class, I read:

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment1, null);

    mSelectedItemView = (TextView) v.findViewById(R.id.selected_item);
    mSelectedItemView.setText(Html.fromHtml(getResources().getString(R.string.selected_item),
            null, new MyHtmlTagHandler()));
    
    return v;
    

    }
    }

    Now, when you call a fragment in case 0 - I'm calling his mark. TextViewwhich takes the text from string.selected_itemby causing the same fragment case 1 That's what happens. The same text will be on the screen. string.selected_item♪ But I need the same text from the same text. string.txt_1 For example. Not to be created for everyone case fragment. But I'll write a line in case:

    mSelectedItemView = (TextView) v.findViewById(R.id.selected_item);
    mSelectedItemView.setText(Html.fromHtml(getResources().getString(R.string.selected_item),
    null, new MyHtmlTagHandler()));

    changing the resource address in each case: R.string.selected_itemR.string.txt_0 for case 1 and R.string.txt_1 for case 2 It's not gonna work. Then how to do my job. Thank you for your help!



  • You can tell me what line you need to put into the fragment.

        frag1 = new YourFragment();// создаете фрагмент
        Bundle args = new Bundle();
        int stringId = 0;
        switch (childPosition) {
            case 0:
                stringId = R.string.txt_1
                break;
            case 1:
                stringId = R.string.txt_2;
                break;
            case 2:
                stringId = R.string.txt_3;
                break;
            case 3:
                stringId = R.string.txt_4;
                break;
            default:
                break;
        }
        args.putInt("stringId", stringId);
        frag1.setArguments(args);
        fTrans.replace(R.id.frgmCont, frag1);
    

    And now in the most fragment:

    mSelectedItemView = (TextView) v.findViewById(R.id.selected_item);
    

    mSelectedItemView.setText(Html.fromHtml(getResources().getString(getArguments().getInt("stringId")), null, new MyHtmlTagHandler()));




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2