0
I’m having a problem here which is the following, I have an Activity (extending from FragmentActivity
) who owns a ViewPager
using SectionsPagerAdapter
as an adapter.
I can create as many pages as I need and pass data to them, my problem is to be able to capture the data recorded in the fields.
For example, when switching pages I would like to already record what was typed in the fields of the current screen, before going to Screen 2.
Or through a Button
, click and already record what was typed.
I don’t know how to get the EditText
of the pages.
Look at the code:
Activity Code
package br.example.teste;
import java.util.List;
import java.util.Locale;
import br.example.modelo.Abastecimento;
import br.example.modelo.Atendimento;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.LayoutAnimationController;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
public class AbastecimentoFragmentActivity extends FragmentActivity {
private Context contexto;
private static LinearLayout layoutAnimado;
private static final int ACTIVITY_PRODUTOS = 1;
private Atendimento atendParaSerAlterado = null;
private List<Abastecimento> abastecimentos;
private Abastecimento abastecimento = null;
private int qtd_molas;
private static String[] lista_prods = new String[] {"MAQ BEB QUENTE BARI 220 NV","MAQ.PREP.BEB.Q.XM240E/4","MAQUINA LANCHE AMS M"};
//private static final String[] PRODUTOS = new String[] {"MAQ BEB QUENTE BARI 220 NV","MAQ.PREP.BEB.Q.XM240E/4","MAQUINA LANCHE AMS M"};
/**
* The {@link android.support.v4.view.PagerAdapter} that will provide
* fragments for each of the sections. We use a
* {@link android.support.v4.app.FragmentPagerAdapter} derivative, which
* will keep every loaded fragment in memory. If this becomes too memory
* intensive, it may be best to switch to a
* {@link android.support.v4.app.FragmentStatePagerAdapter}.
*/
private SectionsPagerAdapter mSectionsPagerAdapter;
/**
* The {@link ViewPager} that will host the section contents.
*/
private ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.abastecimento);
contexto = this;
Intent intent = this.getIntent();
atendParaSerAlterado = (Atendimento) intent.getSerializableExtra("atendimento");
//abastecimento = (Abastecimento) intent.getSerializableExtra("abastecimento");
abastecimentos = atendParaSerAlterado.getAbastecimento();
Bundle extras = intent.getExtras();
qtd_molas = extras.getInt("qtd_molas");
if (qtd_molas == 0){
qtd_molas = 5;
}
// Create the adapter that will return a fragment for each of the three
// primary sections of the app.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
//mViewPager.setPageTransformer(true, new ZoomOutPageTransformer());
mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
// When changing pages, reset the action bar actions since they are dependent
// on which page is currently active. An alternative approach is to have each
// fragment expose actions itself (rather than the activity exposing actions),
// but for simplicity, the activity provides the actions in this sample.
//abastecimento.setAbastecido(mViewPager.); //invalidateOptionsMenu();
Log.i("FRAGMENT","Alterando tela -> " +position+1);
mViewPager.getCurrentItem();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
/**
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a DummySectionFragment (defined as a static inner class
// below) with the page number as its lone argument.
mViewPager.getCurrentItem();
Fragment fragment = new DummySectionFragment();
Abastecimento abast = (Abastecimento) abastecimentos.get(position);
abast.setSaldo(String.valueOf(position));
// passar aqui argumentos para campos serem preenchidos
Bundle args = new Bundle();
args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);
args.putInt("num", position + 1);
args.putSerializable("abastecimento", abast);
fragment.setArguments(args);
return fragment;
}
@Override
public int getCount() {
// Show 3 total pages.
return qtd_molas;
}
@Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
int pos = position + 1;
return "Mola " +pos;
}
@Override
public void startUpdate (ViewGroup container){
Log.i("STARTUPDATE","STARTUPDATE");
EditText saldo = (EditText) container.findViewById(R.id.EdtSaldo);
EditText retirado = (EditText) container.findViewById(R.id.EdtRetirado);
EditText abastecido = (EditText) container.findViewById(R.id.EdtAbastecido);
EditText descarte = (EditText) container.findViewById(R.id.EdtDescarte);
//mSectionsPagerAdapter.getItem(0).getArguments();
//this.getCount();
//mViewPager.getCurrentItem();
//Abastecimento abast = (Abastecimento) abastecimentos.get(mViewPager.getCurrentItem());
//abast.setAbastecido(abastecido.getText().toString());
}
/*
@Override
public void finishUpdate(ViewGroup container) {
// TODO Auto-generated method stub
Log.i("FINISHUPDATE","FINISHUPDATE");
//EditText saldo = (EditText) container.findViewById(R.id.EdtSaldo);
}
*/
}
/**
* A dummy fragment representing a section of the app, but that simply
* displays dummy text.
*/
public static class DummySectionFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
public static final String ARG_SECTION_NUMBER = "section_number";
public DummySectionFragment() {
}
/**
* When creating, retrieve this instance's number from its arguments.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
int mNum = getArguments() != null ? getArguments().getInt("num") : 1;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main_dummy, container, false);
//TextView dummyTextView = (TextView) rootView.findViewById(R.id.section_label);
//dummyTextView.setText(Integer.toString(getArguments().getInt(ARG_SECTION_NUMBER)));
int num = getArguments().getInt("num");
Abastecimento abast = (Abastecimento) getArguments().getSerializable("abastecimento");
// layout animado
layoutAnimado = (LinearLayout) rootView.findViewById(R.id.animado);
// campos do layout - preencher aqui os valores se necessario
EditText saldo = (EditText) rootView.findViewById(R.id.EdtSaldo);
saldo.setText(abast.getSaldo());
EditText retirado = (EditText) rootView.findViewById(R.id.EdtRetirado);
retirado.setText(abast.getRetirado());
EditText abastecido = (EditText) rootView.findViewById(R.id.EdtAbastecido);
abastecido.setText(abast.getAbastecido());
EditText descarte = (EditText) rootView.findViewById(R.id.EdtDescarte);
descarte.setText(abast.getDescarte());
// alterar o produto da mola
ImageView mola = (ImageView) rootView.findViewById(R.id.btMola);
mola.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View view){
Intent edicao = new Intent(getActivity(),ListaProdutosActivity.class);
startActivityForResult( edicao,ACTIVITY_PRODUTOS );
}
});
mostrar();
return rootView;
}
// na destruicao da view - grava os dados
@Override
public void onDestroyView(){
super.onDestroyView();
Log.i("ONDESTROYVIEW","DESTRUINDO A VIEW");
}
}
// verificar
public static void mostrar(){
if (layoutAnimado.getVisibility() == View.GONE){
animar(true);
layoutAnimado.setVisibility(View.VISIBLE);
}
}
public static void animar(boolean mostrar){
AnimationSet set = new AnimationSet(true);
Animation animation = null;
if (mostrar){
//desde la esquina inferior derecha a la superior izquierda
animation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 1.0f,
Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, 1.0f,
Animation.RELATIVE_TO_SELF, 0.0f);
}
//duración en milisegundos
animation.setDuration(500);
set.addAnimation(animation);
LayoutAnimationController controller = new LayoutAnimationController(set, 0.25f);
layoutAnimado.setLayoutAnimation(controller);
layoutAnimado.startAnimation(animation);
}
//
public void salvaForm(View view){
Log.i("salvaForm","salvaForm");
mViewPager.getCurrentItem();
Abastecimento abast = (Abastecimento) abastecimentos.get(mViewPager.getCurrentItem());
// como obter os edittext dos fragments?
//mViewPager
}
@Override
public void onActivityResult(int codigo, int resultado, Intent it){
super.onActivityResult(codigo, resultado, it);
}
@Override
public void onBackPressed() {
Log.i("onBackPressed","onBackPressed");
super.onBackPressed();
}
}
Code of the XML Layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity$DummySectionFragment"
android:orientation="vertical" >
<ImageView
android:id="@+id/btMola"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_gravity="center"
android:src="@drawable/ic_launcher" />
<LinearLayout
android:id="@+id/animado"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:visibility="visible"
android:orientation="vertical"
android:padding="8dp"
android:background="@android:color/transparent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:visibility="visible"
android:orientation="horizontal"
android:padding="8dp"
android:background="@android:color/transparent" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_acao_refresh" />
<EditText
android:id="@+id/EdtSaldo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Saldo"
android:background="@drawable/border"
android:inputType="number" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:visibility="visible"
android:orientation="horizontal"
android:padding="8dp"
android:background="@android:color/transparent" >
<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_acao_refresh" />
<EditText
android:id="@+id/EdtRetirado"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Retirado"
android:background="@drawable/border"
android:inputType="number" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:visibility="visible"
android:orientation="horizontal"
android:padding="8dp"
android:background="@android:color/transparent" >
<ImageView
android:id="@+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_acao_refresh" />
<EditText
android:id="@+id/EdtAbastecido"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Abastecido"
android:background="@drawable/border"
android:inputType="number" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:visibility="visible"
android:orientation="horizontal"
android:padding="8dp"
android:background="@android:color/transparent" >
<ImageView
android:id="@+id/imageView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_acao_refresh" />
<EditText
android:id="@+id/EdtDescarte"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Vencido/Avaria"
android:background="@drawable/border"
android:inputType="number" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:visibility="visible"
android:orientation="horizontal"
android:padding="8dp"
android:background="@android:color/transparent" >
<Button
android:id="@+id/btnSalvar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="salvaForm"
android:text="Salvar" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
Jackson, I recommend using the
OnPageChangeListener
in theViewPager
. I saw you using, not working? But I also recommend you to change the way your Adapter is. Save Fragments instances in a list, otherwise you will lose Fragment data while browsing Viewpager. Have a look at the methodsonDestroyView
andonSaveInstanceState
of Fragment, can help you if you reuse Fragments with the list.– Wakim
Thank you for your answer! I solved otherwise, for the application that I have this type of screen would not be more appropriate, I decided using a screen with Gridview.
– jdeus762