ListFragment显示数据时的延迟

发布于 2024-11-08 02:18:28 字数 2109 浏览 6 评论 0原文

首先感谢所有让我的 Android 开发之旅变得更加轻松的用户。然而,我遇到了一种没有找到解决方案的情况,所以这里是:

我有一个活动,其中包含旨在相互交互的各种片段。其中包括:

  1. 一个包含 GridView 的片段,该 GridView 由 Sqlite 数据库中的一个表中的 SimpleCursorAdapter 填充

    // 网格功能
    
    公共无效填充网格(){
    dba = new myDB(getActivity().getApplicationContext());
    dba.open();
    光标c = dba.getRows();
    如果(c!=空){
    String[] 列 = 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); 
    
    视图 v = getView();
    GridView gv = (GridView)v.findViewById(R.id.sale_grid);
    gv.setAdapter(newAdapter);
    gv.setOnItemClickListener(itemSelected);
    
    }
    }
    
  2. 一个 ListFragment,由另一个表中的另一个 SimpleCursorAdapter 填充 // 列表功能

    public void PopulateList(int index){
    Log.v("销售列表", "填充列表开始");
    dba.open();
    光标cursor = dba.getList(index);
    if(光标!=空){
    String[] 列 = 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, 光标, 列, to); 
    this.setListAdapter(newAdapter);
    
    }
    dba.close();
    
    }
    
  3. 当用户从 GridView 中选择某些内容时,Activity 会将该项目插入 ListView 的表中,然后重新初始化它。

    <前><代码>@Override 公共无效onGridClicked(长值){ insertItem(value.toString()); } //做事 公共无效insertItem(字符串结果){ // 添加项目 dba.addItem(结果,数量,索引); //重新填充面板 漫长的开始; 长尾; 开始 = System.currentTimeMillis(); 刷新面板(索引); 结束 = System.currentTimeMillis(); Log.v("刷新列表:", "时间"+(end-start)); } 私有无效刷新面板(int索引){ lf.PopulateList(索引); ListView lv = lf.getListView(); lv.setSelection(lv.getCount()-1); }

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

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

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

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:

  1. a Fragment containing a GridView populated by a SimpleCursorAdapter from one table in the Sqlite db

    // 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. 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();
    
    }
    
  3. 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..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浅唱々樱花落 2024-11-15 02:18:28

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

If you make your SQLite operation fast, the UI thread can update the UI faster.
Use beginTransaction(), setTransactionSuccessful, endTransaction().

亽野灬性zι浪 2024-11-15 02:18:28

感谢您的帮助,事实证明问题不在 ListView 加载数据中,而是 GridView 花费太长时间来注册单击查看主题 5197438 现在我必须找出解决方法......

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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文