在Android中异步任务后在TextView中显示项目数

在Android中异步任务后在TextView中显示项目数,android,listview,Android,Listview,我调用了一个web服务,它获取在Android活动的ListView中显示的0-n项 在我的onCreate()中,我有 它填充一个名为customerDataList的对象 在onPostExecute中,我使用一个列表适配器来显示返回的数据 在ListView上方,我希望显示检索到的项目数,但是如果我执行以下操作: new GetData().execute(); TextView numberOfItemsElm = (TextView) findViewById(R.id.number_

我调用了一个web服务,它获取在Android活动的ListView中显示的0-n项

在我的onCreate()中,我有

它填充一个名为customerDataList的对象

在onPostExecute中,我使用一个列表适配器来显示返回的数据

在ListView上方,我希望显示检索到的项目数,但是如果我执行以下操作:

new GetData().execute();
TextView numberOfItemsElm = (TextView) findViewById(R.id.number_of_items);
numberOfItemsElm.setText(customerDataList.size());
显示0,因为该行是在执行完成之前运行的

有什么想法吗

显示0,因为该行是在执行完成之前运行的

那完全正确

所以,移动这些线

TextView numberOfItemsElm = (TextView) findViewById(R.id.number_of_items);
numberOfItemsElm.setText(customerDataList.size());
在列表中设置适配器后,将其添加到您的
onPostExecute()

或者,您可以将设置文本的代码放在方法中,该方法在
onPostExecute()
中调用,以防您在
活动中的其他地方使用该代码

尽管如此,这实际上应该抛出一个
ResourceNotFoundException
,因为您要发送一个int作为参数。应该是这样的

numberOfItemsElm.setText("" + customerDataList.size());

它将把它抛到一根弦上。如果使用
String.valueOf(customerDataList.size())
numberOfItemsElm.setText("" + customerDataList.size());