滚动列表视图后,应用程序重新启动两次

时间:2017-07-12 08:48:52

标签: android listview

我在我的应用程序中使用listview,因为如果我将滚动listview更多,那么两次应用程序正在重新启动。如何修复它。

这是我的适配器代码:

public class ExpertQAListAdapter extends BaseAdapter {

    static class ViewHolder {

        public TextView mQuestionTextView;
        public TextView mUserNameTextView;
        public TextView mQuestionDateTime;
        public TextView mAnswerTextView;
        public TextView mExpertNameTextView;
        public TextView mAnswerDateTime;
        public ImageView mExpertImageView;
        public ImageView mQuestionaireImageView;
        public ImageView mHelpfulImageView;
        public ImageView mNonHelpfulImageView;
        public TextView mHelpfulTextView;
        public RelativeLayout mRelativelayoutForImageDateAndTime;
        public String mImageUrl;

    }

    private LayoutInflater mInflater;
    private IWDObjectList mQAItemList;
    private ExpertQAListController mExpertQAListController;
    private Context mContext;

    public ExpertQAListAdapter(Context context, LayoutInflater inflater,
            ExpertQAListController expertQAListController) {
        mInflater = inflater;
        mExpertQAListController = expertQAListController;
        mContext = context;
    }

    public void updateDataSet(IWDObjectList itemList) {
        mQAItemList = itemList;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mQAItemList != null ? mQAItemList.Count() : 0;
    }

    @Override
    public AskAnExpertDTO getItem(int position) {
        return (AskAnExpertDTO) mQAItemList.GetItem(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

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

        ViewHolder viewHolder = null;

        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.askanexpert_listitem_view_v25, null);

            View userBoxView = convertView.findViewById(R.id.askanexpert_relativelayout_for_userbox);
            viewHolder.mQuestionTextView = (TextView) userBoxView.findViewById(
                    R.id.askanexpert_textview_for_content);
            viewHolder.mUserNameTextView = (TextView) userBoxView.findViewById(
                    R.id.askanexpert_textview_for_username);
            viewHolder.mQuestionDateTime = (TextView) userBoxView.findViewById(
                    R.id.askanexpert_textview_for_date);
            viewHolder.mQuestionaireImageView = (ImageView) userBoxView.findViewById(
                    R.id.askanexpert_imageview_for_userpic);

            View expertBoxView = convertView.findViewById(
                    R.id.askanexpert_relativelayout_for_expertbox);
            viewHolder.mRelativelayoutForImageDateAndTime=(RelativeLayout)expertBoxView.findViewById(R.id.askanexpert_relativelayout_for_image_date_and_time);

            viewHolder.mAnswerTextView = (TextView) expertBoxView.findViewById(
                    R.id.askanexpert_textview_for_content);
            // To make hyper links clickable
            viewHolder.mAnswerTextView.setMovementMethod(LinkMovementMethod.getInstance());
            viewHolder.mExpertNameTextView = (TextView) expertBoxView.findViewById(
                    R.id.askanexpert_textview_for_username);
            viewHolder.mAnswerDateTime = (TextView) expertBoxView.findViewById(
                    R.id.askanexpert_textview_for_date);
            viewHolder.mExpertImageView = (ImageView) expertBoxView.findViewById(
                    R.id.askanexpert_imageview_for_userpic);

            viewHolder.mHelpfulImageView = (ImageView) expertBoxView.findViewById(
                    R.id.askanexpert_imageview_for_helpful);
            viewHolder.mNonHelpfulImageView = (ImageView) expertBoxView.findViewById(
                    R.id.askanexpert_imageview_for_nothelpful);
            viewHolder.mHelpfulTextView = (TextView) expertBoxView.findViewById(R.id.askanexpert_textview_for_helpful);
            convertView.setTag(viewHolder);
        }
        else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.mHelpfulImageView.setSelected(false);
        viewHolder.mNonHelpfulImageView.setSelected(false);
        viewHolder.mHelpfulImageView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                mExpertQAListController.OnHelpfulIconClick(position);
            }
        });

        viewHolder.mNonHelpfulImageView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                mExpertQAListController.OnNonHelpfulIconClick(position);
            }
        });

        loadQuestionAndAnswer(viewHolder, getItem(position));

        if (position == mQAItemList.Count() - 3) {
            mExpertQAListController.OnListItemScrollEnd();
        }

        return convertView;
    }

    private void loadQuestionAndAnswer(final ViewHolder vh, final AskAnExpertDTO dto) {
        IDateTime answerDateTime = null;
        IMobilePlatformFactory mpf = MobilePlatformFactoryLocator.GetMobilePlatformFactory();
        IFormatDataType formatDataType = MobilePlatformFactoryLocator.GetMobilePlatformFactory()
                .CreateFormatDataType();

        IDateTime questionDateTime = mpf.CreateDateTime(dto.QuestionDateTimeInMillis);
        String questionTime = formatDataType.FormatDateTime(questionDateTime, 
                StringResource.DATE_MMM_DD_YYYY, null, 0);

        vh.mQuestionTextView.setText(dto.Question);
        vh.mUserNameTextView.setText(dto.QuestionerName);
        vh.mQuestionDateTime.setText(questionTime);

        if (dto.Status == QAStatusRequestDTO.STATUS_QA_USEFUL) {
            vh.mHelpfulImageView.setSelected(true);
        }

        if (dto.Status == QAStatusRequestDTO.STATUS_QA_UNUSEFUL) {
            vh.mNonHelpfulImageView.setSelected(true);
        }

        if (dto.Status == QAStatusRequestDTO.STATUS_QA_DEFAULT) {
            vh.mNonHelpfulImageView.setSelected(false);
            vh.mHelpfulImageView.setSelected(false);
        }

        String mobileSKey = DataAccessObjectFactoryLocator.getDataAccessObjectFactory()
                .getSecurityInformationLookUpDAO().GetMobileSKey();
        PatientLookUpDAO patientDao = DataAccessObjectFactoryLocator.getDataAccessObjectFactory()
                .getPatientLookUpDAO();
        PatientLookUpDTO patientDto = patientDao.Read(mobileSKey, true);
        if (dto.QuestionerId == patientDto.PatientId && patientDto.Base64EncodedProfilePicture!=null) {
            byte[] profilePicData = formatDataType.DecodeBase64EncodedString(patientDto.Base64EncodedProfilePicture);
            Bitmap bitmap = BitmapFactory.decodeByteArray(profilePicData, 0, profilePicData.length);
            vh.mQuestionaireImageView.setAdjustViewBounds(true);
            vh.mQuestionaireImageView.setImageBitmap(bitmap);
            vh.mQuestionaireImageView.setScaleType(ImageView.ScaleType.FIT_XY);
        }
        else {
            vh.mQuestionaireImageView.setImageDrawable(mContext.getResources().getDrawable(
                    R.drawable.ask_an_expert_profile_pic_male_icon_v25));
        }

        if (dto.AnswerDateTimeInMillis != 0) {
            answerDateTime = mpf.CreateDateTime(dto.AnswerDateTimeInMillis);
        }

        if (answerDateTime != null) {
            if (dto.ExpertId != 0) {
                String imageUrl = MobilePlatformFactoryLocator.GetMobilePlatformFactory().GetPortalURL() + "/Service/api/AskAnExpert/GetExpertImage/" + dto.ExpertId;
                ImageLoader.loadWithAuth(mContext, vh.mExpertImageView, imageUrl, null);
            }
            String answerTime = formatDataType.FormatDateTime(answerDateTime, StringResource.DATE_MMM_DD_YYYY, null, 0);
            vh.mExpertNameTextView.setText(dto.ExpertName);
            vh.mAnswerDateTime.setText(answerTime);
        }
        else {
            vh.mExpertNameTextView.setVisibility(View.GONE);
            vh.mAnswerDateTime.setVisibility(View.GONE);
            vh.mExpertImageView.setVisibility(View.GONE);
        }

        if (dto.Answer.equalsIgnoreCase(mContext.getResources().getString(R.string.ExpertQAList_yet_to_be_answered_LBL))){
            vh.mNonHelpfulImageView.setVisibility(View.GONE);
            vh.mHelpfulImageView.setVisibility(View.GONE);
            vh.mHelpfulTextView.setVisibility(View.GONE);
        }else {
            vh.mNonHelpfulImageView.setVisibility(View.VISIBLE);
            vh.mHelpfulImageView.setVisibility(View.VISIBLE);
            vh.mHelpfulTextView.setVisibility(View.VISIBLE);
        }
        vh.mAnswerTextView.setText(Html.fromHtml(dto.Answer));

        vh.mRelativelayoutForImageDateAndTime.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i=new Intent(mContext,ExpertDescriptionActivity.class);
                i.putExtra("ask an expert object",dto);
                mContext.startActivity(i);
            }
        });
    }
}

这是我的imageLoader类:

public final class ImageLoader {

    public static void load(Context context, ImageView view, String url) {
        load(context, view, url, null);
    }

    public static void load(Context context, ImageView view, String url, Callback callback) {
        if (TextUtils.isEmpty(url)) {
            return;
        }

        Picasso.with(context)
                .load(url)
                .fit()
                .skipMemoryCache()
                .into(view, callback);
    }

    public static void loadWithAuth(Context context, ImageView view, String url, Callback callback) {
        if (TextUtils.isEmpty(url)) {
            return;
        }

        OkHttpClient client = new OkHttpClient.Builder()
                .addInterceptor(new Interceptor() {
                    @Override
                    public Response intercept(Chain chain) throws IOException {
                         SecurityInformationLookUpDAO securityInformationLookUpDAO = CommonDataAccessObjectFactoryLocator
                                 .getCommonDataAccessObjectFactory().getSecurityInformationLookUpDAO();
                        Request newRequest = chain.request().newBuilder()
                                .addHeader(HttpHeaderDTO.CUSTOM_HEADER_ACCESS_TOKEN, securityInformationLookUpDAO.ReadAccessToken())
                                .addHeader(HttpHeaderDTO.CUSTOM_HEADER_CSRF_TOKEN, securityInformationLookUpDAO.ReadCSRFToken())
                                .build();
                        return chain.proceed(newRequest);
                    }
                })
                .build();

        Picasso picasso = new Picasso.Builder(context)
                .downloader(new OkHttp3Downloader(client))
                .build();

        picasso.load(url).into(view, callback);
    }
}

我出现了内存错误。请帮我解决。

0 个答案:

没有答案