Swapnil Deshmukh
Swapnil Deshmukh

Reputation: 665

How to access views from view pager

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.

Upvotes: 12

Views: 11702

Answers (3)

pskink
pskink

Reputation: 24720

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

Upvotes: 13

Swapnil Deshmukh
Swapnil Deshmukh

Reputation: 665

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

Upvotes: 1

Meehow
Meehow

Reputation: 51

A quick solution would be to keep a List in your ExamplePagerAdapter and add to it after your addView() call. Then you could implement a public function such as getItemAtPosition(int position) that returns the LinearLayout at that page of the ViewPager. Then you can run findViewById(R.id.edtFieldName) on that linear layout in your Activity. In code:

In your ExamplePagerAdapter, add a private object:

List<LinearLayout> mItems;

add in your constructor:

mItems = new Vector<LinearLayout>();

add in instantiateItem():

((ViewPager) container).addView(linearLayout, 0);
mItems.add(linearLayout);

then add a method:

public LinearLayout getItemAtPosition(int position) {
    return mItems.get(position);
}

Then use that method in your Activity. Hope it's what you were looking for.

Upvotes: -1

Related Questions