2014-10-07 76 views
0

我不熟悉製作自定義適配器,但是我已經看到並遵循了我在線看到的很多示例。我不知道爲什麼我的getView沒有被調用。爲什麼我的自定義getView未被調用?

下面是代碼:

private String[] numbers = new String[] { 

"42", "0", "0", "39", "32", "0", "0", "0", "0", "0", "0", "0", "0", "0", 
     "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "45", "0", "51", 
     "36", "35", "20", "0", "22", "0", "53", "0", "1", "2", "0", "16", 
     "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "5", 
     "6", "0", "0", "0", "57", "0", "0", "64", "7", "0", "0", "0" }; 



@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
//other code 
grid = (GridView) findViewById(R.id.GridViewGame); 
    CustomArrayAdapter adapter = new CustomArrayAdapter(this, 
      R.layout.item_layout, R.id.editTextGridItem, numbers); 
    grid.setAdapter(adapter); 

的CustomArrayAdapter類:

public class CustomArrayAdapter extends ArrayAdapter<String> { 

public CustomArrayAdapter(Context context, int resource, 
     int textViewResourceId, Object[] objects) { 

    super(context, resource, textViewResourceId); 

} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    View v = convertView; 
    if (v == null) { 
     LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     v = inflater.inflate(R.layout.item_layout, null); 
    } 
    else{ 
    v.setTag(Integer.toString(position)); 
    System.out.println(v.getTag()); 
    } 
    return v; 
} 

總體什麼,我想設置一個視圖到GridView(在我來說,每一個細胞都含有1 EDITTEXT)時發生這種情況我想分配editText一個將匹配它在數字[]中的位置的標籤。我不確定你的代碼我現在會這樣做,但因爲getView永遠不會被調用

回答

1

您還沒有將對象數組傳遞給父ArrayAdapter類,所以它認爲有零個項目要顯示。

構造函數改成這樣:

public class CustomArrayAdapter extends ArrayAdapter<Object> { 

    public CustomArrayAdapter(Context context, int resource, 
     int textViewResourceId, Object[] objects) { 

     super(context, resource, textViewResourceId, objects); 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     View v = convertView; 
     if (v == null) { 
      LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      v = inflater.inflate(R.layout.item_layout, null); 
     } 
     else{ 
     v.setTag(Integer.toString(position)); 
     System.out.println(v.getTag()); 
     } 
     return v; 
    } 
} 
+0

謝謝。這是這個問題。構造函數失蹤的原因是由於錯誤。顯然,正確的解決方法是將對象投射到一個字符串[]。然而,我刪除了變量,因爲它是java給我的第一個修復列表。無論如何,再次感謝你。 – Noob 2014-10-07 23:09:51

相關問題