I am using view pager in my activity.I created views for 7 pages. Now i want to access page view inside my activity. I am getting data as blank. In Activity
public class ActivitySingleEntry extends Activity implements OnClickListener {
private ViewPager mPager;
private FixedTabsView mFixedTabs;
private ExamplePagerAdapter mPagerAdapter;
private TabsAdapter mFixedTabsAdapter;
private EditText edtFieldName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fixed_tabs);
initViewPager(7, 0xFFFFFFFF, 0xFF000000);
mFixedTabs = (FixedTabsView) findViewById(R.id.fixed_tabs);
mFixedTabsAdapter = new FixedTabsAdapter(this);
mFixedTabs.setAdapter(mFixedTabsAdapter);
mFixedTabs.setViewPager(mPager);
}
private void initViewPager(int pageCount, int backgroundColor, int textColor) {
mPager = (ViewPager) findViewById(R.id.pager);
mPagerAdapter = new ExamplePagerAdapter(this, pageCount,
backgroundColor, textColor);
mPager.setAdapter(mPagerAdapter);
mPager.setCurrentItem(1);
mPager.setPageMargin(5);
}
@Override
public void onClick(View v) {
// LinearLayout lin=(LinearLayout) mPager.getChildAt(mPager.getCurrentItem());
// edtFieldName=(EditText) lin.findViewById(R.id.edtFieldName);
// Log.d("test", "From get child:"+edtFieldName.getText().toString()+":");
Log.d("test", "Current Page:"+mPager.getCurrentItem());
LinearLayout linearLayout=(LinearLayout) mPager.findViewWithTag("lin"+mPager.getCurrentItem());
edtFieldName=(EditText) linearLayout.findViewById(R.id.edtFieldName);
edtFieldName=(EditText) findViewById(R.id.edtFieldName);
if (edtFieldName==null) {
ShowToast.makeToast(getApplicationContext(), "Edt null");
}else
ShowToast.makeToast(getApplicationContext(),
"Data saved " + edtFieldName.getText().toString() + ":"
+ mPager.getCurrentItem());
}
}
My PageAdaper
public class ExamplePagerAdapter extends PagerAdapter {
protected transient Activity mContext;
private int mLength = 0;
private int mBackgroundColor = 0xFFFFFFFF;
private int mTextColor = 0xFF000000;
private String[] mData = { "Temperature", "Sugar", "BP", "Field 4",
"Field 5", "Field 6", "Field 7" };
public ExamplePagerAdapter(Activity context, int length,
int backgroundColor, int textColor) {
mContext = context;
mLength = length;
mBackgroundColor = backgroundColor;
mTextColor = textColor;
}
@Override
public int getCount() {
return mLength;
}
@Override
public Object instantiateItem(View container, int position) {
LinearLayout linearLayout = (LinearLayout) View.inflate(mContext,
R.layout.activity_single_entry, null);
TextView txtFieldName = (TextView) linearLayout
.findViewById(R.id.txtFieldName);
EditText edtFieldName = (EditText) linearLayout
.findViewById(R.id.edtFieldName);
String filedName = mData[position];
txtFieldName.setText(filedName);
edtFieldName.setHint("Please enter " + filedName);
edtFieldName.setInputType(InputType.TYPE_CLASS_TEXT);
if (filedName.equals("Temperature")) {
edtFieldName.setInputType(InputType.TYPE_CLASS_NUMBER);
} else if (filedName.equals("Sugar")) {
edtFieldName.setInputType(InputType.TYPE_CLASS_NUMBER);
} else if (filedName.equals("BP")) {
edtFieldName.setInputType(InputType.TYPE_CLASS_NUMBER);
}
edtFieldName.setTag("edt");
((ViewPager) container).addView(linearLayout, 0);
linearLayout.setTag("lin"+position);
Log.d("test", "Adapter creating item:"+position );
return linearLayout;
}
@Override
public void destroyItem(View container, int position, Object view) {
((ViewPager) container).removeView((View) view);
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((View) object);
}
@Override
public void finishUpdate(View container) {
}
@Override
public void restoreState(Parcelable state, ClassLoader loader) {
}
@Override
public Parcelable saveState() {
return null;
}
@Override
public void startUpdate(View container) {
}
}
In R.layout.activity_single_entry xml i have edittext and button with onclick.
MYXml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/txtFieldName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<EditText
android:id="@+id/edtFieldName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_weight="1"
android:hint="Please enter field value" />
</LinearLayout>
<Button
android:id="@+id/btnSave"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Save"
android:layout_margin="20dp"
android:onClick="onClick"
/>
</LinearLayout>
If i used inside pageadapter
@Override
public int getItemPosition(Object object) {
// TODO Auto-generated method stub
return POSITION_NONE;
}
OnClick of button i want to access data from edit text.
I able to get value for first page.Not for all.
The ViewPager is a layout widget made up of multiple child views where each child view constitutes a page in the layout: Typically, ViewPager is used in conjunction with Fragments ; however, there are some situations where you might want to use ViewPager without the added complexity of Fragment s.
This example demonstrates how to use android view pager. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.
setPageTransformer () -Set a transformer to apply custom transformations when page changes occur To add the View Pager 2 into our project we need to begin by adding the required dependency to our build.gradle file: With that added, we now have access to the View Pager 2 component, which means we can go ahead and add it to our layout file:
When the ViewPager displays the image at position, it displays this ImageView. Initially, InstantiateItem is called twice to populate the first two pages with views. As the user scrolls, it is called again to maintain views just behind and ahead of the currently displayed item. The DestroyItem method removes a page from the given position.
firstm you setTag():
linearLayout.setTag("lin"+position);
where position is in <0..numPages-1>
but read it:
LinearLayout linearLayout=(LinearLayout) mPager.findViewWithTag("lin"+mPager.getCurrentItem());
is findViewWithTag returning null everywhere or only on the last page? mPager.getCurrentItem() returns 0 if it is first page so there is no need to add +1 while reading
In page adapter i am using single view in each page so its refers to only first item.If i tried to get view for page its giving me only first object so i applied distinct id for edit text in each page.
In page adapter
@Override
public Object instantiateItem(View container, int position) {
LinearLayout linearLayout = (LinearLayout) View.inflate(mContext,
R.layout.activity_single_entry, null);
TextView txtFieldName = (TextView) linearLayout
.findViewById(R.id.txtFieldName);
EditText edtFieldName = (EditText) linearLayout
.findViewById(R.id.edtFieldName);
***edtFieldName.setId(position);***
String filedName = mData[position];
txtFieldName.setText(filedName);
edtFieldName.setHint("Please enter " + filedName);
edtFieldName.setInputType(InputType.TYPE_CLASS_TEXT);
if (filedName.equals("Temperature")) {
edtFieldName.setInputType(InputType.TYPE_CLASS_NUMBER);
} else if (filedName.equals("Sugar")) {
edtFieldName.setInputType(InputType.TYPE_CLASS_NUMBER);
} else if (filedName.equals("BP")) {
edtFieldName.setInputType(InputType.TYPE_CLASS_NUMBER);
}
edtFieldName.setTag("edt"+position);
((ViewPager) container).addView(linearLayout, 0);
mItems.add(linearLayout);
linearLayout.setTag("lin"+position);
Log.d("test", "Adapter creating item:"+position );
return linearLayout;
}
Its working now
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With