2014-10-07 62 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

相关问题