2016-12-15 49 views
0

我有一个EditText,它用于获取用户输入。用户输入特定数据后,与EditText关联的文本更改侦听程序将调用刷新的光标,并尝试更新显示在正下方的ListView中的结果。ListView在Sqlite数据库光标更新时频繁挂起

一切都很好。但是,无论何时发生搜索查询中的任何更改,所产生的光标和更新都需要一些时间,例如n秒。在这段时间内,用户界面停止(暂停/挂起您可能调用的任何内容),直到刷新的光标可用并且整个填充完整为止。

当我尝试将游标更新置于不同的线程中时,它不允许将相同的内容反映到UI中,因为UI线程不允许被其他线程指挥。任何UI活动(如列表更新)都必须通过的MainActivity类实现。

请建议我允许EditText被用户修改的方式,以及刷新ListView的更新游标,而不影响前者。

回答

0

基本上,你正在尝试错误的方法。当我们希望列表的数据直接来自SQLite数据库查询时,我们可以使用CursorAdapter。

创建适配器

public class MyCursorAdapter extends CursorAdapter { 

    // Default constructor 
    public MyCursorAdapter(Context context, Cursor cursor, int flags) { 
     ... 
    } 

    public void bindView(View view, Context context, Cursor cursor) { 
     ... 
    } 

    public View newView(Context context, Cursor cursor, ViewGroup parent) { 
     ... 
     return null; 
    } 
} 

Get Values from database 
Cursor todoCursor = db.rawQuery("SELECT * FROM todo_items", null); 

现在,我们可以使用CursorAdapter在活动显示项目的数组到ListView中:

// Find ListView to populate 
ListView lvItems = (ListView) findViewById(R.id.lvItems); 
// Setup cursor adapter using cursor from last step 
TodoCursorAdapter todoAdapter = new TodoCursorAdapter(this, todoCursor); 
// Attach cursor adapter to the ListView 
lvItems.setAdapter(todoAdapter); 
This will then trigger the CursorAdapter iterating through the result set and populating the list. We can change the cursor to update the adapter at any time with: 

// Switch to new cursor and update contents of ListView 
todoAdapter.changeCursor(newCursor); 
+0

我已经使用一个CursorAdaptor并在bindView方法,我已经完成了所有必须完成的工作。但是,在为返回400行的给定查询获取结果期间,需要一定的时间来处理其他UI功能处于挂起状态。 –

+0

你用过textChangeListner吗? –

+0

使用内容obsever查看反映更改。 400条记录无关紧要。你可以使用限制和分页 –