在notifyDataSetChanged之后,ListView不会直观地更新其内容

时间:2013-05-08 09:01:19

标签: android listview adapter onstart

我有一个静态的项目数组,它被设置为listview,我可以从另一个Activity修改(向它添加更多的项目)。因此,当我回到第一个活动时,我必须使用新项目刷新列表视图的内容。

我通过adapter.notifyDataSetChanged()方法执行此操作:onStart(),当然要验证我是否必须这样做。

问题是,只有当我滚动列表时,我才会看到我的列表视图发生了变化。

问题是什么?

LE 解决方法是在更改Activity之前设置 listview.invalidateViews(),当我返回第一个活动时,通知适配器结束它的目的。

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。此适配器正在扩展ArrayAdapter。

1 个答案:

答案 0 :(得分:2)

解决方法是在更改Activity之前设置listview.invalidateViews()并返回第一个活动,通知适配器结束它的目的。