1

I have these codes in my project:

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

            int intPos = getArguments().getInt(ARG_SECTION_NUMBER);
            View rootView;

            rootView = inflater.inflate(R.layout.fragment_main, container,false);

            TableLayout ll = (TableLayout) rootView.findViewById(R.id.tableLayoutList);
            View mTableRow = null;
            Typeface tf = Typeface.createFromAsset(getActivity().getAssets(), "fonts/Far_DastNevis.otf");

            for (int i =1; i <= 2000; ++i)
            {
                mTableRow = (TableRow) View.inflate(getActivity(), R.layout.mrowrayout, null);
                final TextView txtBody = (TextView)mTableRow.findViewById(R.id.txtItem);
                txtBody.setText("Some Text" + i);
                txtBody.setId(i);
                txtBody.setTypeface(tf);
                mTableRow.setTag(i);
                ll.addView(mTableRow);
            }

            return rootView;
        }

It needs some seconds (or minutes) to load and I wanna show process dialog or something like this to users until it load. But I can't use process dialog in onCreateView. Please help me guys.

Arma
  • 152
  • 1
  • 12

6 Answers6

1

You have to use ASyncTask. See http://www.vogella.com/tutorials/AndroidBackgroundProcessing/article.html#concurrency_asynchtask for a very good tutorial. The idea is, to run your long running code in a different thread. If it runs in the UI thread you can't update the UI at all.

Or in your case a Handler might help (same link). First let the system open the dialog (by leaving onCreateView()), the your handler code runs.

Otherwise you app will get ANR reports.

The whole thing is a little bit tricky, because you have to update the UI:

Place the look in doInBackground() and call publishProgress():

protected Integer doInBackground()
....
for (int i = 0; i < 2000; i++) {
         publishProgress(i);
}

Next fill the onProgressUpdate(Integer... progress) function with your UI code.

protected void onProgressUpdate(Integer... progress) {
   // update progress dialog here
   for(int i:progress) {
            mTableRow = (TableRow) View.inflate(getActivity(), R.layout.mrowrayout, null);
            final TextView txtBody = (TextView)mTableRow.findViewById(R.id.txtItem);
            txtBody.setText("Some Text" + i);
            txtBody.setId(i);
            txtBody.setTypeface(tf);
            mTableRow.setTag(i);
            ll.addView(mTableRow);
    }
}
protected void onPostExecute() {
     // close progress dialog here
 }
protected void onPreExecute() {
     // open progress dialog here
 }

This is just meta code, you have to modify it to your needs. And you can update the progress dialog here.

brummfondel
  • 1,202
  • 1
  • 8
  • 11
  • I think it must work but if I use ASyncTask, how I can update my UI? I must use my loop in ASyncTask class?! Something like this: http://stackoverflow.com/questions/14426356/how-to-use-progress-dialog-in-asynctask-in-android ??? – Arma Nov 28 '14 at 13:05
1

There are mainly two ways with that you can solve it.

  1. getActivity() method:- Use getActivity() method where you use context or this keyword.

  2. If you can not access getActivity out of your onCreateView method, then make your View variable global and use its context, eg, if your View itemView. itemView.getContext()

This is the exact what you want.

Gulnaz Ghanchi
  • 485
  • 3
  • 14
  • I can access getActivity() but it doesn't show any process until my loop is finished. – Arma Nov 28 '14 at 12:57
0
ProgressDialog pDialog;
pDialog = new ProgressDialog((Main)context);
pDialog.setMessage("Loading");
pDialog.show();
Andres Cárdenas
  • 720
  • 1
  • 5
  • 26
0

The problem is that you inflate all those Views on the UIThread, and you can solve it but doing it in a AsyncTask as sugested by other posts. But the problem as I see it is that you inflate them at all, 2000 of them!?

I would suggest using a ListView and a Adapter, that would remove the need for the Loading... dialog since there will be no loading at all.

Fredrik Metcalf
  • 307
  • 2
  • 10
0

Define TableLayout ll globally before your class definition..

Call AsyncTask to do execute the part of the code which is taking time. In the onPreExecute of AsyncTask, define the ProgressDialog. You can get the activity by getActivity() call.

In doInBackground populate the text data into a global ArrayList.

In onPostExecute, call an adapter like this...

ll.setAdapter(new llAdapter(getActivity(),yourArrayList));

Create a function llAdapter which extends BaseAdapter. Use this to populate all the data into your tablerow.

May be this will help Android: Customize list view, Table view in adapter

Hope this helps..

Community
  • 1
  • 1
-1

Try This code...

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

                ProgressDialog dialog = new ProgressDialog(getActivity());
                dialog.setMessage("Loading");
                dialog.show();

                int intPos = getArguments().getInt(ARG_SECTION_NUMBER);
                View rootView;

                rootView = inflater.inflate(R.layout.fragment_main, container,false);

                TableLayout ll = (TableLayout) rootView.findViewById(R.id.tableLayoutList);
                View mTableRow = null;
                Typeface tf = Typeface.createFromAsset(getActivity().getAssets(), "fonts/Far_DastNevis.otf");

                for (int i =1; i <= 2000; ++i)
                {
                    mTableRow = (TableRow) View.inflate(getActivity(), R.layout.mrowrayout, null);
                    final TextView txtBody = (TextView)mTableRow.findViewById(R.id.txtItem);
                    txtBody.setText("Some Text" + i);
                    txtBody.setId(i);
                    txtBody.setTypeface(tf);
                    mTableRow.setTag(i);
                    ll.addView(mTableRow);
                }

                return rootView;
            }
Ravi Makvana
  • 2,872
  • 2
  • 24
  • 38
  • just remove dialog.dismiss(); – Ravi Makvana Nov 28 '14 at 13:00
  • This can't work because you are in the UI thread. This won't refresh the UI until onCreateView() has returned. – brummfondel Nov 28 '14 at 13:01
  • this is my code @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.mysnipclip_fragment, container, false); ProgressDialog dialog = new ProgressDialog(getActivity()); dialog.setMessage("Loading"); dialog.show(); System.out.println("onCreateView"); mctx = getActivity(); return v; } – Ravi Makvana Nov 28 '14 at 13:05
  • @Rv Panchal: No it doesn't work. Yes it shows dialog if I remove dialog.dismiss(); but it displays after my loop is finished! I need show dialog before loop is starts until it ends. – Arma Nov 28 '14 at 13:10