繁体   English   中英

Android Studio:在非活动类中访问子项时出现问题

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

更新:现在可以正确显示网格,但是缺少内容

我检查了

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

并且文本用字符串填充,如果我使用活动文件中的代码,则会显示数据。

有任何想法吗


原始问题

我在非活动类中使用findViewById()时遇到问题。 如果我处于最高级别,那就没问题。

((Activity)context).findViewById(R.id.statistic_grid);

但是,当我尝试引用子视图时,我会“ null”

View view = super.getView(position, convertView, parent);
TextView tv = (TextView) view.findViewById(R.id.statistic_cell_text); 

可以请任何人帮助我。

提前致谢

拉尔斯

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

}

您需要像这样膨胀布局。 扩充布局后,即可获得子项。

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

请注意,我没有对此进行测试。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM