I have two Tabs in my Application, made up of fragments. The first is a form that saves the data in the database. The second is a list that retrieves this data. I would like to know how to update my List fragment, because after I save I need to restart the Activity to list the saved data.
This is 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 that I need 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());
}
}