0
When I click on a Main Activity button, a Viewpager is created that allows me to move between 7 layouts that I have. The pager is set to present first the layout called "domingo.xml" and everything works normally. I just want this layout to be shown a date, so I use this code:
Locale local = new Locale("pt", "BR");
DateFormat sdf = new SimpleDateFormat("EEEE - dd/MM", local);
Date d = Calendar.getInstance().getTime();
String mostrarData = sdf.format(d).toUpperCase(local);
final TextView textoData = (TextView) findViewById(R.id.textView1);
textoData.setText(mostrarData);
I created a new layout just to test and the date code works and displays the date normally, but in the Domingo.xml layout (which is handled by Viewpager) I can’t at all.
This is the code of the button that calls the Viewpager:
public void btTela1(View v) {
startActivity(new Intent(getBaseContext(), Pager.class));
}
And this is the Viewpager code:
public class Pager extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pager);
AdapterPages adapter = new AdapterPages();
ViewPager myPager = (ViewPager) findViewById(R.id.pager);
myPager.setAdapter(adapter);
myPager.setCurrentItem(0);
}}
There is also the Pageradapter code used to configure Viewpager. Where should I write the date code? I have tried in several places, but it is always a mistake. I do not know if there is some conceptual error that I do not understand. I’m new to programming and this is the first time I’ve put on a forum. If you need more information tell me what I add. Thanks.
Code of the Pageradapter:
public class AdapterPages extends PagerAdapter {
@Override
public int getCount() {
return 7;
}
@Override
public Object instantiateItem(View collection, int position) {
LayoutInflater inflater = (LayoutInflater) collection.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
int resId = 0;
switch (position) {
case 0:
resId = R.layout.domingo;
break;
case 1:
resId = R.layout.segunda;
break;
case 2:
resId = R.layout.terca;
break;
case 3:
resId = R.layout.quarta;
break;
case 4:
resId = R.layout.quinta;
break;
case 5:
resId = R.layout.sexta;
break;
case 6:
resId = R.layout.sabado;
break;
}
View view = inflater.inflate(resId, null);
((ViewPager) collection).addView(view, 0);
return view;
}
@Override
public void destroyItem(View arg0, int arg1, Object arg2) {
((ViewPager) arg0).removeView((View) arg2);
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == ((View) arg1);
}
@Override
public Parcelable saveState() {
return null;
}}
Code of Sunday.xml:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/data_domingo"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:background="#BEBEBE"
android:gravity="center"
android:textStyle="bold" />
...
From what @Wakim taught me I modified a small part of the Pageradapter code and stayed like this:
View view = inflater.inflate(resId, null);
((ViewPager) collection).addView(view, 0);
Locale local = new Locale("pt", "BR");
DateFormat sdf = new SimpleDateFormat("EEEE - dd/MM", local);
Date d = Calendar.getInstance().getTime();
String mostrarData = sdf.format(d).toUpperCase(local);
TextView textoData = (TextView) collection
.findViewById(R.id.data_domingo);
textoData.setText(mostrarData);
return view;
Now the date appeared, however, when I slide to the third layout hangs everything. What is happening ?
It is not enough to assign the
String(of the formatted date) within the methodgetViewofAdapterPages? It would be nice to just put the code ofAdapterPagesand its xml ofViewthat it creates and adds toViewPager.– Wakim
@Wakim added the codes you requested...
– Felipe
Thank you Felipe, could you tell me where exactly you use the code that changes the
textoDatain relation to theViewPageror was just for testing?– Wakim
@Wakim I used for testing and also tried to write the whole date code directly on both Viewpager and Pageradapter. Where would be the correct ?
– Felipe
The most correct is in
PagerAdapter. You need to add a logic within thegetViewthat fills theTextView. And you don’t need a layout for each day (if the layout is similar), just one and add logic to customize depending on the position.– Wakim
@Wakim They are similar, I understand what this logic would be, but I still don’t know how to make it. Could you give me an example of what this getView should look like ?
– Felipe
Yes, I will prepare an answer with this idea that I suggested.
– Wakim
@Wakim Ok, thank you very much. And don’t you notice, I just started.
– Felipe
Okay, I get it. Assuming you’re still using that switch, you can’t use fixed R.id.data_domingo, because it’s only Sunday.xml, in the case of the second.xml the id is another, right? For this you will have to create a variable called textViewId and set according to the switch value (equal to Resid). And use findViewById(textViewId) in the view and not in the container.
– Wakim
I think I get it, I’m gonna try and I’ll get back to you, thank you very much.
– Felipe
@Wakim excuse the delay, it worked perfectly, the emulator once in a while, but the device is perfect. I added an If for each case, like this: if (position == 1) { Textview textData = (Textview) Collection . findViewById(R.id.data_segunda); textData.setText(showData); } I believe it is not the right one, but it worked, thank you very much for your efforts in helping me. I don’t know if there’s a rule, but add yourself on the face. vlw, good night.
– Felipe