Get the value of an EditText from each tabs in a tablayout

I need help on how to retrieve the userinput value from an edittext in a tablayout. I have 4 tabs and each tab have the same editText widget meaning that I only implemented one edittext view. Problem is, if I put a value in each editText I have no idea how to retrieve them since I don't know how to find the position for each editText considering that I only implemented it once in my layout. I even need to put them inside my database which is in sqlite. Would someone guide me. Thanks.

My_Notes_Add_Main.java

package com.zaid.recyclerviewsqlite.addNotes;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

import com.zaid.recyclerviewsqlite.R;

public class My_Notes_Add_Main extends AppCompatActivity implements View.OnClickListener {

    private final int numOfPages = 4; //viewpager has 4 pages
    private final String[] pageTitle = {"Terms", "Definition", "Syntax", "Code"};
    private ImageButton addNew;
    private EditText terms, def, syn, code;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_notes_add_main);



        addNew = (ImageButton)findViewById(R.id.addNew);
        addNewTerms();

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar1);
        setSupportActionBar(toolbar);

        TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);

        for (int i = 0; i < numOfPages; i++) {
            tabLayout.addTab(tabLayout.newTab().setText(pageTitle[i]));
        }

        //set gravity for tab bar
        tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

        final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
        final PagerAdapter adapter = new My_Notes_Add_MyPagerAdapter(getSupportFragmentManager(), numOfPages);

        viewPager.setAdapter(adapter);
        viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
        tabLayout.setOnTabSelectedListener(onTabSelectedListener(viewPager));
    }

    private TabLayout.OnTabSelectedListener onTabSelectedListener(final ViewPager pager) {
        return new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                pager.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        };
    }

  /**  @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.my_notes_add_main_menu, menu);        
        return true;
    } */

    public void addNewTerms(){
        addNew.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                Toast.makeText(getApplicationContext(), "Add Click", Toast.LENGTH_SHORT).show();
            }

    });
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

    }






}

My_Notes_Add_MyPagerAdapter.java

package com.zaid.recyclerviewsqlite.addNotes;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;

public class My_Notes_Add_MyPagerAdapter extends FragmentStatePagerAdapter {
    int numOfTabs;

    public My_Notes_Add_MyPagerAdapter(FragmentManager fm, int numOfTabs) {
        super(fm);
        this.numOfTabs = numOfTabs;
    }

    @Override
    public Fragment getItem(int position) {

       return My_Notes_Add_TabFragment.getInstance(position);
    }

    @Override
    public int getCount() {
        return numOfTabs;
    }
}

My_Notes_Add_TabFragment.java

package com.zaid.recyclerviewsqlite.addNotes;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.zaid.recyclerviewsqlite.R;

public class My_Notes_Add_TabFragment extends Fragment {

    private int position;
    private TextView content;
    private ImageView image;

    public static Fragment getInstance(int position) {
        My_Notes_Add_TabFragment f = new My_Notes_Add_TabFragment();
        Bundle args = new Bundle();
        args.putInt("position", position);
        f.setArguments(args);
        return f;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //get data from Argument
        position = getArguments().getInt("position");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.my_notes_add_tab_fragment, container, false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);


        content = (EditText) view.findViewById(R.id.textView);

    }




}

Answers


first of all try to set the offset limit for your view pager

viewPager.setOffscreenPageLimit(4);

in this way your fragment will be created only one time.

then in your main activity create a list of fragments like this

ArrayList<Fragment> mFragmentList = new ArrayList<>();

change your Pager adapter to this

class PagerAdapter extends FragmentPagerAdapter {

private final List<Fragment> fragmentList = new ArrayList<>();
private final List<String> fragmentTitleList = new ArrayList<>();

public PagerAdapter(FragmentManager fragmentManager) {
        super(fragmentManager);
}

public void addFragment(Fragment fragment, String title) {
     fragmentList.add(fragment);
     fragmentTitleList.add(title);
}

@Override
public Fragment getItem(int position) {
      Fragment fragment = fragmentList.get(position);
      Bundle bundle = new Bundle();
      // add any data if you want.
      bundle.putInt("any_data", "any_data");
      fragment.setArguments(bundle);
      return fragment;
}

@Override
public int getCount() {
     return fragmentList.size();
}

@Override
public CharSequence getPageTitle(int position) {
     return fragmentTitleList.get(position);
}

}

now add fragment to adapter like this.

public class My_Notes_Add_Main extends AppCompatActivity implements View.OnClickListener {

private final int numOfPages = 4; //viewpager has 4 pages
private final String[] pageTitle = {"Terms", "Definition", "Syntax", "Code"};
private ImageButton addNew;
private EditText terms, def, syn, code;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.my_notes_add_main);



    addNew = (ImageButton)findViewById(R.id.addNew);
    addNewTerms();

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar1);
    setSupportActionBar(toolbar);

    TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);

    for (int i = 0; i < numOfPages; i++) {
        tabLayout.addTab(tabLayout.newTab().setText(pageTitle[i]));
    }

    //set gravity for tab bar
    tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

    final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);

    PagerAdapter pagerAdapter= new My_Notes_Add_MyPagerAdapter(getSupportFragmentManager());
Fragment fragment = My_Notes_Add_TabFragment .getInstance();
pagerAdapter.addFragment(fragment , pageTitle[0]);
mFragmentList.add(fragment);

Fragment fragment = My_Notes_Add_TabFragment .getInstance();
pagerAdapter.addFragment(fragment , pageTitle[1]);
mFragmentList.add(fragment);

Fragment fragment = My_Notes_Add_TabFragment .getInstance();
pagerAdapter.addFragment(fragment , pageTitle[2]);
mFragmentList.add(fragment);

Fragment fragment = My_Notes_Add_TabFragment .getInstance();
pagerAdapter.addFragment(fragment , pageTitle[3]);
mFragmentList.add(fragment);

    viewPager.setAdapter(adapter);
    viewPager.setOffscreenPageLimit(4);
    viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    tabLayout.setOnTabSelectedListener(onTabSelectedListener(viewPager));
}

now go to your fragment and change the status of your textview

public TextView content;

now loop through array list and get the textView through object and then you can access it value.

Hope that will help you.


Need Your Help

mdate() function codeigniter - error

php codeigniter datetime timestamp

In my database I have a datetime format row.

python-magic WindowsError: [Error 193] %1 is not a valid Win32 application\r

python python-2.7 libmagic

Hi I'm trying to use python-magic on windows 8 but when I try to import with the command import magic it gives me this error WindowsError: [Error 193] %1 is not a valid Win32 application\r I have t...