Update of Fragments Tabs!

Asked

Viewed 738 times

0

I have two Tabs in my App, composed of fragments. The first is a form that saves the data in the database. The second is a list that retrieves that data. I would like to know how to update my List fragment, because after saved I need to restart the Activity to collect the saved data.

That’s my Main, where the Tabs are.

public class MainActivity extends AppCompatActivity {
private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

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

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    viewPager = (ViewPager) findViewById(R.id.viewpager);
    setupViewPager(viewPager);

    tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(viewPager);
}

private void setupViewPager(ViewPager viewPager) {
    ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
    adapter.addFragment(new HomeFragment(), "BOLETIM");
    adapter.addFragment(new ListaBasicaFragment(), "LISTA DE BOLETINS");
    viewPager.setAdapter(adapter);
}

class ViewPagerAdapter extends FragmentPagerAdapter {
    private final List<Fragment> mFragmentList = new ArrayList<>();
    private final List<String> mFragmentTitleList = new ArrayList<>();

    public ViewPagerAdapter(FragmentManager manager) {
        super(manager);
    }

    @Override
    public Fragment getItem(int position) {
        return mFragmentList.get(position);
    }

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

    public void addFragment(Fragment fragment, String title) {
        mFragmentList.add(fragment);
        mFragmentTitleList.add(title);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mFragmentTitleList.get(position);
    }
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu, menu);

    return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == R.id.action_logout) {
        startActivity(new Intent(this, SincronizarActivity.class));
        finish();
    }
    return super.onOptionsItemSelected(item);
}


}

This is my List I need you to update.

 public class ListaBasicaFragment extends Fragment {

private RecyclerView rvListaBasica;
private BasicaAdapter adapter;
private LinearLayoutManager llBasico;
private List<AtMecRaptMecDeVO> list = new ArrayList<>();
private AtMecRaptMecHeVO HeVO = new AtMecRaptMecHeVO();
private AtMecRaptMecDeVO DeVO = new AtMecRaptMecDeVO();
private Context context;
private AtMecRaptMecDeBO deBO;
private AtMecRaptMecHeBO heBO;
private FragmentTransaction ft = null;
private TextView tvBoletim, tvHrProd, tvHrImprod, tvHrTot, tvData, tvTurno, tvFrente, tvFuncionario, tvEquip, tvK20, tvVazao;
private Button btnEditar, btnFinalizarBoletim;
private static final String TAG = "LBActivity";

public ListaBasicaFragment() {
    // Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, final ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_lista_basica, container, false);

    try {
        //initViews();
        tvBoletim = (TextView) view.findViewById(R.id.tv_boletim_he);
        tvHrProd = (TextView) view.findViewById(R.id.tv_hr_prod);
        tvHrImprod = (TextView) view.findViewById(R.id.tv_hr_impr);
        tvHrTot = (TextView) view.findViewById(R.id.tv_hr_tot);
        tvData = (TextView) view.findViewById(R.id.tv_data_he);
        tvTurno = (TextView) view.findViewById(R.id.tv_turno_he);
        tvFrente = (TextView) view.findViewById(R.id.tv_frente_he);
        tvFuncionario = (TextView) view.findViewById(R.id.tv_funcionario_he);
        tvEquip = (TextView) view.findViewById(R.id.tv_equip_he);
        tvVazao = (TextView) view.findViewById(R.id.tv_vazao_he);
        tvK20 = (TextView) view.findViewById(R.id.tv_k20_he);
        btnEditar = (Button) view.findViewById(R.id.btn_editar_he);
        btnFinalizarBoletim = (Button) view.findViewById(R.id.btn_finalizar_boletim_list);
        btnEditar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, EditHeActivity.class);
                Bundle bundle = new Bundle();
                bundle.putInt("id", HeVO.getId());
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
        btnFinalizarBoletim.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            }
        });
        //initViews();

        context = getContext();
        rvListaBasica = (RecyclerView) view.findViewById(R.id.rv_lista_basica);
        llBasico = new LinearLayoutManager(context);
        llBasico.setOrientation(LinearLayoutManager.VERTICAL);
        rvListaBasica.setHasFixedSize(true);
        rvListaBasica.setLayoutManager(llBasico);
        deBO = new AtMecRaptMecDeBO(context);
        heBO = new AtMecRaptMecHeBO(context);
        list = deBO.getall();
        HeVO = heBO.getBoletimAberto();
        adapter = new BasicaAdapter(context, getActivity(), list);
        rvListaBasica.setAdapter(adapter);
        setValues(HeVO, DeVO);
        Log.i(TAG, "vo.toString(): " + HeVO.toString());
    } catch (Exception e) {
        Log.i(TAG, "onCreate: " + e);
    }
    // Inflate the layout for this fragment
    return view;
}

private void setValues(AtMecRaptMecHeVO HeVO, AtMecRaptMecDeVO DeVO) {
    Log.i(TAG, "setValues: " + DeVO.toString());
    tvBoletim.setText(HeVO.getNoBoletim().toString());
    tvK20.setText(HeVO.getQtTeorK2o().toString());
    tvVazao.setText(HeVO.getQtVazaoPrg().toString());
    tvData.setText(HeVO.getDtOperacao());
    switch (HeVO.getFgTurno()) {
        case "A":
            tvTurno.setText(String.valueOf("A - 23h às 7h"));
            break;
        case "B":
            tvTurno.setText(String.valueOf("B - 7h às 15h"));
            break;
        case "C":
            tvTurno.setText(String.valueOf("C - 15h às 23h"));
            break;
    }
    tvFrente.setText(HeVO.getCdFrenTran().toString());
    tvFuncionario.setText(HeVO.getCdFunc().toString());
    tvEquip.setText(HeVO.getCdEquipto().toString());

    tvHrProd.setText(deBO.samProdutivo(Double.valueOf(HeVO.getNoBoletim()), 123.0).toString());
    tvHrImprod.setText(deBO.samProdutivo(Double.valueOf(HeVO.getNoBoletim()), 9999.0).toString());
    tvHrTot.setText(deBO.samTotal(Double.valueOf(HeVO.getNoBoletim())).toString());

}
}
  • Try to fetch your information from the database in the onResume of your Fragment, overwrite onResume and take the test.

  • Could you give me an example of how it would look?? I’m new to programming. I haven’t used onResume yet();

2 answers

0

Alter FragmentPagerAdapter for FragmentStatePagerAdapter

If it doesn’t work, you can do the following

  • Create an interface with a "upgrade method"

  • Your Fragment Listbasicafragment should implement this interface

  • In the implementation of your upgrade methodList you should call your method that assembles the list, pass it to Adapter and then list using setAdapter
  • add:

            viewpager.setOnPageChangeListener(new OnPageChangeListener() {
              @Override
              public void onPageSelected(int position) {
                Fragment f = getChildFragmentManager().getFragments().get(viewPager.getCurrentItem());
              }}
    

    //Cast for your Fragment and call updateList

See this link and this

0


I managed to solve my problem. In case anyone has any further doubts. I am leaving the method I used in my List so that the necessary refresh occurs:

@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
    super.setUserVisibleHint(isVisibleToUser);

    if (isVisibleToUser) {
        Log.d("MyFragment", "Fragment is visible.");
        tvBoletim = (TextView) view.findViewById(R.id.tv_boletim_he);
        tvHrProd = (TextView) view.findViewById(R.id.tv_hr_prod);
        tvHrImprod = (TextView) view.findViewById(R.id.tv_hr_impr);
        tvHrTot = (TextView) view.findViewById(R.id.tv_hr_tot);
        tvData = (TextView) view.findViewById(R.id.tv_data_he);
        tvTurno = (TextView) view.findViewById(R.id.tv_turno_he);
        tvFrente = (TextView) view.findViewById(R.id.tv_frente_he);
        tvFuncionario = (TextView) view.findViewById(R.id.tv_funcionario_he);
        tvEquip = (TextView) view.findViewById(R.id.tv_equip_he);
        tvVazao = (TextView) view.findViewById(R.id.tv_vazao_he);
        tvK20 = (TextView) view.findViewById(R.id.tv_k20_he);
        rvListaBasica = (RecyclerView) view.findViewById(R.id.rv_lista_basica);
        btnEditar = (Button) view.findViewById(R.id.btn_editar_he);
        btnFinalizarBoletim = (Button) view.findViewById(R.id.btn_finalizar_boletim_list);
        initObject();
        if (HeVO!=null){
            setValues(HeVO, DeVO);
            setRecyclerView();
        }
    }else {
        Log.d("MyFragment", "Fragment is not visible.");
    }
}

Browser other questions tagged

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