2013-01-11 30 views
3

這是我的項目添加到ListView:如何清除ArrayAdapter定製的ListView

public class ServersAdapter extends ArrayAdapter<String> { 

    public ServersAdapter(Context context, int resource, int textViewResourceId, String[] servers) { 
     super(context, resource, textViewResourceId, servers); 
     Log.d("noc", "ServersAdapterin: "); 
     // TODO Auto-generated constructor stub 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     Log.d("noc", "getView: "); 

     LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View row = inflater.inflate(R.layout.servers_list, parent, false); 

     Log.d("noc", "inflate: "); 
     //datasource = new ServersDataSource(getContext()); 
     datasource.open(); 
     //servir list 
     Log.d("noc", "ServersDataSource: "); 
     Server[] servers = datasource.getAllServers(); 
     ImageView iv = (ImageView) row.findViewById(R.id.imageView1); 
     TextView tv = (TextView) row.findViewById(R.id.textView1); 

     Log.d("noc", "setText: "); 
     if (servers.length > 0) { 
      tv.setText(servers[position].getTitle()); 
      Log.d("noc", "getTitle: "); 

      Log.d("noc", "servers[position].getTitle()-==================: "+ servers[position].getTitle()); 
      if (servers[position].getEnabled() < 1) { 
       iv.setImageResource(R.drawable.ic_server_status_red); 
      } else { 
       iv.setImageResource(R.drawable.ic_server_status_green); 
      } 
     } 
     return row; 

    } 
} 

這是的onCreate:

...... 
try { 

     Log.d("noc", "need to delete this: "); 
     datasource = new ServersDataSource(this); 
     datasource.open(); 
     //servir list 
     Log.d("noc", "servir list: "); 
     //String[] servers = new String[10]; 
     //servir list 
     Log.d("noc", "ServersDataSource: "); 
     String[] servers = new String[10]; 


     Log.d("noc", "end need to delete this"); 
     ArrayAdapter<Server> adapter = new ServersAdapter(this, android.R.layout.simple_list_item_1, R.id.textView1, servers); 
     setListAdapter(adapter); 
    } catch (Exception e) { 
     Log.d("noc", "error (ArrayAdapter): " + e.toString()); 
    } 

我無法從全部清除的ListView其內容(我試圖清空它)。我一直在努力這樣做,像這樣:

ArrayAdapter<Server> adapter = (ArrayAdapter<Server>) getListAdapter(); 
Server server = null; 
switch (view.getId()) { 
    case R.id.add: 
     // Save the new server to the database 
     // server_id, "title", "url", "ip", enabled, "services", fetch_interval 
     server = datasource.createServer(1, "title of the server", "url of the server", "ip of the server", 1, "services of the server", 1); 
     adapter.add(server); 
     break; 
    case R.id.delete: 
     Log.d("noc", "delete "); 
     if (getListAdapter().getCount() > 0) { 
      datasource.emptyServersTable(); 
      Log.d("noc", "clear "); 
      adapter.clear(); 
      Log.d("noc", "after clear "); 

     } 
     break; 
    } 
    Log.d("noc", "notifyDataSetChanged"); 
    adapter.notifyDataSetChanged(); 

,這是輸出時,點擊「刪除」:

01-11 23:41:54.210: D/noc(14898): delete 
01-11 23:41:54.210: D/noc(14898): emptyServersTable: 
01-11 23:41:54.230: D/noc(14898): clear 
01-11 23:41:54.230: D/AndroidRuntime(14898): Shutting down VM 
01-11 23:41:54.230: W/dalvikvm(14898): threadid=1: thread exiting with uncaught exception (group=0x4140e2a0) 
01-11 23:41:54.230: E/AndroidRuntime(14898): FATAL EXCEPTION: main 
01-11 23:41:54.230: E/AndroidRuntime(14898): java.lang.IllegalStateException: Could not execute method of the activity 
01-11 23:41:54.230: E/AndroidRuntime(14898): at android.view.View$1.onClick(View.java:3699) 
01-11 23:41:54.230: E/AndroidRuntime(14898): at android.view.View.performClick(View.java:4223) 
01-11 23:41:54.230: E/AndroidRuntime(14898): at android.view.View$PerformClick.run(View.java:17275) 
01-11 23:41:54.230: E/AndroidRuntime(14898): at android.os.Handler.handleCallback(Handler.java:615) 
01-11 23:41:54.230: E/AndroidRuntime(14898): at android.os.Handler.dispatchMessage(Handler.java:92) 
01-11 23:41:54.230: E/AndroidRuntime(14898): at android.os.Looper.loop(Looper.java:137) 
01-11 23:41:54.230: E/AndroidRuntime(14898): at android.app.ActivityThread.main(ActivityThread.java:4898) 
01-11 23:41:54.230: E/AndroidRuntime(14898): at java.lang.reflect.Method.invokeNative(Native Method) 
01-11 23:41:54.230: E/AndroidRuntime(14898): at java.lang.reflect.Method.invoke(Method.java:511) 
01-11 23:41:54.230: E/AndroidRuntime(14898): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1008) 
01-11 23:41:54.230: E/AndroidRuntime(14898): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:775) 
01-11 23:41:54.230: E/AndroidRuntime(14898): at dalvik.system.NativeStart.main(Native Method) 
01-11 23:41:54.230: E/AndroidRuntime(14898): Caused by: java.lang.reflect.InvocationTargetException 
01-11 23:41:54.230: E/AndroidRuntime(14898): at java.lang.reflect.Method.invokeNative(Native Method) 
01-11 23:41:54.230: E/AndroidRuntime(14898): at java.lang.reflect.Method.invoke(Method.java:511) 
01-11 23:41:54.230: E/AndroidRuntime(14898): at android.view.View$1.onClick(View.java:3694) 
01-11 23:41:54.230: E/AndroidRuntime(14898): ... 11 more 
01-11 23:41:54.230: E/AndroidRuntime(14898): Caused by: java.lang.UnsupportedOperationException 
01-11 23:41:54.230: E/AndroidRuntime(14898): at java.util.AbstractList.remove(AbstractList.java:638) 
01-11 23:41:54.230: E/AndroidRuntime(14898): at java.util.AbstractList$SimpleListIterator.remove(AbstractList.java:75) 
01-11 23:41:54.230: E/AndroidRuntime(14898): at java.util.AbstractList.removeRange(AbstractList.java:658) 
01-11 23:41:54.230: E/AndroidRuntime(14898): at java.util.AbstractList.clear(AbstractList.java:466) 
01-11 23:41:54.230: E/AndroidRuntime(14898): at android.widget.ArrayAdapter.clear(ArrayAdapter.java:258) 
01-11 23:41:54.230: E/AndroidRuntime(14898): at com.wr.noc.ServersStatus.onClick(ServersStatus.java:91) 
01-11 23:41:54.230: E/AndroidRuntime(14898): ... 14 more 

所以有什麼問題。有沒有另外一種方法呢?

+0

你是冒充的陣列到適配器或'ArrayList'? –

+0

發佈如何創建ArrayAdapter,它是否使用原始數組? – Sam

+0

你是否嘗試調用'setListAdapter(null);'而不是'clear' – petey

回答

6

你應該儘量覆蓋在ServersAdapter類的clear()方法和呼叫添加到您的ArrayListclear()方法,如果你有一個。如果您的數據從數組中加載到視圖中,只需將其清空即可。

看你的代碼,我想關注一個是服務器

+0

我正在使用ArrayAdapter而不是ArrayList – Waqleh

+1

是的,但是'ArrayAdapter'不應該保存數據,通常'Adapter'包含一個'ArrayList'或者一個普通數組,然後把每個對象的數據放到數組到使用int **位置在getView()中創建的視圖**來獲取數組/ ArrayList中此索引處的數據。 – Flawyte

+0

目前它不起作用,因爲在您的構造函數中,您傳遞了一個String []數組,它不是保存要加載到視圖中的數據的數組。只需傳遞在'getView()'方法中使用的數組(稱爲** servers **),然後使用索引獲取數組中此位置的數據。 – Flawyte

5

我猜你傳遞一個List或者數組適配器。如果你把這個添加的項目的情況下,你可以做:

adapter.clear(); 
listview.getAdapter().notifyDataSetChanged(); 

並非所有的適配器有一個clear()方法。 ArrayAdapter的做法,但ListAdapterSimpleAdapter

+0

我可以做你推薦的東西嗎?我用更多的編碼編輯了我的問題。 – Waqleh

+0

我不認爲.notifyDataSetChanged()是一個有效的方法調用在這裏。它在這個例子中似乎不起作用。 –

0

該構造(具有陣列)不Arrays.asList(objects)其產生的不可變列表的實現。嘗試傳遞一個明確的ArrayList與字符串給這個構造:

public ArrayAdapter(Context context, int resource, 
         int textViewResourceId, List<T> objects) 
0

按我所知,沒有必要清除適配器以及notifyDataSetChanged()的適配器。 你已經使用List,ArrayList對於該項目或數組1清除它並重新初始化它,並將數據傳遞給服裝適配器清除使用的數組/列表。它將工作正常!

0

試試這個,它從視圖中刪除所有項目

final int adapterCount = adapter.getCount(); 

    for (int i = 0; i < adapterCount; i++) { 
     View item = adapter.getView(i, null, null); 
     myLayout.removeView(item); 
    }