user4108483
user4108483

Reputation:

Android setText in fragment does not work

in this sample code i want to custom message by setText but that doesnot work.

public class newCustomers extends Fragment {
    public newCustomers () {
    }
    @Override
    public View onCreateView (LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
        View rootView = inflater.inflate ( R.layout.fragment_new_customers, container, false );
        final Date date = new Date();
        DateFormat dateFormat = new SimpleDateFormat ("yyyy-MM-dd");
        String currentDate = dateFormat.format ( date );
        String cDate1 = JalaliCalendar.g2j ( currentDate );

        View view = inflater.inflate(R.layout.fragment_new_customers, container, false);
        EditText name = (EditText) view.findViewById ( R.id.name );

        String string="this is a text";
        name.setText ( string );
        return rootView;
    }
    @Override
    public void onResume () {
        super.onResume ();
    }
}

i'm wondering why thats dont work correctly. all of cast and my code is correct

Upvotes: 0

Views: 458

Answers (2)

max59
max59

Reputation: 606

Remove this line from your code:

View view = inflater.inflate(R.layout.fragment_new_customers, container, false);

and make this edit in your code:

EditText name = (EditText) rootView.findViewById ( R.id.name );

Upvotes: 1

ρяσѕρєя K
ρяσѕρєя K

Reputation: 132972

Change return of onCreateView method to view instead of rootView :

name.setText ( string );
return view;

becuase you are access EditText from view instead of rootView.

Upvotes: 0

Related Questions