-1

i have created a viewpager swipe view withot using fragments. according to the swipe get the position and layout is displayed. Actually this is a calendar application. i have get the current date when app starting and i want to change a textview according to that date. the problem is when i access the non class layout like below and change the textview, it want change.

Calendar cal = Calendar.getInstance();
int dd = cal.get(Calendar.DAY_OF_MONTH);
int mm = cal.get(Calendar.MONTH);
++mm;
int yy = cal.get(Calendar.YEAR);
String today = String.valueOf(dd);

final View vi = inflater.inflate(R.layout.december, null);
t1 = (TextView)vi.findViewById(R.id.do4);
t1.setText("today");
Shan Nirmala
  • 37
  • 1
  • 7
  • Possible duplicate of [How to access Activity UI from my class?](https://stackoverflow.com/questions/6030982/how-to-access-activity-ui-from-my-class) – ADM Dec 22 '17 at 08:48

1 Answers1

0

Your Activity has a TextView which you want to change. I would, first, make a method in that Activity changing the textview, say,

public void setDate(Date date) {
   dateView.setText(formatDateSomehow(date));
}

Then if you want to set the proper date as soon as the Activity starts I would call this method from the Activity's onCreate()

public void onCreate(Bundle savedState) {
   super.onCreate(savedState);
   setDate(new Date());
}

Then if you want to set the date from outside the Activity (and you don't even know if your activity is displayed) you can use LocalBroadcast similar to this: https://androidcookbook.com/Recipe.seam?recipeId=4547

Or you can use an EventBus like this one: http://square.github.io/otto/

Or you can use LiveData https://developer.android.com/topic/libraries/architecture/livedata.html

Alexander Kulyakhtin
  • 47,782
  • 38
  • 107
  • 158
  • you mean that create method to set text to the textview in non class activity xml ,in my main activity class and call it in main activity oncreate method? – Shan Nirmala Dec 22 '17 at 09:25
  • i think that the main activity can access the text views that is available in the setcontentview(layout) file. but the textview i want to change is available in a layout xml file that has no class. so i think first i need to initiate the textview correctly by findviewbyid and then set the text. – Shan Nirmala Dec 22 '17 at 09:27