在非活动类

时间:2017-01-07 18:21:34

标签: java android gridview simpleadapter

当我在Activity类中使用以下代码时,它可以正常工作,但是当移动到单独的类中时,会绘制表格,但不会显示任何内容。

String text[] = getResources().getStringArray(R.array.main_table);

我检查了所有变量(text,fillMaps,from),并且可以获得正确的数据。另外,如果我使用标准的SimpleAdapter,结果是一样的。

如果有人有想法,我将不胜感激。

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(context, fillMaps, R.layout.calendar_title_cell, from, to);
    statistic.setAdapter(adapter);

}



private class MyCursorAdapter extends SimpleAdapter {

    //private variables
    Context context;
    LayoutInflater inflater;
    public MyCursorAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to) {
        super(context, data, resource, from, to);
        this.context = context;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            convertView = inflater.inflate(R.layout.statistic_cell, parent, false);
        }

        Set<Integer> VALUES = new HashSet<Integer>(Arrays.asList(
                new Integer[]{0, 1, 2, 3, 4, 5, 10, 15, 20}
        ));

        if (VALUES.contains(Integer.valueOf(position))) {
            convertView.setBackgroundColor(getResources().getColor(R.color.colorPrimary));

            TextView tv = (TextView) convertView.findViewById(R.id.statistic_cell_text);
            tv.setTextColor(getResources().getColor(R.color.statistic_title));
            convertView.setPadding(0, 0, 0, 0);
        } else {
            convertView.setBackground(getResources().getDrawable(R.drawable.border_intern));
        }
        return convertView;
    }
}

}

0 个答案:

没有答案