为什么notifySetDataChanged在android中不起作用?

时间:2014-10-27 03:57:03

标签: java android listview

我制作演示,其中我显示来自webservice的数据。哪个工作正常。我可以在第一次点击服务时在listview上显示数据。但我需要一次又一次地点击服务器并获取新数据。我是能够获得新数据。但我需要在listview上显示新数据,但是当我获得新数据时,我的列表变为空白或空白。为什么?

这是我的代码。

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.departure_dashboard);

    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);
         listObject=data.getData();
        adapter = new CustomListAdapter(this,  listObject);

         listView.setAdapter(adapter);
         timer =new Timer();
         timer.scheduleAtFixedRate(new alertTask(), 0, 14000);
    } 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);
        listObject.clear();


         deparaturedaseboarddto localdata = new Gson().fromJson(result, deparaturedaseboarddto.class);
         listObject.addAll(localdata.getData());
        adapter.notifySetDataChanged();

    }

这是我的listview自定义适配器

package com.firstgroup.components;

import java.util.ArrayList;

import com.firstgroup.applicationload.Appliacationload;
import com.firstgroup.applicationload.R;
import com.firstgroup.dto.deparaturedaseboarddto;


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

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();


    }

}

当我按下它时显示新列表一秒钟而不是关闭应用程序原因?

2 个答案:

答案 0 :(得分:0)

可能是因为您已在BaseAdapter中编写了notifyDataSetChanged(),而在将适配器设置为列表视图后应该理想地调用它。 或者它也可能是因为在基础适配器内你有一个方法

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

但是您没有使用数组适配器实例在方法内部调用notifyDataSetChanged()。 请参阅此tutorial

答案 1 :(得分:0)

尝试使用新数据为列表view.set adapter()分配完整列表视图,重置适配器。抱歉我的缩进