Register with Tabs

Asked

Viewed 86 times

-3

Gentlemen I am making a registration and I chose to do using Tabs because the same is extensive ( many fields ) and involves photos, this way I can put organized information, is the registration of service order, however I noticed that Tabs is 99% to present data I did not find any example that shows its use in a registration (in my case in sqlite) and now with the screen made how to save/update this data that are in the tabs?

I believe that the logic would be to obtain the contents of the tabs (3), to save a button in the last tab? Has one of you ever done such a thing And can suggest me a way?

I tried to evolve the routine and now I have more information and codes that can exemplify what was done until then.

The logic used for a register with Tabs is that there will be a "listener" in the navigation, this will access the corresponding Fragment in viewPager and get the data for validation or persistence.

Below the listener in the mother activity, note that the process will be done by obtaining the Fragment from the previous tab

private void setupTabListener() {
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
    @Override
    public void onTabSelected(TabLayout.Tab tab) {
        viewPager.setCurrentItem(tab.getPosition());

        View customView = tab.getCustomView();
        if (customView != null) {
            ImageView icon = (ImageView) customView.findViewById(R.id.itemImage);
            int tabIconColor = ContextCompat.getColor(getApplicationContext(), android.R.color.white);
            icon.setColorFilter(tabIconColor, PorterDuff.Mode.SRC_IN);
        }
    }
    // Aba Anterior
    @Override
    public void onTabUnselected(TabLayout.Tab tab) {
        Log.i(TAG, "onTabUnselected()" + tab.getPosition());
        View customView = tab.getCustomView();
        Boolean valido;
        // Obter o fragment usando o adapter
        if (tab.getPosition()==0){
           VeiculoFragment fragment = (VeiculoFragment)
                   .customFragmentPageAdapter
                   .getItem(tab.getPosition());
           // Na execução do método do fragment abaixo ocorre um erro  
           fragment.isValid();
        }

         }
    }

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

Error occurs and captured the stack

     Process: br.com.tecnico, PID: 19927
 java.lang.NullPointerException
 at br.com.tecnico.fragment.VeiculoFragment.isValid(VeiculoFragment.java:143)
 at br.com.tecnico.atividades.InstalacaoActivity$1.onTabUnselected(InstalacaoActivity.java:158)
 at android.support.design.widget.TabLayout.dispatchTabUnselected(TabLayout.java:1170)
 at android.support.design.widget.TabLayout.selectTab(TabLayout.java:1153)
 at android.support.design.widget.TabLayout.selectTab(TabLayout.java:1127)
 at android.support.design.widget.TabLayout$Tab.select(TabLayout.java:1426)
 at android.support.design.widget.TabLayout$TabView.performClick(TabLayout.java:1536)

The line where the error occurs is in Fragment that was instantiated and called the isValid() method, the error occurs in if(edtNumero.gettext())

class VeiculoFragment extends BaseFragment {         
@Override
public boolean isValid() {
    // O Erro ocorre neste if abaixo
    if (edtNumero.getText().toString().trim().equals("")){
        //mostraSnackErro(getResources().getString(R.string.erro_frota_vazia));
        return false;
    }

    if (edtModelo.getText().toString().trim().equals("")){
        //mostraSnackErro(getResources().getString(R.string.erro_modelo_vazio));
        return false;
    }
  • I updated giving more information and codes

1 answer

1

For others develop a screen with Tabs and make use of it for a registration you will need to take the data that are in the fragments of each Tab and this is not an easy task, I spent a few days to solve the question, I asked here and there, tested, I went in the documentation of Fragments, I did what he suggests, namely to work with interfaces to send data to the parent activity but none of this would solve my case specifically.

Solution: In Activity have private attributes of my fragments, use the Viewpager Adapter to create the instances already in onCreate, and make use of the Tablayout System to monitor the change in Tab, when the user click on the following Tab save the data from the previous Tab

Code of activity

public class Instalacaoactivity extends Appcompatactivity {

private static final String TAG = InstalacaoActivity.class.getSimpleName();
private CustomFragmentPageAdapter customFragmentPageAdapter;
private ViewPager viewPager;
private TabLayout tabLayout;

private int id;
private Fragment1 fragment1;
private Fragment2 fragment2;
private Fragment3 fragment3;
private Fragment4 fragment4;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // outros 
    customFragmentPageAdapter = new CustomFragmentPageAdapter( getSupportFragmentManager(), getResources()
            .getStringArray(R.array.titles_tabs), this);

    viewPager = (ViewPager) findViewById(R.id.view_pager);
    //viewPager.setOffscreenPageLimit(4);
    viewPager.setAdapter(customFragmentPageAdapter);
    //
    customFragmentPageAdapter.startUpdate(viewPager);
    fragment1 = (Fragment1) customFragmentPageAdapter.instantiateItem(viewPager, 0);
    fragment2 = (Fragment2) customFragmentPageAdapter.instantiateItem(viewPager, 1);
    fragment3 = (Fragment3) customFragmentPageAdapter.instantiateItem(viewPager, 2);
    fragment4 = (Fragment4) customFragmentPageAdapter.instantiateItem(viewPager, 3);
    customFragmentPageAdapter.finishUpdate(viewPager);
    //
    tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(viewPager);
}

// Aqui neste ouvinte vamos acessar o fragment
private void setupTabListener() {
    tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            viewPager.setCurrentItem(tab.getPosition());

            View customView = tab.getCustomView();
            if (customView != null) {
                ImageView icon = (ImageView) customView.findViewById(R.id.itemImage);
                int tabIconColor = ContextCompat.getColor(getApplicationContext(), android.R.color.white);
                icon.setColorFilter(tabIconColor, PorterDuff.Mode.SRC_IN);
            }
        }

        // Aqui qdo o usuário muda de tab vamos trabalhar e obter os dados
        // ou validar os dados da tab anterior
        @Override
        public void onTabUnselected(TabLayout.Tab tab) {
            Log.i(TAG, "onTabUnselected()" + tab.getPosition());
            View customView = tab.getCustomView();

            Boolean valido;

            //Fragment1 fragment1 = (Fragment1)
            //    customFragmentPageAdapter.getItem(tab.getPosition());
            // A linha acima ira dar o erro de NullPointer porque estou aqui criando uma nova instancia do fragement então aqui estava um grande problema que eu enfrentei  
            if (tab.getPosition()==0){
                if (fragment1 != null){
                    Log.i(TAG, "ver Validação dos dados");
                    valido = fragment1.isValid();
                    // Aqui estou chamando um metodo do fragmento 
                    // mais poderiamos obter os objetos da view e salvar 
                    // estes dados  
                    if(!valido){
                        Helper.displayInfoMessage(InstalacaoActivity.this,"Dados invalidos");
                    }
                }

I hope this helps others who have the same problem or who want to use Tabs to compose a Registration and save the data by obtaining the contents of the various Tabs

Browser other questions tagged

You are not signed in. Login or sign up in order to post.