Android Studio : Problems accessing Sub-Item in non activity class

84 views Asked by At

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

}

1

There are 1 answers

2
AudioBubble On BEST ANSWER

You need to inflate the layout, like this. Once you inflate the layout you can get the child items.

    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;
//get layoutinflater
         inflater = LayoutInflater.from(context);
        }

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

//inflate your layout
           if(convertView==null)
    {
          convertView = inflater.inflater(layoutfile, parent,false);
    }     


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

Please note I did not test this.