我做了一個演示,在其中顯示來自webservice的數據。哪個工作正常。當我第一次啓動服務時,我能夠在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 task.and調用webservice的連連
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();
}
這裏我的自定義適配器爲列表視圖
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();
}
}
做yu從同一個類打web服務嗎?先生 – Elltz 2014-10-27 01:41:06