ListView doesn't visually update its content after notifyDataSetChanged

旧城冷巷雨未停 提交于 2019-12-10 18:38:20

问题


I have a static array of items which is set to a listview, that I might modify from another Activity (add some more items to it). So when I turn back to first activity, I have to refresh the content of the listview with the new items.

I do that by: adapter.notifyDataSetChanged() in the onStart() method, verifying of course if I have to do so.

Problem is, I only see my listview changed only if I scroll the list a bit.

What could the problem be?

LE: Solution was to set listview.invalidateViews() before changing Activity and as I return to first activity, notifying the adapter ends the purpose of it.

LE @vorrtex:

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

    if (convertView == null) {
        convertView = inflater.inflate(row, parent, false);
    }


    TextView tvAuthor = (TextView) ViewHolder.get(convertView, R.id.authorText);
    TextView tvDate = (TextView) ViewHolder.get(convertView,R.id.dateText);
    TextView tvCategory = (TextView) ViewHolder.get(convertView,R.id.categoryText);
    TextView tvTitle = (TextView) ViewHolder.get(convertView,R.id.titleText);
    TextView tvContent = (TextView) ViewHolder.get(convertView,R.id.contentText);
    TextView tvNoComment = (TextView) ViewHolder.get(convertView,R.id.noComments);
   WebView sv_banner = (WebView) ViewHolder.get(convertView,R.id.wv_imageBanner);

    objBean = items.get(position);


    sv_banner.getSettings().setJavaScriptEnabled(true);
    sv_banner.getSettings()
            .setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
    sv_banner.getSettings().setLightTouchEnabled(true);
    sv_banner.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
    sv_banner.getSettings().setPluginState(WebSettings.PluginState.ON);


    if ((position + 1) % 3 == 0) {
        if (sv_banner != null && null != objBean.getBanner_img_url() && objBean.getBanner_img_url().trim()
                .length() > 0) {
            sv_banner.setVisibility(View.VISIBLE);
            sv_banner.loadUrl(objBean.getBanner_img_url().toString());
            sv_banner.setFocusable(false);
        }
    } else {
        sv_banner.setVisibility(View.GONE);
    }


    sv_banner.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_UP) {
                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(objBean.getBanner_url().toString()));
                activity.startActivity(intent);
                return true;
            }
            return false;
        }
    });

    if (tvAuthor != null && null != objBean.getAuthor()
            && objBean.getAuthor().trim().length() > 0) {
        tvAuthor.setText(Html.fromHtml(objBean.getAuthor()));
    }

    if (tvNoComment != null && null != objBean.getNoComments()
            && objBean.getNoComments().trim().length() > 0) {
        int nrComent = Integer.valueOf(objBean.getNoComments());

        if (nrComent == 0) {
            tvNoComment.setText("");
        } else {
            tvNoComment.setText(Html.fromHtml(objBean.getNoComments()) + "  comments");
        }
    }


    if (tvDate != null && 0 != objBean.getPost_date()
            && objBean.getPost_date() > 0) {
        tvDate.setText(Html.fromHtml(String.valueOf(dateFormat.format(objBean.getPost_date()))));
    }

    if (tvCategory != null && null != objBean.getCategory()
            && objBean.getCategory().trim().length() > 0) {
        tvCategory.setText(Html.fromHtml(objBean.getCategory()).toString());
    }

    if (tvTitle != null && null != objBean.getTitle()
            && objBean.getTitle().trim().length() > 0) {
        tvTitle.setText(Html.fromHtml(objBean.getTitle()).toString());
    }

    if (tvContent != null && null != objBean.getContent()
            && objBean.getContent().trim().length() > 0) {
        String miniText = html2text(objBean.getContent());
        Log.i("Content", "MiniText = " + miniText);
        if (miniText.length() < 150) {
            String finalMiniText = miniText.substring(0, miniText.length());
            tvContent.setText(finalMiniText + "...");
        } else {
            String finalMiniText = miniText.substring(0, 150);
            tvContent.setText(finalMiniText + "...");
        }
    }


    if (position == 0) {

        RelativeLayout child = (RelativeLayout) convertView.findViewById(R.id.layoutNewsForex);
        child.setBackgroundResource(R.drawable.list_shape_corners);
        RelativeLayout.LayoutParams head_params = (RelativeLayout.LayoutParams) child.getLayoutParams();
        head_params.setMargins(18, 18, 18, 0); //substitute parameters for left, top, right, bottom
        child.setLayoutParams(head_params);

    } else {
        RelativeLayout child = (RelativeLayout) convertView.findViewById(R.id.layoutNewsForex);
        child.setBackgroundColor(Color.parseColor("#ebebeb"));
        RelativeLayout.LayoutParams head_params = (RelativeLayout.LayoutParams) child.getLayoutParams();
        head_params.setMargins(18, 0, 18, 0); //substitute parameters for left, top, right, bottom
        child.setLayoutParams(head_params);
    }


    return convertView;

}

PS. This adapter is extending ArrayAdapter.


回答1:


Solution was to set listview.invalidateViews() before changing Activity and as I return to first activity, notifying the adapter ends the purpose of it.



来源:https://stackoverflow.com/questions/16436542/listview-doesnt-visually-update-its-content-after-notifydatasetchanged

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!