4

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

我通过以下方式做到这一点:adapter.notifyDataSetChanged()onStart()方法中,当然要验证我是否必须这样做。

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

问题可能是什么?

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

乐@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。

4

1 回答 1

3

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

于 2013-05-13T07:50:07.573 回答