更新MergeAdapter / SectionedAdapter

时间:2012-03-15 12:44:42

标签: android android-arrayadapter commonsware-cwac

我正在尝试创建一个 LazyList EndlessList,但是使用了一个分段适配器......这些部分将是将从Internet下载的项目页面。

我尝试使用MergeAdapter或不推荐使用的SectionedAdapter(Mark Murphy)来实现它的基本要点。但我面临着两个问题。

1)使用SectionedAdapter,它加载第一页正常,但在第二页上我遇到了这个例外。

Uncaught handler: thread main exiting due to uncaught exception
java.lang.ArrayIndexOutOfBoundsException
at android.widget.AbsListView$RecycleBin.addScrapView(AbsListView.java:3572)
at android.widget.AbsListView.trackMotionScroll(AbsListView.java:2487)
at android.widget.AbsListView$FlingRunnable.run(AbsListView.java:2353)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4363)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
at dalvik.system.NativeStart.main(Native Method)
经过几个小时的研究,我意识到这与错误的getViewTypeCount()值有关。但我不知道如何正确实现它。

2)然后我尝试使用MergeAdapter,但这只会使情况变得更糟,甚至是各个部分也会随机出现。

我写了一个小示例程序只是为了显示我的实现。

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

import android.app.ListActivity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class LazySectionListActivity extends ListActivity implements
        OnScrollListener {

    private int mPrevItemCount;
    private int nextPrimes = 0;
    private final int MAX = 100;
    private final SectionAdapter adapter = new SectionAdapter();

    // private final MergeAdapter adapter = new MergeAdapter();

    private class SectionAdapter extends SectionedAdapter {

        @Override
        protected View getHeaderView(String caption, int index,
                                     View convertView, ViewGroup parent) {
            TextView tv = new TextView(LazySectionListActivity.this);
            tv.setText(caption);
            return tv;
        }

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
        setProgressBarIndeterminate(true);
        setContentView(R.layout.main);
        getListView().setOnScrollListener(this);
        setListAdapter(adapter);
        new NextThousandPrimes(nextPrimes).execute();
        nextPrimes += MAX;
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem,
                         int visibleItemCount, int totalItemCount) {

        if(firstVisibleItem + visibleItemCount >= totalItemCount
           && mPrevItemCount != totalItemCount) {
            new NextThousandPrimes(nextPrimes).execute();
            nextPrimes += MAX;
            mPrevItemCount = totalItemCount;
        }

    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {

    }

    private class NextThousandPrimes extends AsyncTask<Void, Void, Void> {
        private final int start;
        private final List<String> primes = new ArrayList<String>();

        public NextThousandPrimes(int nextPrimes) {
            this.start = nextPrimes;
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            setProgressBarIndeterminateVisibility(true);
        }

        @Override
        protected Void doInBackground(Void... params) {
            for(int s = start; s < start + MAX; s++)
                if(BigInteger.valueOf(s).isProbablePrime(1)) {
                    primes.add(String.valueOf(s));
                }
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            setProgressBarIndeterminateVisibility(false);
            //TextView view = new TextView(LazySectionListActivity.this);//for merge adapter, use the commented code
            //view.setText(String.format("%d-%d", start, start + MAX));
            // adapter.addView(view);
            // adapter.addAdapter(new
            // ArrayAdapter<String>(LazySectionListActivity.this,
            // android.R.layout.simple_list_item_1,
            // primes));
            adapter.addSection(String.format("%d-%d", start, start + MAX),
                               new ArrayAdapter<String>(LazySectionListActivity.this,
                                                        android.R.layout.simple_list_item_1,
                                                        primes));
            adapter.notifyDataSetChanged();

        }
    }
}

那我错过了什么?任何帮助表示赞赏。提前谢谢。

1 个答案:

答案 0 :(得分:5)

MergeAdapter文档中所述:

  

在调用setListAdapter()以将MergeAdapter与ListView相关联之前,必须将内容倒入MergeAdapter。此限制是必需的,因为Android只调用一次getViewTypeCount(),添加更多视图或适配器会添加更多视图类型。

因此,MergeAdapter无法在无尽的环境中工作,因为我们不知道有多少种可能的视图类型。

欢迎您尝试黑客攻击MergeAdapter的副本,您可以将getViewTypeCount()连接起来,以确保确定比您的需求更大,并查看是否这样可行。俗话说,你的里程可能会有所不同......: - )