11

我使用ArrayAdapter將項目添加到自定義ListView並在Android應用中顯示結果。我遇到的問題是,ArrayAdapter似乎等到所有項目都在它顯示視圖之前。也就是說,當我將這些項目添加到ArrayAdapter並調用notifyDataSetChanged時,它不會更新ListView以顯示添加的項目。它將等待,直到添加所有項目並在顯示項目之前調用GetView。Android - 使用ArrayAdapter一次添加並向ListView顯示項目

我想要做的是在將它添加到ListView後立即顯示該項目。這可能嗎?

我相信相關的代碼如下:

r_adapter = new ReminderAdapater(Activity_ContentSearch.this, R.layout.search_listitem, myList); 
listView.setAdapter(r_adapter); 
... 
r_adapter.notifyDataSetChanged(); 
r_adapter.clear(); 
for(int i = 0; i < myList.size(); i++) 
{ 
    r_adapter.add(myList.get(i)); 
    r_adapter.notifyDataSetChanged(); 
} 

正如你所看到的,即使我add方法調用後notifyDataSetChanged,它實際上並沒有更新的觀點。在完成上面的循環後,視圖最終被更新(基於我所知道的,這是因爲GetView在代碼的這部分完成之後才被調用)。

我試圖重寫我的自定義ArrayAdapter的add方法沒有運氣,因爲我沒有訪問該方法中的視圖。

任何幫助將受到歡迎:)

巴拉

回答

23

Android的用戶界面是單線程的。每次向適配器添加條目時,您都不會從主應用程序線程向Android回覆控制權。因此,在您返回控制權之前,Android沒有機會顯示您的條目,直到您將整個適配器填充爲止,您纔會這樣做。

Here is an example顯示使用AsyncTask通過後臺線程逐步填充ArrayAdapter

/*** 
    Copyright (c) 2008-2012 CommonsWare, LLC 
    Licensed under the Apache License, Version 2.0 (the "License"); you may not 
    use this file except in compliance with the License. You may obtain a copy 
    of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required 
    by applicable law or agreed to in writing, software distributed under the 
    License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS 
    OF ANY KIND, either express or implied. See the License for the specific 
    language governing permissions and limitations under the License. 

    From _The Busy Coder's Guide to Android Development_ 
    http://commonsware.com/Android 
*/ 

package com.commonsware.android.async; 

import android.app.ListActivity; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.os.SystemClock; 
import android.widget.ArrayAdapter; 
import android.widget.Toast; 
import java.util.ArrayList; 

public class AsyncDemo extends ListActivity { 
    private static final String[] items={"lorem", "ipsum", "dolor", 
             "sit", "amet", "consectetuer", 
             "adipiscing", "elit", "morbi", 
             "vel", "ligula", "vitae", 
             "arcu", "aliquet", "mollis", 
             "etiam", "vel", "erat", 
             "placerat", "ante", 
             "porttitor", "sodales", 
             "pellentesque", "augue", 
             "purus"}; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

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

    new AddStringTask().execute(); 
    } 

    class AddStringTask extends AsyncTask<Void, String, Void> { 
    @Override 
    protected Void doInBackground(Void... unused) { 
     for (String item : items) { 
     publishProgress(item); 
     SystemClock.sleep(200); 
     } 

     return(null); 
    } 

    @SuppressWarnings("unchecked") 
    @Override 
    protected void onProgressUpdate(String... item) { 
     ((ArrayAdapter<String>)getListAdapter()).add(item[0]); 
    } 

    @Override 
    protected void onPostExecute(Void unused) { 
     Toast 
     .makeText(AsyncDemo.this, "Done!", Toast.LENGTH_SHORT) 
     .show(); 
    } 
    } 
} 
相關問題