我目前正在开发一个生成列表的 android 应用程序。此列表是从创建 xml 文件的 php 站点生成的。这一切都很好,我得到了结果。但是当更新 php 站点时,我的 android 应用程序中的结果当然不会更新。有人可以解释一下我如何让我的 android 应用程序每分钟自动更新列表。
我已经查看了警报管理器,但我没有成功自动刷新我的列表。
请帮忙...
这是我现在用来生成列表的代码:
package com.pxr.tutorial.xmltest;
import java.util.ArrayList;
import java.util.HashMap;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import android.app.ListActivity;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
public class Main extends ListActivity {
private ServiceCount count;
@Override
public void onCreate(Bundle savedInstanceState) {
count = new ServiceCount((long) (1 * 60 * 1000), 1000); // 1 minute
count.start();
super.onCreate(savedInstanceState);
setContentView(R.layout.listplaceholder);
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
String xml = XMLfunctions.getXML();
Document doc = XMLfunctions.XMLfromString(xml);
int numResults = XMLfunctions.numResults(doc);
if ((numResults <= 0)) {
Toast.makeText(Main.this, "Geen resultaten gevonden",
Toast.LENGTH_LONG).show();
finish();
}
NodeList nodes = doc.getElementsByTagName("result");
for (int i = 0; i < nodes.getLength(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) nodes.item(i);
map.put("id", XMLfunctions.getValue(e, "id"));
map.put("beschrijving", XMLfunctions.getValue(e, "beschrijving"));
map.put("type", XMLfunctions.getValue(e, "type"));
map.put("datum", XMLfunctions.getValue(e, "datum"));
mylist.add(map);
}
ListAdapter adapter = new SimpleAdapter(this, mylist, R.layout.main,
new String[] { "beschrijving", "type", "datum" }, new int[] {
R.id.item_title, R.id.item_subtitle,
R.id.item_subtitle1 });
setListAdapter(adapter);
final ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
@SuppressWarnings("unchecked")
HashMap<String, String> o = (HashMap<String, String>) lv
.getItemAtPosition(position);
Toast.makeText(Main.this,
"ID '" + o.get("id") + "' was clicked.",
Toast.LENGTH_LONG).show();
}
});
}
private class ServiceCount extends CountDownTimer
{
public ServiceCount(long millisInFuture, long countDownInterval)
{
super(millisInFuture, countDownInterval);
}
@Override
public void onFinish()
{
count = new ServiceCount((long) (1 * 60 * 1000), 1000); count.start(); //start timer another time
mylist.clear();
adapter.notifyDataSetChanged();
}
@Override
public void onTick(long millisUntilFinished)
{
Log.d("timer", "" + millisUntilFinished / 1000);
}
}
}