gpt4 book ai didi

java - 如何在固定时间间隔后刷新listview?

转载 作者:行者123 更新时间:2023-12-02 05:20:05 24 4
gpt4 key购买 nike

我制作了演示,其中显示来自网络服务的数据。它工作正常。这意味着当我第一次访问服务时,我能够在 ListView 上显示数据。但是我需要一次又一次地访问服务器并获取新数据。我是能够获取新数据。但我需要在 ListView 上显示新数据,但当我获取新数据时,我的列表变为空白或为空。为什么?

这是我的代码。

protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.departure_dashboard);
Log.d("=========onCreate", "constructor");

try {
Bundle bundle = getIntent().getExtras();
String message = bundle.getString("Response");
Log.d("=========onCreate", message);
listView=(ListView)findViewById(R.id.listView1);
data = new Gson().fromJson(message, deparaturedaseboarddto.class);
adapter = new CustomListAdapter(this, data.getData());

listView.setAdapter(adapter);
timer =new Timer();
timer.scheduleAtFixedRate(new alertTask(), 0, 4000);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

当计时器启动时,我使用了alerk任务,并一次又一次调用web服务

public class alertTask extends TimerTask {

@Override
public void run() {
// TODO Auto-generated method stub
WebserviceMethod callDepartudeDashboard=new WebserviceMethod();
callDepartudeDashboard.setObserver(Departuredashboardscreen.this);
callDepartudeDashboard.getwebService(ConstantVariable.dashboardWebServiceURL+"a/"+"departuredashboard"+"?crsCode=hnh");


}

}

这是一次又一次给出响应的方法...

@Override
public void getWebserviceResponse(String result) {
// TODO Auto-generated method stub
Log.d("timer", result);
data.getData().clear();


deparaturedaseboarddto localData = new Gson().fromJson(result, deparaturedaseboarddto.class);
data=localData
adapter.notifySetDataChanged();

}

这是我的 ListView 自定义适配器

public class CustomListAdapter extends BaseAdapter{
ArrayList<deparaturedaseboarddto> deparaturedaseboarddto;
private Context context;
public CustomListAdapter( Context context, ArrayList<deparaturedaseboarddto> deparaturedaseboarddto){
this.deparaturedaseboarddto=deparaturedaseboarddto;
this.context = context;
}

@Override
public int getCount() {
// TODO Auto-generated method stub
return deparaturedaseboarddto.size();
}

@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return deparaturedaseboarddto.get(position);
}

@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View v = convertView;
if (v == null) {
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = mInflater.inflate(R.layout.row_listitem, null);
}

final TextView platFormName = (TextView) v
.findViewById(R.id.item_platform);

final TextView schDepart = (TextView) v
.findViewById(R.id.item_schDepart);
final TextView expDepart = (TextView) v
.findViewById(R.id.item_expectdepart);
final TextView arrival = (TextView) v
.findViewById(R.id.item_arrival);
final TextView exparrival = (TextView) v
.findViewById(R.id.item_expertarrival);
final TextView stationName = (TextView) v
.findViewById(R.id.item_stationName);

final String platformValue = deparaturedaseboarddto.get(position).getPlatformNo();
final String schDepartValue= deparaturedaseboarddto.get(position).getSchDepart();
final String schExpectValue= deparaturedaseboarddto.get(position).getExpDepart();
final String arrivalValue= deparaturedaseboarddto.get(position).getDestSchArrival();
final String exparrivalValue= deparaturedaseboarddto.get(position).getDestSchArrival();
String stationNameValue= deparaturedaseboarddto.get(position).getDestinationStation().getStationName();

platFormName.setText(platformValue);

schDepart.setText(schDepartValue);
expDepart.setText(schExpectValue);
arrival.setText(arrivalValue);
exparrival.setText(exparrivalValue);
stationName.setText(stationNameValue);
return v;
}

public void notifySetDataChanged() {
// TODO Auto-generated method stub
notifyDataSetChanged();
}

}

最佳答案

这是因为您的适配器仍然保留对数据集的旧引用,当您调用 data.getData().clear();

时该数据集被清空

澄清一下,当您第一次创建适配器时,

adapter = new CustomListAdapter(this, data.getData());

您正在将 ArrayList data.getData() 传递给您的适配器,这就是您的适配器将从中读取数据的 ArrayList。

现在,当您收到新数据时,您将数据指向 localData,但您的适配器不知道这一点。

@Override
public void getWebserviceResponse(String result) {
// TODO Auto-generated method stub
Log.d("timer", result);
data.getData().clear();
deparaturedaseboarddto localData = new Gson().fromJson(result,deparaturedaseboarddto.class);
data=localData;
adapter.notifySetDataChanged();
}

因此,您不应执行 data = localData;,而应该执行 data.addAll(localData.getData());,然后调用 notificationDataSetChanged();

基本上,您想使用一个 ArrayList,然后不断地从中删除数据并向其中添加新数据。

关于java - 如何在固定时间间隔后刷新listview?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26579465/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com