ListActivity 中适配器中的 add() 函数显示不支持的操作



使用的代码。我希望列表在开始时加载一次,然后在后续滚动中加载数据。

public class ViewPage extends ListActivity{
ListView lv;
ListAdapter ld;
int size=7;
String filename[] = new String[size];
Cursor contactcursor;
ArrayAdapter<String> arr_ad;
int contact_column_index;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mylist);
    for(int i=0;i<size;i++)
        filename[i]=new String();

    String[] proj = { ContactsContract.Contacts.DISPLAY_NAME };
    contactcursor = managedQuery(ContactsContract.Contacts.CONTENT_URI, proj, null, null, null);
    contact_column_index = contactcursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME);
    lv = (ListView)findViewById(android.R.id.list);
    contactcursor.moveToFirst();
        for(int i=0;i<size;i++)
        {
            filename[i] = contactcursor.getString(contact_column_index);
            Toast.makeText(ViewPage.this, filename[i] +" ok "+ contactcursor.getString(contact_column_index) , 1).show();
            if(!contactcursor.moveToNext())
                break;
        }
        arr_ad = new ArrayAdapter<String>(ViewPage.this, android.R.layout.simple_list_item_1, filename);
        setListAdapter(arr_ad);
        lv.setOnScrollListener(new EndlessScrollListener());
}
public class EndlessScrollListener implements OnScrollListener {
private int visibleThreshold = size;
private int currentPage = 0;
private int previousTotal = 0;
private boolean loading = true;
public EndlessScrollListener() 
{
}
public EndlessScrollListener(int visibleThreshold) 
{
    this.visibleThreshold = visibleThreshold;
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
        int visibleItemCount, int totalItemCount) {
    // TODO Auto-generated method stub
    Toast.makeText(ViewPage.this, "In" , 1).show();
     if (loading) 
     {
         if (totalItemCount > previousTotal) 
         {
            loading = false;
            previousTotal = totalItemCount;
            currentPage++;
         }
     }
     if (!loading && (totalItemCount - visibleItemCount) <= (firstVisibleItem + visibleThreshold)) 
     { 

            for(int i=0;i<size;i++)
            {
                filename[i] = contactcursor.getString(contact_column_index);
                Toast.makeText(ViewPage.this, filename[i] +" "+ contactcursor.getString(contact_column_index) , 1).show();
                arr_ad.add(filename[i]);
                if(!contactcursor.moveToNext())
                    break;
            }
            arr_ad.notifyDataSetChanged();
            //arr_ad = new ArrayAdapter<String>(ViewPage.this, android.R.layout.simple_list_item_1, filename);

            //setListAdapter(arr_ad);
            loading = true;
            Toast.makeText(ViewPage.this,totalItemCount+" "+visibleItemCount+" "+firstVisibleItem+" "+visibleThreshold+" ", 1).show();
     }
}
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
    // TODO Auto-generated method stub
}
}
}

它在第 98 行显示错误,该错误为 arr_ad.add(文件名[i]);并表示不支持该操作。谁能帮忙!提前感谢。

如果要使用 ArrayAdapter 的 add 方法,则必须在构造函数中赋予 List 对象(不是 String[])。

最新更新