Update : the Grid is now displayed correctly, but the content is missing
I checked
String text[] = getResources().getStringArray(R.array.main_table);
and text is filled with the Strings and if I use the code in the activity file the data is shown.
Any ideas
Original Question
I am having problems using findViewById() in non-activity classes. If I am on the top Level, it is no problem.
((Activity)context).findViewById(R.id.statistic_grid);
But when I am trying to reference child views, I am getting "null"
View view = super.getView(position, convertView, parent);
TextView tv = (TextView) view.findViewById(R.id.statistic_cell_text);
Can please anybody help me out.
Thanks in advance
Lars
public class MainStatisticGridView extends GridView {
public MainStatisticGridView(Context context) {
super(context);
GridView statistic = (GridView) ((Activity)context).findViewById(R.id.statistic_grid);
String[] from = new String[]{"text"};
int[] to = new int[]{R.id.statistic_cell_text};
List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>();
String text[] = getResources().getStringArray(R.array.main_table);
for (int i = 0; i < text.length; i++) {
HashMap<String, String> map = new HashMap<String, String>();
map.put("text", text[i]);
fillMaps.add(map);
}
SimpleAdapter adapter = new MyCursorAdapter(((Activity)context), fillMaps, R.layout.calendar_title_cell, from, to);
statistic.setAdapter(adapter);
}
private class MyCursorAdapter extends SimpleAdapter {
public MyCursorAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to) {
super(context, data, resource, from, to);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Set<Integer> VALUES = new HashSet<Integer>(Arrays.asList(
new Integer[]{0, 1, 2, 3, 4, 5, 10, 15, 20}
));
View view = super.getView(position, convertView, parent);
if (VALUES.contains(Integer.valueOf(position))) {
view.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
TextView tv = (TextView) view.findViewById(R.id.statistic_cell_text);
tv.setTextColor(getResources().getColor(R.color.statistic_title));
view.setPadding(0, 0, 0, 0);
} else {
view.setBackground(getResources().getDrawable(R.drawable.border_intern));
}
return view;
}
}
}
You need to inflate the layout, like this. Once you inflate the layout you can get the child items.
Please note I did not test this.