2014-02-22 105 views
0

以下是我的代碼我想每5秒listview將會用新值刷新我做什麼請檢查我的代碼我只是按照這個簡單的教程http://www.mkyong.com/android/android-listview-example/現在我想更新每5第二,當我運行此代碼應用程序崩潰,可能會通知內部可運行告訴我爲什麼?如何刷新listview每5秒幫我

public class ListMobileActivity extends ListActivity { 

    Handler mHandler; 
    static int n = 4; 
    private static String[] MOBILE_OS = new String[n]; 

    MobileArrayAdapter setListAdapter ; 

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

     MOBILE_OS[0] = "Android1"; 
     MOBILE_OS[1] = "iOS1"; 
     MOBILE_OS[2] = "WindowsMobile1"; 
     MOBILE_OS[3] = "Blackberry1"; 

     this.mHandler = new Handler(); 

     this.mHandler.postDelayed(m_Runnable,5000); 
     setListAdapter(new MobileArrayAdapter(this, MOBILE_OS)); 
    } 

    private final Runnable m_Runnable = new Runnable() 
    { 
     public void run() 

     { 
      MOBILE_OS[0] = "hello1"; 
      MOBILE_OS[1] = "hello2"; 
      MOBILE_OS[2] = "hllo3"; 
      MOBILE_OS[3] = "hello4"; 
      Toast.makeText(ListMobileActivity.this, "in 
        runnable",Toast.LENGTH_SHORT).show(); 
        setListAdapter.notifyDataSetChanged(); 
      ListMobileActivity.this.mHandler.postDelayed(m_Runnable, 5000); 
     } 
    };//runnable 

} 

private final Context context; 
private final String[] values; 

    public MobileArrayAdapter(Context context, String[] values) { 
     super(context, R.layout.list_mobile, values); 
     this.context = context; 
     this.values = values; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     LayoutInflater inflater = (LayoutInflater) context 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View rowView = inflater.inflate(R.layout.list_mobile, parent, false); 
     TextView textView = (TextView) rowView.findViewById(R.id.label); 
     ImageView imageView = (ImageView) rowView.findViewById(R.id.logo); 
     textView.setText(values[position]); 

     // Change icon based on name 
     String s = values[position]; 

     System.out.println(s); 

     return rowView; 
    } 
} 
+0

嘗試在此行上使用Timer類 –

+0

問題setListAdapter.notifyDataSetChanged();如何更新可運行內部的列表視圖? – user3341546

+0

只是告訴我如何設置適配器裏面可運行? – user3341546

回答

0

使用Timer類,您可以更新您的ListView每5秒如下:

public class ListViewRefreshEvery5Seconds extends ListActivity 
{ 
    Timer timer; 
    ArrayList<String> arr1; 
    ArrayAdapter<String> adapter; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 

     arr1 = new ArrayList<String>(); 
     for(int i=1;i<=5;i++) 
     { 
      arr1.add("Item "+i); 
     } 

     adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arr1); 
     setListAdapter(adapter); 

     setTimerForAdvertise(); 
    } 

    void setTimerForAdvertise() { 
     timer = new Timer(); 
     TimerTask updateProfile = new CustomTimerTask(); 
     timer.scheduleAtFixedRate(updateProfile, 5000, 5000); 
    } 

    public class CustomTimerTask extends TimerTask 
    { 

     private Handler mHandler = new Handler(); 

     @Override 
     public void run() 
     { 
      new Thread(new Runnable() 
      { 
       @Override 
       public void run() 
       { 
        mHandler.post(new Runnable() 
        { 
         @Override 
         public void run() 
         { 

          for(int i=1;i<=5;i++) 
          { 
           arr1.add("Item "+i); 
          } 
          adapter.notifyDataSetChanged(); 
         } 
        }); 
       } 
      }).start(); 

     } 

    } 
} 

,或者如果您想更新的ListView只有一次,那麼你可以簡單地使用處理程序是這樣的:

new Handler().postDelayed(new Runnable() 
     { 

      @Override 
      public void run() 
      { 
       // TODO Auto-generated method stub 
       for(int i=1;i<=5;i++) 
       { 
        arr1.add("Item "+i); 
       } 
       adapter.notifyDataSetChanged(); 
      } 
     }, 5000);