Я использую view pager в своей деятельности. Я создал представления для 7 страниц. Теперь я хочу получить доступ к просмотру страницы в своей деятельности. Я получаю данные пустыми. В действии
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) {
}
}
В R.layout.activity_single_entry xml у меня есть edittext и кнопка с 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>
Если я использовал внутри pageadapter
@Override
public int getItemPosition(Object object) {
// TODO Auto-generated method stub
return POSITION_NONE;
}
OnClick кнопки Я хочу получить доступ к данным из текста редактирования.
Я могу получить значение для первой страницы. Не для всех.