Android : How do I update my textView in a Fragment

前端 未结 3 990
旧巷少年郎
旧巷少年郎 2020-12-29 08:44

I am trying to use fragments to build my first proper Android App. I have a main xml. which consists of two vertical fragments, the top fragments consists of just two TextVi

相关标签:
3条回答
  • 2020-12-29 08:47

    You need to assign the inflated fragment layout to a variable so that you can access its findViewById() method. Then you return it once you're done updating your widgets.

    @Override
    public View onCreateView(LayoutInflater inflater,
            ViewGroup container, Bundle savedInstanceState){
        // -- inflate the layout for this fragment
        View myInflatedView = inflater.inflate(R.layout.fragmentmt, container,false);
    
        // Set the Text to try this out
        TextView t = (TextView) myInflatedView.findViewById(R.id.viewItem);
        t.setText("Text to Display");
    
        return myInflatedView;
    }
    

    Normally, when you call findViewById() within an Activity, you're invoking Activity.findViewById(). Trying the same call within your Fragment class will fail because there is no such Fragment.findViewById() method. So, you must use View.findViewById() with your inflated view to obtain references to your widgets.

    0 讨论(0)
  • 2020-12-29 08:54

    I am using this code from my activity to update a textview added in xml tag. The textview are class level objects. Heres some code:

    <fragment
        android:id="@+id/fragmentRegister"
        android:name="com.proshore.loveis.RegisterFragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/view1" />
    

    Heres the fragment code just showing the class level variable.

    public class RegisterFragment extends Fragment implements OnClickListener {
    
    TextView textViewDOB, textViewLanguage;
    
    public RegisterFragment() {
    }
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
    //eluded methods here
    }
    }
    

    Now the activity from where I update my fragment:

    public class RegisterActivity extends FragmentActivity {
    Button buttonEnter;
    RegisterFragment fragmentRegister;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        buttonEnter = (Button) findViewById(R.id.buttonEnter);
        fragmentRegister = (RegisterFragment) getSupportFragmentManager()
                .findFragmentById(R.id.fragmentRegister);
    }
    
    @Override
    protected void onStart() {
        // TODO Auto-generated method stub
        super.onStart();
        buttonEnter.setOnClickListener(new View.OnClickListener() {
    
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Log.d("dontknowitwillowrk", fragmentRegister.textViewLanguage
                        .getText().toString());
                fragmentRegister.textViewLanguage.setText("hello mister how do you do");
    
    
            }
        });
    }
    }
    

    Note: not sure if this is a good way.

    0 讨论(0)
  • 2020-12-29 08:58

    I am not an expert but I would say that return statement within a method returns something and the next lines after this statement are unreachable :O

    So try to change the order! :)

    0 讨论(0)
提交回复
热议问题