我制作演示,其中我显示来自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();
}
}
当Timer启动时我使用了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();
}
这是我的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();
}
}
答案 0 :(得分:2)
这是因为您的适配器仍然保留对调用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());
,然后调用notifyDataSetChanged();
基本上你想要使用一个ArrayList,只是不断从中删除数据并向其中添加新数据。