Android 使用CursorAdapter bindView()设置数据
我知道系统可以多次调用Android 使用CursorAdapter bindView()设置数据,android,android-cursor,Android,Android Cursor,我知道系统可以多次调用bindView(),对此我无能为力 我想在bindView()中设置一些变量值,如 public void bindView(View view, Context context, Cursor cursor) { int salary = cursor.getInt(1); MainActivity.totalSalary += salary; //totalSalary is a static field } 但是,
bindView()
,对此我无能为力
我想在bindView()
中设置一些变量值,如
public void bindView(View view, Context context, Cursor cursor) {
int salary = cursor.getInt(1);
MainActivity.totalSalary += salary; //totalSalary is a static field
}
但是,由于多次调用,数据会损坏和无效
我想不出有什么解决办法。。有什么想法吗?在适配器的
swapCursor
方法中,您可以在光标上循环并计算工资,您可以在MainActivity之外计算工资。totalSalary+=salary;也来自数据库itself@RalphBergmann仅当活动重新启动时,我才使用swapCursor
。(也就是说,调用了onRestart()
)。在其他时候,我从新获取的记录创建游标适配器。因此,基本上,当应用程序创建时,我将无法计算totalSalary
。编辑:我实际上是说我使用的是changeCursor(),而不是swapCursor()。这很关键吗?@Pavan“外部MainActivity.totalSalary+=salary;”是什么意思?是的。。我正在使用一个AsyncTask
我调用的ChangeCursorTask
,它使用rawQuery()
并在其onPostExecute()中调用changeCursor
。我想我可以从那里执行另一个线程,它将再次从数据库中获取数据以获得总工资。我只是想省掉这一切。。有没有更好的方法?可以添加数据获取代码吗