Adapter not updating in UI....

  • Replies:1
ronan clancy
  • Forum posts: 3

May 20, 2013, 10:48:13 PM via Website

Can any one tell me why my imageview adapter wont update in the UI....when i print Log.d i see the new color was added to the array...


1public class GameScreen extends Activity {
2 int yellow = 0xffffff66;
3 int green = 0xff00EE76;
4 int red = 0xffff4342;
5 int blue = 0xff42c3ff;
6 int purple = 0xff9932CC;
7 int white = 0xFFFFFFFF;
8
9 int total_Count = 0;
10 int colorPosition = 0;
11 int colorPickerStart = 0;
12 Button button;
13
14 ImageView imageView;
15
16 ImageAdapter ia;
17 GridView gridview2;
18
19 ArrayList<Integer>nextColorArray = new ArrayList<Integer>(10);
20 ArrayList<Integer>colorPicker = new ArrayList<Integer>();
21
22 @Override
23 protected void onCreate(Bundle savedInstanceState) {
24 super.onCreate(savedInstanceState);
25 setContentView(R.layout.game_screen);
26
27 //set adapters
28 GridView gridview = (GridView) findViewById(R.id.gridViewGame);
29 gridview.setAdapter(new ButtonAdapter(this));
30
31 gridview2 = (GridView) findViewById(R.id.colorNext);
32 ia = new ImageAdapter(this, nextColorArray);
33 gridview2.setAdapter(ia);
34
35 //next color elements
36 nextColorArray.add(blue);
37 nextColorArray.add(green);
38 nextColorArray.add(red);
39 nextColorArray.add(yellow);
40 nextColorArray.add(purple);
41
42 }
43 public class ImageAdapter extends BaseAdapter {
44 private Context mContext;
45 ArrayList<Integer> a;
46
47 public ImageAdapter(Context c, ArrayList<Integer> a) {
48 mContext = c;
49 this.a = a;
50 }
51 public int getCount() {
52 int a = 5;
53 return a;
54 }
55
56 public Object getItem(int position) {
57 return null;
58 }
59
60 public long getItemId(int position) {
61 return 0;
62 }
63
64 /*create a new ImageView for each item referenced by the Adapter*/
65 public View getView(int position, View convertView, ViewGroup parent) {
66 if (convertView == null) {/*if it's not recycled, initialize some attributes*/
67 imageView = new ImageView(mContext);
68 imageView.setLayoutParams(new GridView.LayoutParams(50, 45));
69 imageView.setBackgroundColor(nextColorArray.get(colorPosition));
70 if(colorPosition < 9) colorPosition++;
71 } else {
72 imageView = (ImageView) convertView;
73 }
74 return imageView;
75 }
76
77 }
78 /*button adapter*/
79 public class ButtonAdapter extends BaseAdapter {
80 private Context mContext;
81
82 public ButtonAdapter(Context c) {
83 mContext = c;
84 }
85
86 public int getCount() {
87 int a = 16;
88 return a;
89 }
90
91 public Object getItem(int position) {
92 return null;
93 }
94
95 public long getItemId(int position) {
96 return 0;
97 }
98
99 // create a new button for each item referenced by the Adapter
100 public View getView(int position, View convertView, ViewGroup parent) {
101
102 if (convertView == null) {
103
104 button = new Button(mContext);
105 button.setLayoutParams(new GridView.LayoutParams(128, 128));
106 button.setBackgroundColor(colorPicker.get(colorPickerStart));
107 if(colorPickerStart < 15) colorPickerStart++;
108 button.setOnClickListener(new Button.OnClickListener() {
109 @Override
110 public void onClick(View arg0) {
111 if(total_Count > 10){
112 nextColorArray.add(0, white);
113 ia.notifyDataSetChanged();
114 }
115 }
116 });
117 total_Count++;
118 } else {
119 button = (Button) convertView;
120 }
121 return button;
122 }// end get view
123 }// end button adapter
124}

Reply
FChopin Pich
  • Forum posts: 6

May 24, 2013, 12:52:25 AM via Website

do not use int as a parameter for imageView.setBackgroundColor
because " imageView.setBackgroundColor(0x123456); " don't work at all.

change to this
" view.setBackgroundColor(Color.parseColor("#e8e8e8")); "

Optional
and you should add your data to nextColorArray before setAdapter (may be it can help)

Reply