【问题标题】:ListFragment delay when displaying dataListFragment 显示数据时的延迟
【发布时间】:2011-05-18 00:02:42
【问题描述】:

首先感谢所有让我的 android 开发冒险变得如此轻松的用户。但是,我遇到了一种我找不到解决方案的情况,所以这里是:

我有一个 Activity,其中包含旨在相互交互的各种 Fragment。其中包括:

  1. 包含 GridView 的 Fragment 由 Sqlite db 中的一个表中的 SimpleCursorAdapter 填充

    // Grid Functionality
    
    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);
    
    }
    }
    
  2. 由另一个表中的另一个 SimpleCursorAdapter 填充的 ListFragment // 列表功能

    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();
    
    }
    
  3. 当用户从 GridView 中选择某些内容时,Activity 会将该项插入到 ListView 的表中,然后重新初始化它。

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

现在解决问题:当我查看日志并记录每个步骤时。整个过程不超过 50-70 毫秒,但从我单击 Grid 到更新 ListView 的时间,我可以计算 2-3 秒。 似乎问题在于 ListFragment 以及 setListAdapter() 并刷新视图所需的时间。

我尝试了各种方法来解决这个问题,例如 1. 使用接口来监听选择或直接从 GridView 片段调用它 2. 使用 newadapter.changecursor(c)newadapter.getCursor().requery() 在 ListFragment 中调用辅助函数3. 这个的其他变种...

但它似乎总是需要任何帮助..

【问题讨论】:

  • 使用 Traceview 确定您的困难来源。 developer.android.com/guide/developing/tools/traceview.html
  • @CommonsWare 好的,我从 GridView 的 itemselected 侦听器开始添加了一个跟踪视图,并在 insertItem() 完成后结束。这给了我 157 毫秒的执行时间,但是一旦这个过程完成,在屏幕上显示之前仍然会有延迟
  • 在该窗口期间不会调用加载内容的查询。当 Android 开始使用您的 SimpleCursorAdapter 时将调用它。要强制在所需窗口期间进行查询,请在 PopulateList() 内部的 SimpleCursorAdapter 上调用 getCount()。顺便说一句,Java 约定是方法名称为 camelCaseWithLeadingLowercaseLetter()
  • @CommonsWare 感谢您的帮助,原来问题不在于 ListView 加载数据,而是 GridView 注册点击的时间太长,请参阅topic 5197438 现在我得想办法在它周围......

标签: android android-listview android-fragments simplecursoradapter


【解决方案1】:

如果你让你的 SQLite 操作更快,UI 线程可以更快地更新 UI。 使用beginTransaction()setTransactionSuccessfulendTransaction()

【讨论】:

    【解决方案2】:

    感谢您的帮助,原来问题不在于 ListView 加载数据,而在于 GridView 注册点击的时间太长,请参阅主题 5197438 现在我得想办法解决它...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-04-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-20
      • 2011-03-12
      相关资源
      最近更新 更多