Keshav Agarwal
Keshav Agarwal

Reputation: 13

How can I inflate another view in a fragment?

My app has a tabbed activity with two tabs. I want another view abcd.xml to be inflated in the fragments of the tabs. I tried doing this but it is not working.

This is the code where fragment is extended. IncomeFrag.java

public class IncomeFrag extends Fragment{
    private static final String TAG = "Income Frag";


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

    private void populate() {

        SQLiteDatabase mydatabase = getActivity().openOrCreateDatabase("tracker",MODE_PRIVATE,null);

        Intent intent = getActivity().getIntent();
        int user_id = Integer.parseInt(intent.getStringExtra("user_id"));

        LinearLayout linearLayout = (LinearLayout) getActivity().findViewById(R.id.inc_frag_tab);

        LayoutInflater vi = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        try {
            Cursor rs = mydatabase.rawQuery("select * from transactions where user_id = " + user_id + ";", null);

            if (rs.moveToLast()) {
                do {
                    View pastItem = vi.inflate(R.layout.abcd, null);
                    int amount = rs.getInt(2);
                    String cat = rs.getString(3);
                    String description = rs.getString(4);
                    String type = rs.getString(5);
                    String date = rs.getString(6);

                    if (type.equalsIgnoreCase("income")) {
                        TextView amt_tv = (TextView) pastItem.findViewById(R.id.amount_inflator);
                        amt_tv.setText(Integer.toString(amount));
                        amt_tv.setTextColor(getResources().getColor(R.color.green));
                        TextView cat_tv = (TextView) pastItem.findViewById(R.id.category_inflator);
                        cat_tv.setText(cat);
                        TextView desc_tv = (TextView) pastItem.findViewById(R.id.description_inflator);
                        desc_tv.setText(description);
                        TextView date_tv = (TextView) pastItem.findViewById(R.id.date_inflator);
                        date_tv.setText(date);

                        linearLayout.addView(pastItem);
                    }
                } while (rs.moveToPrevious());
            }
            rs.close();
        }
        catch (Exception e) {
            // Nothing
        }
    }
}

This is the xml file of the view i want to inflate abcd.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="7sp">

    <TextView
        android:id="@+id/amount_inflator"
        android:text="@string/username"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="25sp"
        android:textColor="#FF0000"
        android:paddingBottom="3sp"/>

    <TextView
        android:id="@+id/category_inflator"
        android:text="abcd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:paddingBottom="2sp"/>
    <TextView
        android:id="@+id/description_inflator"
        android:text="@string/app_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/date_inflator"
        android:text="@string/app_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <android.support.v4.widget.Space
        android:layout_width="match_parent"
        android:layout_height="15sp" />

</LinearLayout>

and i want it to be inflated here

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:id="@+id/inc_frag_tab"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    </LinearLayout>

</ScrollView>

</LinearLayout>

Upvotes: 1

Views: 2852

Answers (1)

Andreas Engedal
Andreas Engedal

Reputation: 801

You cannot access views like LinearLayout or TextView from the onCreateView() method, when working with fragments. Try putting the populate() method inside onViewCreated() instead like this, and see if it works.

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

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    populate();

Upvotes: 4

Related Questions