Sorry if my question is silly, and my mistake is blatant, but I am now one week into learning java and android dev, so any constructive criticism, even ones that aren't directly related to my question, for my code, would be very welcome!
Thank you for your help, and time in advanced.
My current situation is, that I am trying to determine which page number I'm on in my ViewPager set up. Basically, I have a bunch of 'forms' that are created, which are instances of a "UserFrament.java" fragment. The form collects user input from a few edit texts and on the push of the button, hopefully stores the data, and goes to the next fragment. (I currently think this is working). However, now, I'm trying to determine the current page I'm on, so I can have my 'onClick' method perform a different task if it's on the last viewPage (store all data, put into an intent and then start another activity to do some stuff with the data).
So currently I have this code inside my 'UserFragment.java':
int currentviewnum = ((FirstFragment) getActivity()).getPager().getCurrentItem();
String pagenum = Integer.toString(currentviewnum);
Now FirstFragment is the activity that set's up the viewPager etc. My adapter is "FragmentAdapter.java" With my current set up when I print the string 'pagenum' to a random textview on each viewPager page, as I swipe to the next page, (or navigate with my button), the number's are just not right. Say I have 5 pages (the user defines how many forms there are) so, FirstFragment receives an intent defining the number of instances it creates.
The number sequence is like "0, 0, 1,2,3" Then as I scroll back it's doing stuff like "3, 2, 3, 2, 1" So I have no idea why it's so stuffed up.
My UserFragment.java:
package com.example.thinice;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class UserFragment extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.layout_avgcalcform,
container, false);
final EditText markc = (EditText)rootView.findViewById(R.id.editTextASSMARK);
final EditText marktotc = (EditText)rootView.findViewById(R.id.editTextASSTOT);
final EditText assvalc = (EditText)rootView.findViewById(R.id.editTextASSWORTH);
int currentviewnum = ((FirstFragment) getActivity()).getPager().getCurrentItem();
String pagenum = Integer.toString(currentviewnum);
TextView tv = (TextView) rootView.findViewById(R.id.avg_testtv);
tv.setText(pagenum);
Button b = (Button) rootView.findViewById(R.id.button1);
b.setOnClickListener( new View.OnClickListener(){
public void onClick(View v){
int currentviewnum = ((FirstFragment) getActivity()).getPager().getCurrentItem();
((FirstFragment) getActivity()).saveData(new Question(markc.getText().toString(),marktotc.getText().toString(),assvalc.getText().toString()));
((FirstFragment) getActivity()).getPager().setCurrentItem(currentviewnum+1);
}
});
return rootView;
}
}
If you need to see any more of my code, please let me know. But this is really annoying me. So if anybody out there could help me, it would be fantastic! Thank you very much in advanced.
EDIT:MORE CODE FragmentAdapter.java :
package com.example.thinice;
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.view.ViewGroup;
public class FragmentAdapter extends FragmentPagerAdapter{
private List<Fragment> fragments;
public FragmentAdapter(FragmentManager fragmentManager, ArrayList<Fragment> fragments) {
super(fragmentManager);
this.fragments = fragments;
}
@Override
public Fragment getItem(int position) {
//Bundle args = new Bundle();
//args.putInt("page_position", position+1);
//((Fragment) fragments).setArguments(args);
return this.fragments.get(position);
}
@Override
public void destroyItem (ViewGroup container, int position, Object object)
{
super.destroyItem(container, position, object);
}
@Override
public int getCount() {
return this.fragments.size();
}
}
FirstFragment.java (this is my activity) :
package com.example.thinice;
import java.util.ArrayList;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.util.SparseArray;
import android.widget.Button;
public class FirstFragment extends FragmentActivity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_viewpager);
// Check whether the activity is using the layout version with
// the fragment_container FrameLayout. If so, we must add the first fragment
int size = getIntent().getExtras().getInt("numass1");
ViewPager vp = (ViewPager) findViewById(R.id.viewPager);
if (savedInstanceState != null) {
return;
}
ArrayList<Fragment> lf = new ArrayList<Fragment>();
for(int count=0; count<size; count ++){
Fragment f = new UserFragment();
lf.add(f);
}
FragmentAdapter hello = new FragmentAdapter(getSupportFragmentManager() , lf);
vp.setAdapter(hello);
// vp.setOffscreenPageLimit(size);
}
SparseArray<Question> questions = new SparseArray<Question>();
public void saveData(Question quest){
ViewPager vp = (ViewPager) findViewById(R.id.viewPager);
questions.put(vp.getCurrentItem(), quest);
}
public ViewPager getPager() {
ViewPager vp = (ViewPager) findViewById(R.id.viewPager);
return vp;
}
}