ListFragment delay when displaying data
First off thanks to all the users who have made my android developing adventure so much easier. I have, however, come across a situation for which I don't find a solution so here goes:
I have an Activity which contains various Fragments which are intended to interact with each other. These include:
a Fragment containing a GridView populated by a SimpleCursorAdapter from one table in the Sqlite db
// Grid Functional开发者_如何学Pythonity public void PopulateGrid(){ dba = new myDB(getActivity().getApplicationContext()); dba.open(); Cursor c = dba.getRows(); if (c!=null){ String[] columns = new String[]{col_1, col_2, col_3, col_4}; int[] to = new int[]{R.id.grid_id, R.id.grid_desc, R.id.grid_value, R.id.grid_image}; newAdapter = new SimpleCursorAdapter(getActivity(), R.layout.grid_item, c, columns, to); View v = getView(); GridView gv = (GridView)v.findViewById(R.id.sale_grid); gv.setAdapter(newAdapter); gv.setOnItemClickListener(itemSelected); } }
a ListFragment which is populated by another SimpleCursorAdapter from a different table // List Functionality
public void PopulateList(int index){ Log.v("sell list", "Populate list started"); dba.open(); Cursor cursor = dba.getList(index); if (cursor!=null){ String[] columns = new String[]{col_1, col_2, col_3, col_4, col_5}; int[] to = new int[]{R.id.code, R.id.description, R.id.qty, R.id.price}; newAdapter = new SimpleCursorAdapter(getActivity(), R.layout.list_row, cursor, columns, to); this.setListAdapter(newAdapter); } dba.close(); }
When the user selects something from the GridView, the Activity inserts the item in the ListView's table and then reinitializes it.
@Override public void onGridClicked(Long value) { insertItem(value.toString()); } //do stuff public void insertItem(String result) { // Add item dba.addItem(result, qty, index); //Re-populate Panels long start; long end; start = System.currentTimeMillis(); refreshPanels(index); end = System.currentTimeMillis(); Log.v("Refresh List:", "Time"+(end-start)); } private void refreshPanels(int index){ lf.PopulateList(index); ListView lv = lf.getListView(); lv.setSelection(lv.getCount()-1); }
Now for the problem: When I look at the logs and Log each step. This entire process take no more than 50-70 ms but from the time I click on the Grid to the time the ListView is updated I can literally count 2-3 seconds.
It seems that the issue lies in the ListFragment and the time it takes to setListAdapter()
and refresh the View.
I have tried various ways of going about this such as 1. using an Interface to listen for the selection or calling it directly from the GridView fragment 2. calling a secondary function in the ListFragment with either newadapter.changecursor(c)
or newadapter.getCursor().requery()
3. other variants of this...
But it always seems to take forever any help please..
If you make your SQLite operation fast, the UI thread can update the UI faster.
Use beginTransaction()
, setTransactionSuccessful
, endTransaction()
.
thanks for the help, turns out the issue is not in the ListView loading the data, but rather the GridView taking too long to register the click see topic 5197438 Now I gotta figure out a way around it...
精彩评论