0

AsynctaskLoader 被多次调用,我在 listfragment 上得到 3 个项目(2 个重复项)。(我不想清除,因为我想实现一个无限滚动)。如果我在调用 onLoadFinished 时删除项目(adapter.remove),我仍然会得到两个项目(一个重复项)。如果我调用 adapter.clear onLoadFinished 我会得到 1 个项目,但无法实现无限滚动。无限滚动器是通过调用具有新想法和新捆绑包的加载器来实现的。

        public class ArticlePreviewAdapter extends ArrayAdapter<ArticlePreview>  {

        private final LayoutInflater inflater;
        private final ImageUpdater imageUpdater;



        public ArticlePreviewAdapter(Context context, ImageUpdater imageUpdater) {
            super(context, android.R.layout.simple_list_item_1);
            android.util.Log.d("SCROLL", "ArticlePreviewAdapter.ArticlePreviewAdapter" );

            this.inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);     
            this.imageUpdater = imageUpdater;
        }


        public void setData(ArticlePreview data) {
            //clear();      
            if (data != null) {         
                add(data);
                //this.add(data);           
            }
        }   

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            View view;

            if (convertView == null) {
                view = inflater.inflate(R.layout.article_preview, parent, false);
            } 
            else {
                view = convertView;
            }

            ArticlePreview articlePreview = getItem(position);      

            android.util.Log.d("SCROLL", "ArticlePreviewAdapter.getView pos: " + position);

            ((TextView) view.findViewById(R.id.article_preview_title)).setText(articlePreview.getTitle());
            ((TextView) view.findViewById(R.id.article_preview_info)).setText(articlePreview.getInfo());

            ImageView imageView = ((ImageView) view.findViewById(R.id.article_preview_image));
            Bitmap bitmap = this.imageUpdater.accessDirectly(articlePreview.getImageLink());

            if (bitmap != null) {
                imageView.setImageBitmap(bitmap);
            }
            else {
                this.imageUpdater.add(new ImageViewWebUpdate(view.hashCode(),
                        imageView, 100, 75, articlePreview.getImageLink()));
            }       

            return view;
        }
        }

加载器类

public class ArticlePreviewLoader extends AsyncTaskLoader<List<ArticlePreview>>
{

    private String type;
    private String nid;
    private String start;
    private String end;

    private List<ArticlePreview> data;

    public ArticlePreviewLoader(Context context, Bundle args)
    {
        super(context);

        this.type = (String) args.get("type");
        this.nid = (String) args.get("nid");
        this.start = (String) args.get("start");
        this.end = (String) args.get("end");
    }

    /**
     * load JSON data, parse it and return.
     */
    @Override
    public List<ArticlePreview> loadInBackground()
    {

        android.util.Log.d("LOAD", "ArticlePreviewLoader.loadInBackground");

        // even if fail return empty list and print exception stack trace
        LinkedList<ArticlePreview> list = new LinkedList<ArticlePreview>();
        URL url;
        HttpURLConnection httpURLConnection = null;

        --

        android.util.Log.d("LOAD", "ArticlePreviewLoader.loadInBackground return");

        return Collections.unmodifiableList(list);
        //return list;
    }

    @Override
    protected void onStartLoading()
    {

        if (data != null)
            deliverResult(data);

        if (takeContentChanged() || data == null)
            forceLoad();
    }

    @Override
    protected void onStopLoading()
    {

        //super.onStopLoading();
        cancelLoad();
    }

    @Override
    protected void onReset()
    {
        super.onReset();

        onStopLoading();
        data = null;
    }
}

帮助将不胜感激..我已经坚持了一天。

编辑

清单代码:

    <uses-sdk
        android:minSdkVersion="13"
        android:targetSdkVersion="16" />

    <uses-permission android:name="android.permission.INTERNET" />

     <!-- Permission to write to external storage -->
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".SplashActivity"
            android:label="@string/title_activity_splash" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".LoginActivity"
            android:label="@string/title_activity_login" >
        </activity>
        <activity
            android:name=".ArticleForumActivity"
            android:label="@string/title_activity_article_forum" >
        </activity>
    </application>

</manifest>
4

2 回答 2

1

Set改为使用List<ArticlePreview> data

于 2012-11-27T07:11:26.903 回答
0

我已经完成了@Yahor10 所说的。加载器中的数据验证并在类 listfragment 的 onActivityCreated 方法中调用了 adapter.clad

于 2012-11-27T07:06:07.257 回答