2013-05-02 8 views
0

나는 버튼을 만들 때마다 버튼을 생성하고, 태그를주고 배열에 추가하는 등, 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; 
+0

17 개의 항목이있는 로그를 어디에서 인쇄 했습니까? –

+0

어댑터의 getView 메소드의 botton에는 ..Log.d (TAG, colorsArray.toString()); –

+0

코드로 판단하면 어디에서 결함인지 아직 판단 할 수 없으므로 더 나은 디버그 출력을 얻으려면 로그에 위치를 함께 포함 할 수 있습니다. –

답변

0

(16 개) 버튼을 ... 만들 때 17 개 요소를 포함하는 이유 :의 GridView는 항상 1 개의 여분보기, 모든 뷰가 표시인지 아닌지에 상관없이 생성합니다. 16 개의 뷰가 보이

예 : enter image description here

은 처음에는 16 만들어 뷰 디스플레이 (마지막 뷰 한 적색). 그런 다음 표시되지 않을 1 개의 추가보기 (보기 17, 빨간색 2)를 작성했습니다.

왜 GridView가 이것을하는지 모르겠습니다. 안드로이드 팀의 누군가는이 막연한 방법으로이 컨트롤을 구현하기로 결정했습니다.

어댑터에 대해서는 colorsArray을 생성자로 채우고 다른 위치에서는 변경하지 않는 것이 좋습니다. 그것이 16 개 이상의 아이템을 포함하지 않을 것입니다.

+0

안녕하세요 vorrtex ... 그런 in-dept 답장을 보내 주셔서 감사합니다.) ... 이제 어댑터에 대해 더 많이 알았습니다 ... 저는 coloArray를 생성자로 옮겼지만 여전히 17 개의 요소를 읽었습니다. 나는 단지 colorsArray.remove (colorsArray.size() - 1); 어댑터가 실행 된 후 –

+0

@ronanc getView 및 getCount 메소드를 변경하여'colorsArray.get (position)'및'colorsArray.getCount()'를 적절하게 호출해야합니다. 그리고 어댑터는 필요에 따라 많은 요소를 읽을 수 있으므로 colorsArray는 동일하게 유지됩니다. – vorrtex

관련 문제