问题
I used the code below to refresh a List Field with data returned from an xml web service every minute. But after first refresh, the data duplicates. How do I prevent it from duplicating?
public final class MyScreen extends MainScreen implements ListFieldCallback
{
ResponseHandler handler = new ResponseHandler();
ListField listUsers = new ListField(handler.getItem().size());
Manager mainManager = getMainManager();
protected void onUiEngineAttached(boolean attached) {
if (attached) {
Timer timer = new Timer();
// schedule the web service task to run every minute
timer.schedule(new WebServiceTask(), 0, 3000);
}
}
public MyScreen() {
setTitle("Uikro");
listUsers.setEmptyString("No Users found", 0);
listUsers.setCallback(this);
add(listUsers);
handler.getItem().removeAllElements();
}
private class WebServiceTask extends TimerTask {
public void run() {
//Fetch the xml from the web service
String wsReturnString = GlobalV.Fetch_Webservice("myDs");
//Parse returned xml
SAXParserImpl saxparser = new SAXParserImpl();
ByteArrayInputStream stream = new ByteArrayInputStream(wsReturnString.getBytes());
try {
saxparser.parse( stream, handler );
}
catch ( Exception e ) {
response.setText( "Unable to parse response.");
}
// now, update the UI back on the UI thread:
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
//Return vector sze from the handler class
int currentIndex = listUsers.getSelectedIndex();
int scrollPosition = getMainManager().getVerticalScroll();
//Return vector sze from the handler class
listUsers.setSize(handler.getItem().size());
listUsers.setSelectedIndex(currentIndex);
getMainManager().setVerticalScroll(scrollPosition);
}
});
}
}
public void drawListRow(ListField list, Graphics g, int index, int y, int w) {
String personToDraw = (String) this.get(list, index);
int drawColor = Color.RED;
g.setColor(drawColor);
g.drawText(personToDraw, 0, y, 0, w);
}
// get the selected index from the correct Vector
public Object get(ListField list, int index) {
return handler.getItem().elementAt(index);
}
public void insert(String toInsert, int index) {
handler.getItem().insertElementAt(toInsert, index);
}
public int getPreferredWidth(ListField list) {
return Display.getWidth();
}
public int indexOfList(ListField listField, String prefix, int start) {
return handler.getItem().indexOf(prefix, start);
}
}
public class ResponseHandler extends DefaultHandler
{
boolean username=false;
Vector _vectorUser = new Vector();
public void startElement(String uri, String localName, String qName, Attributes attributes){
if (qName.equalsIgnoreCase("U_Username")){
username = true;
}
}
public void characters(char[] ch, int start, int length) {
if (username){
_vectorUser.addElement(new String( ch, start, length ));
username=false;
}
}
Vector getItem(){
return _vectorUser;
}
}
回答1:
So, I think the problem is in the ResponseHandler
class. That class uses a member variable _vectorUser
to store the data. The vector is created when the ResponseHandler
is created, and elements are added when the characters(char[],int,int)
method is called.
But, _vectorUser
is never really cleared out (it's cleared before it's ever used, but that doesn't prevent this problem).
So, either you should clear out the vector each time you ask the web service for new data:
private class WebServiceTask extends TimerTask {
public void run() {
//Fetch the xml from the web service
String wsReturnString = GlobalV.Fetch_Webservice("myDs");
//Parse returned xml
SAXParserImpl saxparser = new SAXParserImpl();
ByteArrayInputStream stream = new ByteArrayInputStream(wsReturnString.getBytes());
try {
// clear out the vector first
handler.getItem().removeAllElements();
saxparser.parse( stream, handler );
}
catch ( Exception e ) {
response.setText( "Unable to parse response.");
}
or, just create a new ResponseHandler
, which will also create a new vector:
private class WebServiceTask extends TimerTask {
public void run() {
//Fetch the xml from the web service
String wsReturnString = GlobalV.Fetch_Webservice("myDs");
//Parse returned xml
SAXParserImpl saxparser = new SAXParserImpl();
ByteArrayInputStream stream = new ByteArrayInputStream(wsReturnString.getBytes());
try {
// create a new handler
handler = new ResponseHandler();
saxparser.parse( stream, handler );
}
catch ( Exception e ) {
response.setText( "Unable to parse response.");
}
来源:https://stackoverflow.com/questions/14086650/how-to-prevent-a-list-field-from-showing-duplicate-data-after-auto-refresh