2

There is a WebService which returns a JSON string of a 500 - row 2darray when requested. The array size is approximately 45KB. The average time needed to populate the data is 20 seconds.

This is how it's done:

public View PopulateData(String json_data)
{
    ja = new JSONToStringArray(json_data);
    number_of_columns = ja.getColumns();
    al = ja.getArrayList();

    int column_num = 0;
    int row_num = -1;
    Activity activity = (Activity)main;

    table.setId(CTlist.indexOf(CT));

    initialState = new String[(al.size()/number_of_columns)][number_of_columns];

    for (String item : al)
    {   
        if (column_num % number_of_columns == 0)
        {
            row = new TableRow(main);
            row.setGravity(Gravity.CLIP_HORIZONTAL);
            row.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
            row.setClipChildren(true);
            row.setId(10000 + row_num);
            table.addView(row);             
            column_num = 0;
            row_num++;
        }       




        et = new EditText(main);
        et.setLayoutParams(new TableRow.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
        et.setInputType(InputType.TYPE_CLASS_TEXT|InputType.TYPE_TEXT_FLAG_MULTI_LINE);
        int px = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 150, activity.getResources().getDisplayMetrics());
        et.setMaxWidth(px);
        et.setText(item);
        et.setId(column_num);
        et.setBackgroundResource(R.drawable.rounded_rect);


        initialState[row_num][column_num] = item;


        et.setOnFocusChangeListener(new View.OnFocusChangeListener() {

            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                // TODO Auto-generated method stub
                if (!(v instanceof EditText))
                {                       
                    return;
                }
                EditText tableEt = (EditText)v;

                if(hasFocus)
                {
                    tableEt.setTag(tableEt.getText().toString());
                    tableEt.setText("");
                }
                else
                {
                    if(tableEt.getText().toString().equals(""))
                    {
                    tableEt.setText(tableEt.getTag().toString());
                    tableEt.setTag(null);
                    }

                }
            }
        });

        et.setOnEditorActionListener(new OnEditorActionListener() {

            @Override
            public boolean onEditorAction(TextView arg0, int arg1,
                    KeyEvent arg2) {
                // TODO Auto-generated method stub
                if (arg2.getKeyCode() == KeyEvent.KEYCODE_ENTER)
                {
                    if(arg2.isAltPressed())
                        {
                            return true;
                        }
                    View v = arg0.focusSearch(View.FOCUS_DOWN);
                    if(!(v instanceof EditText))
                    {
                        KeyEvent.changeAction(arg2, KeyEvent.FLAG_CANCELED);
                        return true;
                    }
                    EditText etBelow = (EditText)v;
                    if (etBelow != null)
                    {
                        if (arg0.getText().toString().equals(""))
                        {

                            arg0.setText(arg0.getTag().toString());
                            arg0.setTag("");
                        }
                        etBelow.requestFocus();     
                    }
                    KeyEvent.changeAction(arg2, KeyEvent.FLAG_CANCELED);
                }
                return true;
            }


        }); 

        row.addView(et);
        column_num++;

    }


    view = table;
    return view;
}

Anything is acceptable, even completely changing the way I'm doing things, as long as it fills my table fast.

The delay is mainly caused by et.SetText(item); followed by row.AddView(et);

Mitsosp
  • 399
  • 3
  • 17
  • 1
    Instead of JSONToStringArray , can you use model classes and objects then check:http://stackoverflow.com/questions/21480634/unable-to-loop-through-dynamic-json-string-recursively-in-android/21480997#21480997 Good to see the for each loop use in your code – Pararth Feb 07 '14 at 09:01
  • @user2450263 JSONToStringArray completes in roughly 0.6 seconds, so it's not the main drawback :) Thanks for the info though, it's helpful to see that i can improve that as well :P – Mitsosp Feb 07 '14 at 09:04

1 Answers1

4

I would consider using:

Yuichi Araki
  • 3,438
  • 1
  • 19
  • 24
  • ListView or GridView are indeed faster but they are still slow. The cause is similar to mine, the EditText.SetText() is consuming the biggest part of execution, time wise. So i prefer keeping these extra 2 seconds and get more options about Layout formatting - styling which i get by using Table Layout. As for the rest can you explain to me how exactly the Content Provider helps? It would be a great help – Mitsosp Feb 07 '14 at 10:14
  • In your code above, you are inflating all the views in TableLayout. EditText.setText() is consuming the biggest part because you are calling it too many times. By using ListView, you can only inflate views for visible items. Here is the detailed instruction. http://developer.android.com/guide/topics/ui/layout/listview.html – Yuichi Araki Feb 10 '14 at 02:32
  • Oh, i see, seems that I've been using the ListView wrongly. Thanks a lot, ListView will do ;). – Mitsosp Feb 10 '14 at 10:59