How to create method of sending message in Tabs?

0

I'm creating an app, in android, which has tabs. Each tab is a chat room. I took an example of a video on how to create app with Tabs, where the author, in addition to creating the app using a tab template, he create a tab class for each tab and an xml for each of them as well. It concludes that to send a message on a particular tab, I would have to create the method of sending the message in the class tab, of each one of them, however, I am not able to make the "connection" of the text field (TextView) in this class, since the findoViewById () command does not appear in the tab class!

package com.example.gustavo.vigilantescomunitarios;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class TabRua extends Fragment {

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.tab_rua, container, false);


    }

}
    
asked by anonymous 11.10.2017 / 19:59

2 answers

2

You do not need to use findviewById because you are not using the inflated root of the layout, when you extend to a fragment things are a bit different from when you extend to an Activity, you have to access the other layouts through of the inflated layout:

public class TabRua extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.tab_rua, container, false);
    Textview textView = rootView.findviewById(R.id.textView); //Apenas um exemplo
    return rootView;
}
    
11.10.2017 / 21:05
0

You can use the Bundle class and pass an instance of that class to your Fragment in the act of your Intent .

Example:

Bundle bundle = new Bundle();
bundle.putString("foo", "bar"); // chave e valor, respectivamente
fragment.setArguments(bundle);

And to get the data in your Fragment

Bundle bundle = this.getArguments();

if (bundle != null) {
    String foo = bundle.getInt("foo", "VALOR_PADRAO");
}

EDIT 1: As for findViewById() , I recommend you take a look at ButterKnife library to handle view binding .

    
11.10.2017 / 20:45