getView仅显示绘制视图的标记背景,滚动查看列表中的更多项目时不显示标记背景

时间:2012-06-12 22:36:25

标签: android android-listview flags recycle

我正在尝试创建一个列表视图,该列表视图接收标记的项目列表以向用户指示状态。我已经能够使用黄色背景创建标志显示(见下图)。在Theory中,标记列表中可以包含许多标记的项目。但是在我的应用程序中,只显示前三个标记的背景。我相信这是因为它们最初被吸引到了屏幕上。最初显示在屏幕上的已标记背景不会显示。我想知道如何让剩余的标志显示在列表中。

ListView回收: listView中的背景正在getView()中回收。这种回收从位置0到位置9.我有需要在位置13,14匹配的标志等。这些职位没有显示出来。

listView.getCheckedItemPositions()用于多个选择: 这种方法在我的情况下不起作用,因为用户不会选择标志。标志来自服务器。

setNotifyOnChange()和/或public virtual void SetNotifyOnChange(bool notifyOnChange): 我没有在列表中添加新数据,所以我看不出这个方法对我的程序有用。这种方法在回收数据时是否与getview通信?在我的研究中,我无法找到答案。

public void registerDataSetObserver: 这可能对我的问题有点过分,但是有可能有一个观察者对象跟踪我的项目列表和我的标志列表中的所有位置,无论视图是否被回收并在屏幕上匹配它们?

flagged backgrounds from server http://www.leobee.com/android/push/so/flag.jpg

代码:

package com.convention.notification.app;

import java.util.Iterator;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class NewsRowAdapter extends ArrayAdapter<Item> {

    private Activity activity;
    private List<Item> items;
    private Item objBean;
    private int row;
    private List<Integer> disable;
    View view ;
    int disableView;

    public NewsRowAdapter(Activity act, int resource, List<Item> arrayList, List<Integer> disableList) {
        super(act, resource, arrayList);
        this.activity = act;
        this.row = resource;
        this.items = arrayList;
        this.disable=disableList;

        System.out.println("results of delete list a:"+disable.toString()); 

    }

    public int getCount() {
        return items.size();        
    }
    public Item getItem(int position) {
        return items.get(position);
    }
    public long getItemId(int position) {
        return position;
    }


        @Override
        public int getItemViewType(int position) {

        for(int k =0;k < disable.size();k++){
            if(position==disable.get(k)){

            //System.out.println( "is "+position+" value of disable "+disable.get(k));
                disableView=disable.get(k);

                //AdapterView.getItemAtPosition(position);
            } 

        }



    return position;
}

            @Override
            public View getView(final int position, View convertView, ViewGroup parent) {
                View view = convertView;
                ViewHolder holder;

                if (view == null) {
                    LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    view = inflater.inflate(row, null);

                    getItemViewType(position);
                    long id=getItemId(position);


                    if(position==disableView){ 
                        view.setBackgroundColor(Color.YELLOW);
                        System.out.println(" background set to yellow at position "+position +" disableView is at "+disableView);


                        }else{
                            view.setBackgroundColor(Color.WHITE);
                            System.out.println(" background set to white at position "+position +" disableView is at "+disableView);

                            }


                    //ViewHolder is a custom class that gets TextViews by name: tvName, tvCity, tvBDate, tvGender, tvAge;
                    holder = new ViewHolder();

                    /* setTag Sets the tag associated with this view. A tag can be used to
                     *  mark a view in its hierarchy and does not have to be unique 
                     *  within the hierarchy. Tags can also be used to store data within
                     *   a view without resorting to another data structure.

        */
                    view.setTag(holder);
                } else {

                    //the Object stored in this view as a tag
                    holder = (ViewHolder) view.getTag();
                }

                if ((items == null) || ((position + 1) > items.size()))
                    return view;

                objBean = items.get(position);


        holder.tv_event_name = (TextView) view.findViewById(R.id.tv_event_name);
        holder.tv_event_date = (TextView) view.findViewById(R.id.tv_event_date);
        holder.tv_event_start = (TextView) view.findViewById(R.id.tv_event_start);
        holder.tv_event_end = (TextView) view.findViewById(R.id.tv_event_end);
        holder.tv_event_location = (TextView) view.findViewById(R.id.tv_event_location);


        if (holder.tv_event_name != null && null != objBean.getName()
                && objBean.getName().trim().length() > 0) {
            holder.tv_event_name.setText(Html.fromHtml(objBean.getName()));

        }
        if (holder.tv_event_date != null && null != objBean.getDate()
                && objBean.getDate().trim().length() > 0) {
            holder.tv_event_date.setText(Html.fromHtml(objBean.getDate()));
        }
        if (holder.tv_event_start != null && null != objBean.getStartTime()
                && objBean.getStartTime().trim().length() > 0) {
            holder.tv_event_start.setText(Html.fromHtml(objBean.getStartTime()));
        }
        if (holder.tv_event_end != null && null != objBean.getEndTime()
                && objBean.getEndTime().trim().length() > 0) {
            holder.tv_event_end.setText(Html.fromHtml(objBean.getEndTime()));
        }
        if (holder.tv_event_location != null && null != objBean.getLocation ()
                && objBean.getLocation ().trim().length() > 0) {
            holder.tv_event_location.setText(Html.fromHtml(objBean.getLocation ()));

        }


        return view;
    }

    public class ViewHolder {
        public TextView 
        tv_event_name,
        tv_event_date,
        tv_event_start,
        tv_event_end,
        tv_event_location
        /*tv_event_delete_flag*/;


    }


}

logcat的:

06-12 20:54:12.058: I/System.out(493): item disalbed is at postion :0
06-12 20:54:12.058: I/System.out(493): item disalbed is at postion :4
06-12 20:54:12.069: I/System.out(493): item disalbed is at postion :5
06-12 20:54:12.069: I/System.out(493): item disalbed is at postion :13
06-12 20:54:12.069: I/System.out(493): item disalbed is at postion :14
06-12 20:54:12.069: I/System.out(493): item disalbed is at postion :17
06-12 20:54:12.069: I/System.out(493): results of delete list :[0, 4, 5, 13, 14, 17]
06-12 20:54:12.069: I/System.out(493): results of delete list a:[0, 4, 5, 13, 14, 17]
06-12 20:54:12.069: I/System.out(493):  set adapaer to list view called;
06-12 20:54:12.128: I/System.out(493):  background set to yellow at position 0 disableView is at 0
06-12 20:54:12.628: I/System.out(493):  background set to white at position 1 disableView is at 0
06-12 20:54:12.678: I/System.out(493):  background set to white at position 2 disableView is at 0
06-12 20:54:12.708: I/System.out(493):  background set to white at position 3 disableView is at 0
06-12 20:54:12.738: I/System.out(493):  background set to yellow at position 4 disableView is at 4
06-12 20:54:12.778: I/System.out(493):  background set to yellow at position 5 disableView is at 5
06-12 20:54:12.808: I/System.out(493):  background set to white at position 6 disableView is at 5
06-12 20:54:12.838: I/System.out(493):  background set to white at position 7 disableView is at 5

这是我前一天的第一个问题的链接:

Change Background on a specific row based on a condition in Custom Adapter

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

好的,答案是我必须在else部分重复if(view == null)的代码。

if (view == null) {
                    LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    view = inflater.inflate(row, null);

                    getItemViewType(position);
                    long id=getItemId(position);


                    if(position==disableView){ 
                        view.setBackgroundColor(Color.YELLOW);
                        System.out.println(" background set to yellow at position "+position +" disableView is at "+disableView);


                        }else{
                            view.setBackgroundColor(Color.WHITE);
                            System.out.println(" background set to white at position "+position +" disableView is at "+disableView);

                            }


                    //ViewHolder is a custom class that gets TextViews by name: tvName, tvCity, tvBDate, tvGender, tvAge;
                    holder = new ViewHolder();

                    /* setTag Sets the tag associated with this view. A tag can be used to
                     *  mark a view in its hierarchy and does not have to be unique 
                     *  within the hierarchy. Tags can also be used to store data within
                     *   a view without resorting to another data structure.

        */
                    view.setTag(holder);
                } else {

                    getItemViewType(position);
                    long id=getItemId(position);


                    if(position==disableView){ 
                        view.setBackgroundColor(Color.YELLOW);
                        System.out.println(" background set to yellow at position "+position +" disableView is at "+disableView);


                        }else{
                            view.setBackgroundColor(Color.WHITE);
                            System.out.println(" background set to white at position "+position +" disableView is at "+disableView);

                            }