Noob Noob - 5 months ago 25
Android Question

Why is my custom getView Not called?

I am not familiar with making custom adapters, however I have seen and followed many examples I've seen online. I do not know why my getView is not getting called.

Here is the code:

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);


The CustomArrayAdapter class:

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;
}


Overall what I am trying to set a view to the gridview (In my case every cell contains 1 editText) when this happens I want to assign that editText a tag that will match it's position in numbers[]. I am unsure if you code I have now will do that but because getView is never called

Answer

You have not passed the array of objects to the parent ArrayAdapter class, so it thinks that there are zero items to show.

Change your constructor to this:

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;
    }
}