从tablayout中的每个选项卡中获取EditText的值

时间:2016-03-14 07:22:38

标签: android android-sqlite android-tablayout

我需要有关如何从tablayout中的edittext检索userinput值的帮助。我有4个选项卡,每个选项卡都有相同的editText小部件,这意味着我只实现了一个edittext视图。问题是,如果我在每个editText中放置一个值,我不知道如何检索它们,因为我不知道如何找到每个editText的位置,因为我只在布局中实现了一次。我甚至需要将它们放在sqlite中的数据库中。有人会指导我吗感谢。

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);

    }




}

1 个答案:

答案 0 :(得分:1)

首先尝试设置视图寻呼机的偏移限制

viewPager.setOffscreenPageLimit(4);

这样你的片段只会被创建一次。

然后在你的主要活动中创建一个像这样的片段列表

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

将您的寻呼机适配器更改为此

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);
}

}

现在像这样将片段添加到适配器。

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));
}

现在转到您的片段并更改textview的状态

public TextView content;

现在循环遍历数组列表并通过对象获取textView,然后您可以访问它的值。

希望能帮助你。