2013-11-27 11 views
0

我有一个listview,我想每4周刷新一次。我应该如何完成这项任务?我可以使用计时器还是应该使用计数器?有没有人尝试过这一点。谢谢你的帮助。下面是我的代码的ListView如何每4周刷新一次listview(或可配置的时间)

LogListView.java

public class LogListView extends ListActivity { 
    /** Called when the activity is first created. */ 
    static String newString; 
    private static EntryAdapterLog adapter; 
    int clickCounter = 0; 
    static ArrayList<Item> items = new ArrayList<Item>(); 
    static SharedPreferences preferences = null; 
    private static Context context = null; 
    static StringTokenizer tokens; 
    static String first; 
    private static String second; 
    private JSONArray jsonarry = null; 
    static String saveitems; 
    private JSONObject jsonobject = null; 
    private String subtitle; 
    static String title; 



    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     context = this; 
     adapter = new EntryAdapterLog(this, items); 
     // items.add(new SectionItem("Log Report")); 
     setListAdapter(adapter); 

     if (adapter.getCount() != 0) { 
      // Do nothing Adapter has value 
     } else { 
      retreiveItems(); 
     } 

    } 

    // Method which will handle dynamic insertion 
    public static void addItems() { 

     preferences = context.getSharedPreferences("LOG",android.content.Context.MODE_PRIVATE); 
     newString = preferences.getString("log", ""); 

     tokens = new StringTokenizer(newString, ","); 
     first = tokens.nextToken(); 
     second = tokens.nextToken(); 

     items.add(new EntryItem(first, second)); 

     adapter.notifyDataSetChanged(); 

    } 
    // Method which will handle dynamic insertion ends 

    @Override 
    protected void onDestroy() { 
     super.onDestroy(); 
     saveItems(); 
    } 

    // Save ListItems if restarted 
    protected static void saveItems() { 
     SharedPreferences prefs = context.getSharedPreferences("prefName",Context.MODE_PRIVATE); 
     Editor editor = prefs.edit(); 
     editor.putString("myList", new Gson().toJson(items).toString()); 
     editor.apply(); 
     Log.d("LOG", "Saved Items : " + items); 
    } 
    // Save ListItems if restarted ends 

    // Retrieve ListItems if restarted 
    protected void retreiveItems() { 
     preferences = context.getSharedPreferences("prefName",android.content.Context.MODE_PRIVATE); 
     saveitems = preferences.getString("myList", ""); 
     Log.d("LOG", "Retreived Items : " + saveitems); 

     try { 
      jsonarry = new JSONArray(saveitems); 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 

     if (jsonarry == null || jsonarry.length() == 0) { 
      return;                  //This checks before setting adapter onCreate if adapter is null 
     } 

     for (int i = 0; i < jsonarry.length(); i++) { 

      try { 
       jsonobject = jsonarry.getJSONObject(i); 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 

      // get all values here from JSONObject 
      title = jsonobject.optString("title"); 
      subtitle = jsonobject.optString("subtitle"); 

      items.add(new EntryItem(title, subtitle)); 
      adapter.notifyDataSetChanged(); 

     } 

    } 
    // Retrieve ListItems if restarted ends 

    // Counter for amount of period of time before flusing adapter 
    protected void flushList(){ 

    } 
    // Counter for amount of period of time before flusing adapter ends 

    @Override 
    protected void onListItemClick(ListView l, View v, int position, long id) { 

     if (!items.get(position).isSection()) { 
      items.get(position); 
      Toast.makeText(this, "You clicked " + position, Toast.LENGTH_SHORT).show(); 

     } 

     if (position == 9) { 

     } 

     super.onListItemClick(l, v, position, id); 
    } 

} 

问候

+0

是否要从首选项中刷新项目?你使用Alarm manager来管理一个任务。 –

回答

1

我会考虑的AlarmManagersetRepeating()
试图运行计时器或计数器4周是浪费大量资源。

相关问题