我創建了主/明細流應用程序,即連接到URL以獲取JSON。然後解析此JSON,以獲取命令列表及其所有細節。我想刷新項目列表,而不需要用戶採取任何行動。我知道有一種簡單的方法可以用JSON數據創建列表,但我如何每10秒更新一次?自動更新和刷新主/明細流
編輯:
Timer timerTask = new Timer();
timerTask.schedule(new TimerTask() {
@Override
public void run() {
try{
DummyContent.ITEMS.clear();
GetURLContentTask eu = new GetURLContentTask();
String JSONString = eu.execute(url).get();
System.out.println("debug h " + JSONString);
JSONArray jArray = new JSONArray(JSONString);
System.out.println("debug *****JARRAY*****"+jArray.length());
if (!jArray.equals(null)) {
for(int i=0;i<jArray.length();i++){
JSONObject json_data = jArray.getJSONObject(i);
System.out.println("debug unique id : " + json_data.getString("unique_id") +
", point de vente : " + json_data.getString("pdv") +
", panier : " + json_data.getString("panier"));
DummyContent.addItem(new DummyContent.DummyItem(json_data.getString("unique_id"), json_data.getString("pdv"), json_data.getString("panier")));
// .notifyDataSetChanged();
}
}
}catch (Exception e){System.out.println("error: "+e);}
}
}, 10000, 10000);
我必須使用notifyDataSetChanged(),但我不知道whento調用它。我該怎麼做呢 ?
編輯2: 這是ItemListFragment.java:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// TODO: replace with a real list adapter.
setListAdapter(new ArrayAdapter<DummyContent.DummyItem>(
getActivity(),
android.R.layout.simple_list_item_activated_1,
android.R.id.text1,
DummyContent.ITEMS));
}
這是我ItemListActivity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_commande_app_bar);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
toolbar.setTitle(getTitle());
Timer timerTask = new Timer();
timerTask.schedule(new TimerTask() {
@Override
public void run() {
try{
DummyContent.ITEMS.clear();
GetURLContentTask eu = new GetURLContentTask();
String JSONString = eu.execute(url).get();
System.out.println("debugTT h " + JSONString);
JSONArray jArray = new JSONArray(JSONString);
System.out.println("debugTT *****JARRAY*****"+jArray.length());
if (!jArray.equals(null)) {
for(int i=0;i<jArray.length();i++){
JSONObject json_data = jArray.getJSONObject(i);
System.out.println("debugTT unique id : " + json_data.getString("unique_id") +
", point de vente : " + json_data.getString("pdv") +
", panier : " + json_data.getString("panier"));
DummyContent.addItem(new DummyContent.DummyItem(json_data.getString("unique_id"), json_data.getString("pdv"), json_data.getString("panier")));
CommandeListFragment.adapter.notifyDataSetChanged();
}
}
}catch (Exception e){System.out.println("error: "+e);}
}
}, 10000, 10000);
if (findViewById(R.id.commande_detail_container) != null) {
// The detail container view will be present only in the
// large-screen layouts (res/values-large and
// res/values-sw600dp). If this view is present, then the
// activity should be in two-pane mode.
mTwoPane = true;
// In two-pane mode, list items should be given the
// 'activated' state when touched.
((CommandeListFragment) getSupportFragmentManager()
.findFragmentById(R.id.commande_list))
.setActivateOnItemClick(true);
}
// TODO: If exposing deep links into your app, handle intents here.
}
請檢查這一個http://stackoverflow.com/questions/14376470/scheduling-recurring-task-in-android –
我不夠精確。我想知道的不是如何每10秒調用一次方法或函數,而是調用哪一個方法或函數,以及如何刷新我的項目列表。 – Dhost