I have a Fragment
with a TableLayout
. The data in the table is from a SQLite db. The SQLite db is populated from a RESTful webservice in an AsyncTask
in the MainActivity
. The Fragment
must wait for the task to complete before populating the table. The Fragment
listens for the task onPostExecute()
method to be called. When it is, the method onLoadAndStoreComplete()
in the Fragment
is called. This all works.
I need to get a view of a TableLayout
outside the OnCreateView()
method of a Fragment. If I could get the View of the fragment in onLoadAndStoreComplete
that would get me there.
Same code as here.
I've got mContext
from the MainActivity, but that has no getView()
method associated with it.
I've tried:
- making a class member rootView and assigning in onCreateView(), but in onLoadAndStoreComplete()
, it is null.
- making a class member tableLayout and assigning in onCreateView(), but in onLoadAndStoreComplete()
, it is null.
- calling this.getView() again in onLoadAndStoreComplete()
, but it returns null.
- calling the inflator inside onLoadAndStoreComplete()
, which works, but then I don't know what to use for container in the .inflate()
call
I don't understand why the class member values of rootView and tableLayout are null in onLoadAndStoreComplete()
public class MyFragment extends Fragment implements OnLoadAndStoreCompleteListener {
private TableLayout tableLayout;
private View rootView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mContext = this.getActivity();
rootView = inflater.inflate(R.layout.fragment_permits, container, false); // this works
tableLayout = (TableLayout) rootView.findViewById(R.id.main_table); // and this
...
}
@Override
public void onLoadAndStoreComplete() {
// rootView is null, ie not remembered from OnCreateView
View view = getView(); // null
LayoutInflater inflater = LayoutInflater.from(getActivity());
rootView = inflater.inflate(R.layout.fragment_permits, container, false); // container? don't know what it should be
// tableLayout is null
tableLayout.addView(tableRow, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
}
...
}