inrob
inrob

Reputation: 5089

adding dynamic content on fragments android

I'm new to fragments and they are a bit confusing to me right now. I have read alot and actually built a test app using this tutorial

I was able to add more tabs and so on. But I don't want to display static content (xmls). I want to be able to modify the UI add listviews, load json data with asynctasck and so on but the very first attempt I made has failed.

So on Tab1:

public class Tab1 extends Fragment {

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

}

I tried to declare a textview and edit it's text with settext but I get a crash and also findviewbyid is not available as a method. It didn't throw and error when I typed getView().findViewById but that's not the problem.

Are fragments limited or very different from traditional activities? Do I need to make never ending customizations in order to get a listview loading?

Thank you.

Upvotes: 2

Views: 2479

Answers (3)

kandroidj
kandroidj

Reputation: 13932

You should declare your TextView as a Member variable for your class and get a reference to that view in onCreateView like:

public class Tab1 extends Fragment {

    // your TextView member variable
    private TextView mTextView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v =inflater.inflate(R.layout.tab_1, container, false);
        mTextView  = (TextView) v.findViewById(R.id.my_text_view);
        return v;

    }

    // It is safe to access the views here, not in onCreate since it is called before onCreateView
    @Override
    public void onActivityCreated(Bundle savedInstanceState){
        super.onActivityCreated(savedInstanceState);

        mTextView.setText("Hello, World!");

    }
}

For reference on the Fragment lifecycle

Are fragments limited or very different from traditional activities?

A Fragment has its own lifecycle like that of an Activity but it is dependent upon the lifecycle of its parent Activity, so to answer your question they are not very different, it is just a good practice for modularizing code.

Do I need to make never ending customizations in order to get a listview loading?

A little unclear what you mean by never ending customizations but loading a dynamic ListView is very simple in Android.

For Example:

using fragment_one.xml we have just a FrameLayout

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fl_list_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

And the code:

public class Tab1 extends Fragment {

    // your FrameLayout member variable
    private FrameLayout mFlParent;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_one, container, false);
        mFlParent  = (FrameLayout) v.findViewById(R.id.fl_list_container);
        return v;

    }

    // It is safe to access the views here, not in onCreate since it is called before onCreateView
    @Override
    public void onActivityCreated(Bundle savedInstanceState){
        super.onActivityCreated(savedInstanceState);

        // Create a new list, adapter and add a item click listener
        ListView myList = new ListView(context); // context needed
        myList.setAdapter(new ArrayAdapter<String>(context, android.R.id.simple_list_item_1, new String []{"Item 1", "Item 2", "Item 3", "Item 4"}));
        myList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //... handle clicks on list items as usual
            }
        });

        // add the view to the FrameLayout
        mFlParent.addView(myList); // may want to call mFlParent.removeAllViews(); before adding just to be safe  
    }
}

Good luck and happy coding.

Upvotes: 2

Deepak
Deepak

Reputation: 774

There is basically 2 method to be use in fragment 1. onCreateView 2. onViewCreated

First one is use to inflate layout on your fragment Second one is use to find all view used in your layout.

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

}



@Override
public void onViewCreated(View v){
    mTextView  = (TextView) v.findViewById(R.id.my_text_view);
    mTextView.setText("Hello, World!");

}

Upvotes: 0

Yuriy Kolbasinskiy
Yuriy Kolbasinskiy

Reputation: 3841

Fragment doesn't have an activity methods.

If you want to use findViewById methods, you should use a class field.

public class Tab1 extends Fragment {

private View mView;

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

@Override 
public void onCreate() {
    // here you can use `findViewById` method via `mView` variable
    mView.findViewById(YOUR_VIEW_ID);
}
}

Upvotes: 0

Related Questions