当XML中的数据从服务器更改时自动刷新列表视图

时间:2015-05-18 13:55:09

标签: java android xml xml-parsing

这是我从服务器解析XML并在列表视图中显示的代码。我希望自动刷新在服务器更改数据时显示列表视图。

package com.wafik;

import java.io.InputStream;
import java.net.URL;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.widget.ListView;

public class vctcs extends Activity {
    /** Called when the activity is first created. */
    private ListView listView;



    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        listView = (ListView) findViewById(R.id.listview);
        bindDataToListing();
}

    public void bindDataToListing() {
        try {
            SAXParserFactory saxparser = SAXParserFactory.newInstance();
            SAXParser parser = saxparser.newSAXParser();
            XMLReader xmlReader = parser.getXMLReader();
            ParsingClass pc = new ParsingClass();
            xmlReader.setContentHandler(pc);

                URL url=new URL("my url"); 
                InputStream is=url.openStream(); // Use this InputStream to parse 

            xmlReader.parse(new InputSource(is));
            BindingData bindingData = new BindingData(this, pc.name,
                    pc.address, pc.qua,pc.qua2);
            listView.setAdapter(bindingData);

        } catch (Exception e) {
            e.getMessage();
        }
    }


}

解析XML

package com.wafik;

import java.util.ArrayList;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class ParsingClass extends DefaultHandler {

    ArrayList<String> name = new ArrayList<String>();
    ArrayList<String> address = new ArrayList<String>();
    ArrayList<String> qua = new ArrayList<String>();
    ArrayList<String> qua2 = new ArrayList<String>();

    @Override
    public void startElement(String uri, String localName, String qName,
            Attributes attributes) throws SAXException {
        super.startElement(uri, localName, qName, attributes);
        if (localName.equalsIgnoreCase("time")) {
            tempStore = "";
        } else if (localName.equalsIgnoreCase("street_name")) {
            tempStore = "";
        } else if (localName.equalsIgnoreCase("intersection_no")) {
            tempStore = "";
        } else if (localName.equalsIgnoreCase("traffic_state")) {
                tempStore = "";
        }else{
            tempStore = "";
        }
    }

    @Override
    public void endElement(String uri, String localName, String qName)
            throws SAXException {
        super.endElement(uri, localName, qName);
        if (localName.equalsIgnoreCase("time")) {
            name.add(tempStore);
        } else if (localName.equalsIgnoreCase("street_name")) {
            address.add(tempStore);
        } else if (localName.equalsIgnoreCase("intersection_no")) {
            qua.add(tempStore);
        }else if (localName.equalsIgnoreCase("traffic_state")) {
            qua2.add(tempStore);
        }
        tempStore = "";
    }

    private String tempStore = "";

    @Override
    public void characters(char[] ch, int start, int length)
            throws SAXException {
        super.characters(ch, start, length);
        tempStore += new String(ch, start, length);
    }
}

绑定数据

package com.wafik;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class BindingData extends BaseAdapter {
    ArrayList<String> time;
    ArrayList<String> street_name;
    ArrayList<String> intersection_no;
    ArrayList<String> traffic_state;
    LayoutInflater inflater;

    public BindingData() {

    }

    public BindingData(Activity act, ArrayList<String> name,
            ArrayList<String> add, ArrayList<String> qua,ArrayList<String> qua2) {
        this.time = name;
        this.street_name = add;
        this.intersection_no = qua;
        this.traffic_state = qua2;
        inflater = (LayoutInflater) act
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return time.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Holder holder;
        if (convertView == null) {
            holder = new Holder();
            convertView = inflater.inflate(R.layout.listrow, null);
            holder.txttime = (TextView) convertView.findViewById(R.id.name);
            holder.txtstreet_name = (TextView) convertView.findViewById(R.id.address);
            holder.txtintersection_no = (TextView) convertView.findViewById(R.id.quali);
            holder.txttraffic_state = (TextView) convertView.findViewById(R.id.quali2);
            convertView.setTag(holder);
        } else {
            holder = (Holder) convertView.getTag();
        }
        holder.txttime.setText(Html.fromHtml("<b>Time : </b>" + time.get(position)));
        holder.txtstreet_name.setText(Html.fromHtml("<b>Street Name : </b>"
                + street_name.get(position)));
        holder.txtintersection_no.setText(Html.fromHtml("<b>Intersection No : </b>"
                + intersection_no.get(position)));
        holder.txttraffic_state.setText(Html.fromHtml("<b>Traffic State : </b>"
                + traffic_state.get(position)));

        return convertView;
    }

    private class Holder {
        TextView txttime, txtstreet_name, txtintersection_no,txttraffic_state;
    }
}

2 个答案:

答案 0 :(得分:0)

首先,您需要一个对象来保存所有这些值,如时间,streername等,然后传递该对象的列表,然后在适配器中创建一个π方法,用新的 xml更新列表致电add item以更新您的notifyDataSetChanged()

// 绑定数据

listview

// YourItem

        public void addItem(YourItem item){
        this.yourItemList.add(item);
        notifyDataSetChanged();
    }

答案 1 :(得分:0)

如果要在列表视图中更改实例,则必须通知列表适配器数据已更改,因此每次更改服务器数据时都应调用...

bindingData.notifyDataSetChanged();