Question

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);

Was it helpful?

Solution

I would consider using:

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top