I created a master/detail flow application, that is connecting to a url to get a JSON. This JSON is then parsed, to get a list of commands and all the details of it. I would like to refresh the item list without any action from the user. I know there is a simple way to create the list with the JSON data, but how can i update it every 10 seconds simply ?
EDIT :
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);
I have to use notifyDataSetChanged(), but i don't know whento call it. How do I do this ?
EDIT 2 : This is the 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));
}
And this is my 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.
}