Alex C
Alex C

Reputation: 17024

How do I tune performance when displaying query results in an android TableView?

I'm writing my first android app and coming from the iOS world. I've got a rather large query (~100k+ rows) that I'd like to display in a Table View with scrolling.

When I built this very same function in an iPhone app, their version of the tableview had automatic handling of JUST the rows that were on screen. This made the display pretty easy to get working.

From what I can see, It appears that the android tableview is loading the entire result and is massively slowing down.

Is there any built in functionality for the android tableview class that enables some form of "lazy loading" or do I have to write a number of handlers and limits into my query?

For example the pseudocode:

 // Event Method:
 //    tableview "onscroll"
 //        get current table view index
 //        query DB with limit "Number of rows to display", Offset firstRowOnScreen
 //        display each of the values in this "subset"

Currently the display code is:

    EditText editText = (EditText) findViewById(R.id.lookupFoodInfo);
    String message = editText.getText().toString();
    android.util.Log.i("FOOO", message);

    String where = "";
    if (message.trim() != "") {
        where = "WHERE Long_Desc LIKE '%"+message.trim()+"%'";
    }

    String sql = "  SELECT bt.Long_Desc " + 
                " FROM BIG_TABLE bt INNER JOIN OTHER_TABLE ot ON bt.id = ot.fk_id " +
                where +
                " ORDER BY ot.value, bt.long_desc ASC";


    Cursor c = sqdb.rawQuery(sql, null);
    if (c != null) {
        c.moveToFirst();
        while (c.isAfterLast() == false) 
        {
            String data = c.getString(c.getColumnIndex("Long_Desc"));
            TableRow row = new TableRow(getApplicationContext());
            row.setLayoutParams(new LayoutParams(
                    LayoutParams.WRAP_CONTENT));

            TextView b3=new TextView(this);
            b3.setText(data);
            b3.setTextColor(Color.BLUE);
            b3.setTextSize(25);
            row.addView(b3);
            table.addView(row);

            c.moveToNext();
        }
    }
    c.close();
    sqdb.close();
    myDbHelper.close();

something very similar in iOS runs like lightning.

Upvotes: 0

Views: 116

Answers (2)

TNR
TNR

Reputation: 5869

you are doing a very big mistake. you can't use a table layout for loading a list of rows. you might be done this as you are coming from ios background. you refer for LIstview in Android which does half of the work for you.

Refer for this link. Every thing explained clearly

Upvotes: 1

smith324
smith324

Reputation: 13060

What you're looking for is a ListView and a CursorAdapter. ListViews have rows which are recycled once they move off screen, so you only have ~ n + 2 (where n is the number of visible rows) instead of one row per item in your database.

http://developer.android.com/guide/topics/ui/layout/listview.html

Upvotes: 1

Related Questions