开发者

Unwanted display from getView

I have a listview that uses a custom layout. Inside the layout is a pin imageview. The purpose of this开发者_JAVA技巧 is to keep the same colors to the appropriate position in the list. The variable that stores which positions have pins is called int[] ping. if ping[position]==1 then set a drawing there, otherwise leave it invisible. This works when I have to refresh my list. For example, the first item will always have a red pin, second will be lite blue etc...

My problem is when I refresh my list (automatically calls the getview in my special adapter) and I didn't select the first item (Item 1 is invisible). After going through the getView function instead of an invisible item I now see the item in the highest position. (eg if I have a black and blue pin on at the same time I will see a black pin in the first spot which should be impossible because the first spot should only have a red pin). This seems like an easy fix but I can't seem to figure it out. Here is my code for the special adapter. Thanks!

EDIT The code is much shorter now because I'm putting my drawables into an array.

final public static int[]pincolors={R.drawable.pinred,R.drawable.pinliteblue,R.drawable.pinblack,R.drawable.pindarkblue,R.drawable.pingreen,R.drawable.pinlitegreen,R.drawable.pinorange,R.drawable.pinyellow,R.drawable.pinwhite,R.drawable.pinpurple};

public class SpecialAdapter extends SimpleAdapter {
    private int[] colors = new int[] { R.drawable.row_background_grey,
            R.drawable.row_background_white };

    public SpecialAdapter(Context context,
            ArrayList<HashMap<String, String>> list, int resource,
            String[] from, int[] to) {
        super(context, list, resource, from, to);

    }

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

        View view = super.getView(position, convertView, parent);
        ImageView pincolor = (ImageView) view
                .findViewById(R.id.ivimtrackingpin);

        if (ping[position] == 1) {
                pincolor.setImageResource(pincolors[position]);
                pincolor.setVisibility(View.VISIBLE);
            }

        int colorPos = position % colors.length;
        view.setBackgroundResource(colors[colorPos]);
        return view;
    }

}

PS. if I DO have the first item on (Red pin is showing) when I refresh, the red pin does not get changed!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜