Reputation: 937
I'm trying to set date in the textview from a fragment. The problem is that, after picking a date and clicking the ok button of the dialog, it won't instantly set the date to the textview. Upon clicking the set date button again, that's when the date gets set in the textview.
ButtonFragment
public class ButtonFragment extends Fragment {
Button date;
TextView datetext;
Date dateInterface;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.button_fragment, container, false);
date = (Button) view.findViewById(R.id.date);
datetext = (TextView) view.findViewById(R.id.datetext);
date.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dateInterface.setDate(datetext);
}
});
datetext = (TextView) view.findViewById(R.id.datetext);
return view;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try{
dateInterface = (Date) activity;
}catch(Exception e) {
}
}
public interface Date{
public void setDate(TextView textView);
}
}
MainActivity
public class MainActivity extends AppCompatActivity implements ButtonFragment.Date {
Calendar c = Calendar.getInstance();
TextView display;
int cday, cmonth, cyear;
String date;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
ButtonFragment buttonFragment = new ButtonFragment();
fragmentTransaction.add(R.id.fragment_container, buttonFragment);
fragmentTransaction.commit();
}
public void setDate(String date) {
this.date = date;
}
public String getDate() {
return this.date;
}
DatePickerDialog.OnDateSetListener d = new OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
cday = dayOfMonth;
cmonth = monthOfYear + 1;
cyear = year;
}
};
@Override
public void setDate(TextView textView) {
new DatePickerDialog(MainActivity.this, d,
c.get(Calendar.YEAR), c.get(Calendar.MONTH), c
.get(Calendar.DAY_OF_MONTH)).show();
date = "Choosen date is :" + cday + "/" + cmonth + "/"
+ cyear;
textView.setText(date);
}
}
Upvotes: 0
Views: 1589
Reputation: 1261
I'll answer in short, just use these outside of any method like onViewCreated/onCreateView. Eg: Add these methods as one of the public/private/protected method of the class/Fragment:
DatePickerDialog.OnDateSetListener d = new OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
txtNow.setText(year + "/" + monthOfYear+1 "/" + dayOfMonth);// set the text using the global pointer
}
};
Upvotes: 0
Reputation: 1308
change the code as follows
DatePickerDialog.OnDateSetListener d = new OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
cday = dayOfMonth;
cmonth = monthOfYear + 1;
cyear = year;
date = "Choosen date is :" + cday + "/" + cmonth + "/"
+ cyear;
txtNow.setText(date);// set the text using the global pointer
}
};
TextView txtNow;// this will be global pointer to the textView passed
@Override
public void setDate(TextView textView) {
txtNow = textView;// initilize the Global pointer
new DatePickerDialog(MainActivity.this, d,
c.get(Calendar.YEAR), c.get(Calendar.MONTH), c
.get(Calendar.DAY_OF_MONTH)).show();
}
it should work now
Upvotes: 1