How to handle data entered into a fragment of a Tabbed Activity in Android Studio

Asked

Viewed 73 times

0

I am learning to develop using Android Studio and I am with a beginner’s question: which class I perform data processing from a click of a fragment in a Tabbed Activity. To illustrate simply, I created a simple application using Tabbed Activity with a fragment containing an Edit text and a button. In the code of the screen, I try to call from the click of the button a class called "saved":

 <Button
    android:id="@+id/button3"
    android:layout_width="88dp"
    android:layout_height="48dp"
    android:layout_marginStart="132dp"
    android:layout_marginTop="61dp"
    android:layout_marginEnd="191dp"
    android:layout_marginBottom="382dp"
    android:text="Button"
    android:onClick="salvaDados"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/editText" />

The idea (at the moment) is simply to test the value of Edit text, with the following code:

     public void salvaDados(){
        Button botao = findViewById(R.id.button3);
        EditText texto = findViewById(R.id.editText);
        if(texto.getText().length() == 0){
            Toast.makeText(getApplication(),"nao tem nada",Toast.LENGTH_SHORT).show();
        }
        else
            Toast.makeText(getApplication(),"tem dados",Toast.LENGTH_SHORT).show();

    }

The question is, in what class do I treat it? 1. I tried to create the above class in fragment "mother activity" and gave error; 2. I tried to create a separate class only to process fragment data and also gave error. 3. the above example is just for me to know where I should treat all the code, since later I will do more complex things like save the data in the database, generate graphs on other screens, etc.

It has at least 15 hours that I am trying to solve this and I could not find any tutorial to help. Can someone give me a light? Thanks for your help!

  • Send the class of your fragment where the button is and edittext is. By the fragment you can do this, depends on how you are doing. Inside the fragment in the onCreateView method, you have how to call.

  • Hi Murillo, I tried to call you from the fragment and I still can’t. At present, the onCreateView class simply calls the layout with Layoutinflater Inflater and does not allow using the findViewById method. I have to declare that I am inheriting the layout screen data elsewhere on onCreate?

  • have as yes, I will answer on the basis of your code, then you implement and give me a return to see if it works.

1 answer

0


Here is an example of the fragment class you can use:

public class seuFragmento extends Fragment {


    private View view;
    private Activity activity;

    public void onAttach(Context context) {
        super.onAttach(context);
        activity = (Activity) context;
    }

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

        salvaDados();

        return view;
    }
    public void salvaDados(){
        Button botao = view.findViewById(R.id.button3);
        EditText texto = view.findViewById(R.id.editText);
        if(texto.getText().length() == 0){
            Toast.makeText(activity,"nao tem nada",Toast.LENGTH_SHORT).show();
        }
        else
            Toast.makeText(activity,"tem dados",Toast.LENGTH_SHORT).show();

    }
}

You use the view raised in onCreateView to call findViewById. Use the context raised in onAttach to use in the toast. In the view changes R.layout.seu_layout_fragment to its own layout.

  • Thanks for the answer! I understood how View.I entered the code, but it didn’t work because the layout fragment is not finding the class. The following error appears: "java.lang.Illegalstateexception: Could not find method salvaDados(View) in a Parent or Ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.Appcompatbutton with id 'button3'". From what I understand, I don’t have to deal with clicking the button on the layout fragment, so I tried to create a method inside "saved" using setOnClickListener(new View.Onclicklistener() but tb didn’t work. :/

  • In order to use setOnClickListener... you need to take the onClick:"saved" button in the xml. Maybe the answer to this link question can help you: Illegalstateexception: Could not find method

  • Thank you so much for helping Murilo! It worked here!

  • @Wellington, you are welcome brother, if you can mark as answered and/ or as useful I thank, open

Browser other questions tagged

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