我不熟悉制作自定义适配器,但是我已经看到并遵循了很多我在网上看过的例子。我不知道为什么我的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永远不会被调用
答案 0 :(得分: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;
}
}