Record with Tabs

-3

I am doing a registration and I chose to do it using Tabs because it is extensive (many fields) and involves photos, so I can put the information organized, it is the order of service registration, 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 think the logic would be to get the contents of the tabs (3), to save a button on the last tab? will any of you have done something similar and can you suggest a way?

I tried to evolve the routine and now I have more information and codes that can illustrate what has been done until then.

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

Below the listener in the parent activity, note that the process will be done by getting the fragment of 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) {}
});
}

The Error Occurs and I 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 the 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;
    }
    
asked by anonymous 20.09.2017 / 00:08

1 answer

1

For others to develop a screen with Tabs and make use of it for a registration you will need to get the data that is 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 there, I tested, I went in the documentation of fragments, I did what he suggests or work with interfaces to send data to the mother activity. None of this would solve my specific case.

Solution: In Activity have private attributes of my fragments, use the ViewPager adapter to create the instances already onCreate, and make use of the TabLayout listener to monitor the change in the Tab, when the user clicks the next Tab to save the data from the previous Tab

Activity code

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 may have the same problem or want to use Tabs to compose a Record and save the data by getting the contents of the various Tabs

    
22.09.2017 / 18:47