나는 버튼을 만들 때마다 버튼을 생성하고, 태그를주고 배열에 추가하는 등, gradview에 16 개의 버튼을 만드는 어댑터를 가지고있다. 나는이 문제의 원인을 발견Android, 어댑터가 배열에 추가 요소를 추가 하시겠습니까?
public class ButtonAdapter extends BaseAdapter {
private Context mContext;
ArrayList<String> colorsArray = new ArrayList<String>(0);// add button tags
public ButtonAdapter(Context c) {
mContext = c;
}
public int getCount() {
int a = 16;// HOW MANY TILES WILL THE ADAPTER DISPLAY AND CREATE IN
// THE GRID VIEW, REFRENCED IN THE onCreate METHOD BELOW
return a;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
final Button button;
if (convertView == null) {
int r = 1 + (int) (Math.random() * 5);
button = new Button(mContext);
button.setLayoutParams(new GridView.LayoutParams(100, 100));
button.setText("" + r);// set text to random number
// give each button a new reference number
int r4 = 1 + (int) (Math.random() * 5);
int r3 = 1 + (int) (Math.random() * 4);
int r2 = 1 + (int) (Math.random() * 3);
int r1 = 1 + (int) (Math.random() * 2);
// setup attributes for each button
if (r == 5) {
button.setBackgroundColor(yellow);
button.setText("" + r4);// set text to new random number
colorsArray.add("yellow");
button.setTag("yellow");
} else if (r == 4) {
button.setBackgroundColor(green);
button.setText("" + r3);
colorsArray.add("green");
button.setTag("green");
} else if (r == 3) {
button.setBackgroundColor(red);
button.setText("" + r2);
colorsArray.add("red");
button.setTag("red");
} else if (r == 2) {
button.setBackgroundColor(blue);
button.setText("" + r1);
colorsArray.add("blue");
button.setTag("blue");
} else if (r == 1) {
button.setBackgroundColor(purple);
colorsArray.add("purple");
button.setTag("purple");
} else {
button.setBackgroundColor(white);
button.setText("0");
}
Log.d(TAG, colorsArray.toString());
} else {
button = (Button) convertView;
}
return button;
17 개의 항목이있는 로그를 어디에서 인쇄 했습니까? –
어댑터의 getView 메소드의 botton에는 ..Log.d (TAG, colorsArray.toString()); –
코드로 판단하면 어디에서 결함인지 아직 판단 할 수 없으므로 더 나은 디버그 출력을 얻으려면 로그에 위치를 함께 포함 할 수 있습니다. –