Fragment setContentView Android

1

Hello good day today was making an app connected to the database with Firebase the application is already connected and all with some tutorials that I saw on YouTube, but now I wanted to make my app more aesthetic and I added a Bottom NavigationView that will pass through different fragment by clicking on its corresponding icon, like I searched on YouTube for a tutorial to do it and it's already there, the problem is that a fragment I want to pass through, has the connection to firebase and is being inherited by AppCompatActivity and to do the bottom you need to inherit Fragment (Android.Support.v4) and then I skip error in setContentView, findViewById and getApplicationContext)

This is the code that sends me error when doing the inheritance

 public class Home extends Fragment {

    public RecyclerView mCategList;
    private DatabaseReference mDatabase;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        mDatabase = FirebaseDatabase.getInstance().getReference().child("Global");
        mDatabase.keepSynced(true);

        mCategList = (RecyclerView) findViewById(R.id.myrecycleview);
        mCategList.setHasFixedSize(true);
        mCategList.setLayoutManager(new LinearLayoutManager(this));
    }

    @Override
    protected void onStart() {
        super.onStart();
        FirebaseRecyclerAdapter<Categoria, categoriaViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Categoria, categoriaViewHolder>
                (Categoria.class, R.layout.categ_row, categoriaViewHolder.class, mDatabase) {
            @Override
            protected void populateViewHolder(categoriaViewHolder viewHolder, Categoria model, int position) {
            viewHolder.setTitle(model.getTitle());
            viewHolder.setImage(getApplicationContext(),model.getImage());
            }
        };
        mCategList.setAdapter(firebaseRecyclerAdapter);
    }

        public static class categoriaViewHolder extends RecyclerView.ViewHolder
        {
            View mView;
            public categoriaViewHolder(View itemView)
            {
                super(itemView);
                mView=itemView;
            }
            public void setTitle(String title)
            {
                TextView post_title=(TextView)mView.findViewById(R.id.post_title);
                post_title.setText(title);
            }
            public void setImage(Context ctx, String image)
            {
                ImageView post_image=(ImageView)mView.findViewById(R.id.post_image);
                Picasso.with(ctx).load(image).into(post_image);
            }
        }

    }

When looking for the android studio tips to solve the error it just says "Can not resolve method 'findViewById (int)' And in getAplicationContext it says: "Create Getter"

If I inherit AppCompatActivity, I no longer mark an error, but I need to inherit it from Fragment in order to make the bottom

    
asked by Edgar Saenz 20.05.2018 в 09:23
source

2 answers

1

First of all , you have to understand how Context works, in this link you can see the difference and when to use each one . Then you have to understand the difference between a Fragment and a Activity .

According to the "official documentation" :

A Fragment represents a behavior or part of the user interface in a Activity . You can combine multiple fragments in a single activity to create a multipanel IU and reuse a fragment in multiple activities. You can think of a Fragment as a modular section of an activity that has its cycle own life , receives its own input events and that you can add or remove while the activity is running (something like a "sub-activity" that you can use again in different activities).

A Fragment should always be integrated into an activity and the life cycle The fragment is directly affected by the life cycle of the host activity. For example, when the activity is paused , so are all its fragments, and when the activity is destroyed , the same is true for all fragments.

On the contrary, a Activity , is a single and focused activity or screen what the user can do Almost all activities interact with the user, so the Activity class is responsible for creating a window for you where you can place your User Interface setContentView (View) . When a new activity is started, it is placed at the top of the stack and becomes the execution activity: the previous activity always remains below it in the stack and will not appear in the foreground again until the new activity comes out .

In the following image you can see that the Activity define a cycle of life different from the Fragment and every Fragment must be created by a Activity .

Now, as the graph shows and you could read in the documentation, you can not use setContentView because they are only part of the Activity . So obviously it shows you your error.

An example of how the view is inflated in a Fragment (similar to doing setContentView ) by shaping your code should look like this:

... Your code just like this, just delete the onCreate and replace it with this:

public class Home extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        // Inflar o cargar el layout para el Fragment
        View root = inflater.inflate(R.layout.activity_main, container, false);
        // Para hacer findViewById, debes hacerlo con la referencia de root que es tu layout
        // Ejemplo: TextView textView = root.findViewById(R.id.textView);

        mDatabase = FirebaseDatabase.getInstance().getReference().child("Global");
        mDatabase.keepSynced(true);

        mCategList = (RecyclerView) root.findViewById(R.id.myrecycleview);
        mCategList.setHasFixedSize(true);
        mCategList.setLayoutManager(new LinearLayoutManager(this));
        return root;
    }
}

... Your code as it is, changes getApplicationContext() by getContext() in onStart() .

Now, if you already understood how to load or inflate a view into a Fragment , you should know that to start a Activity and a Fragment should also be different. The Activity are initiated via Intent, while the Fragments must be initiated via Transactions to be able to implement its functional life cycle. You can see more information on this answer I did .

    
answered by 20.05.2018 / 11:03
source
0

Do not worry it's a very simple mistake, you're confusing Fragment with Activity, I pass the code to you as it would be for the fragment.

public class Home extends Fragment {

    public RecyclerView mCategList;
    private DatabaseReference mDatabase;

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View v = inflater.inflate(R.layout.R.layout.activity_main, container, false);
 mDatabase = FirebaseDatabase.getInstance().getReference().child("Global");
        mDatabase.keepSynced(true);

        mCategList = (RecyclerView) v.findViewById(R.id.myrecycleview);
        mCategList.setHasFixedSize(true);
        mCategList.setLayoutManager(new LinearLayoutManager(v.getContext()));
return v;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        FirebaseRecyclerAdapter<Categoria, categoriaViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Categoria, categoriaViewHolder>
                (Categoria.class, R.layout.categ_row, categoriaViewHolder.class, mDatabase) {
            @Override
            protected void populateViewHolder(categoriaViewHolder viewHolder, Categoria model, int position) {
            viewHolder.setTitle(model.getTitle());
            viewHolder.setImage(getContext(),model.getImage());
            }
        };
        mCategList.setAdapter(firebaseRecyclerAdapter);
    }



        public static class categoriaViewHolder extends RecyclerView.ViewHolder
        {
            View mView;
            public categoriaViewHolder(View itemView)
            {
                super(itemView);
                mView=itemView;
            }
            public void setTitle(String title)
            {
                TextView post_title=(TextView)mView.findViewById(R.id.post_title);
                post_title.setText(title);
            }
            public void setImage(Context ctx, String image)
            {
                ImageView post_image=(ImageView)mView.findViewById(R.id.post_image);
                Picasso.with(ctx).load(image).into(post_image);
            }
        }

    }
    
answered by 20.05.2018 в 10:52