我正在使用以下代码作为Android中ListView的Cusotm数组适配器

在视图中新建MyAsyncTask(url,callback);将一次又一次地运行,我该怎么做,这样对服务器的查询将只执行一次。

@Override
        public View getView(int position, View convertView, ViewGroup parent)
        {
            final Question question = quesions.get(position);
            final OpenionUser myUser = question.getUser();

            View view = convertView;
            ViewHolder viewHolder = new ViewHolder();

            if (convertView == null)
            {
                view = inflator.inflate(R.layout.question_adapter_layout, parent, false);

                viewHolder.firstPhotoPercent  = (TextView) view.findViewById(R.id.firstPhotoProgressText);
                viewHolder.secondPhotoPercent = (TextView) view.findViewById(R.id.secondPhotoProgressText);

                viewHolder.comments = (LinearLayout) view.findViewById(R.id.commentsListView);

                viewHolder.profilePic = (ImageButton) view.findViewById(R.id.profile);
                viewHolder.leftPic = (ImageButton) view.findViewById(R.id.photo1);
                viewHolder.rightPic = (ImageButton) view.findViewById(R.id.photo2);

                viewHolder.firstPhotoBg = (RelativeLayout) view.findViewById(R.id.firstPhotoProgress);
                viewHolder.secondPhotoBg = (RelativeLayout) view.findViewById(R.id.secondPhotoProgress);

                view.setTag(viewHolder);
            }
            else
                viewHolder = (ViewHolder) view.getTag();

    //      //imageLoader.displayImage(myUser.getProfilePicture(), viewHolder.profilePic, options);
    //
            viewHolder.username.setText(myUser.getUserName());
            viewHolder.question.setText(question.getQuestion());

            imageLoader.displayImage(question.getRightThumbnailLink(), viewHolder.rightPic, options);
            imageLoader.displayImage(question.getLeftThumbnailLink(), viewHolder.leftPic, options);

            String url = String.format(Constants.GET_QUESTION_COMMENTS,
                        question.getId(),
                        0,
                        2);

            ResponseCallback callback = new ResponseCallback()
            {
                @Override
                public void onSuccess(HttpResponse response)
                {
                    try {
                            JSONObject obj = new JSONObject(Utilities.convertStreamToString(response.getEntity().getContent()));
                            JSONArray array = obj.getJSONArray("comments");
                            ArrayList<Comment> comments = new ArrayList<Comment>();
                            for (int i = 0; i < array.length(); i ++)
                            {
                                Comment comment = Utilities.getCommentFromJSON(array.getJSONObject(i));
                                comments.add(comment);
                            }
                            addFeaturedViews(comments, viewHolder.comments);

                    } catch (IllegalStateException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }




                }


                @Override
                public void onFailure(HttpResponse exception)
                {

                }
            };

            new MyAsyncTask(url, callback);

最佳答案

您可以尝试以下方法:

if(position==0)
{
    new MyAsyncTask(url, callback);
}


*在下面的评论中看到Dhanesh Budhrani提供的信息后修改答案*

如果要运行新的MyAsyncTask(url,callback);只有一次,您可以尝试以下操作:

 if(firsttime)
 {
     firsttime = false;
     new MyAsyncTask(url, callback);
 }


这里firsttime是在适配器的构造函数中初始化为true的布尔变量。

07-24 09:49
查看更多