Android Studio:在非 activity class 中访问子项时出现问题
Android Studio : Problems accessing Sub-Item in non activity class
更新:
网格现在可以正确显示,但内容丢失
我查了
String text[] = getResources().getStringArray(R.array.main_table);
文本中填充了字符串,如果我使用 activity 文件中的代码,则会显示数据。
任何想法
原问题
我在非 activity 类 中使用 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;
}
}
请注意我没有对此进行测试。
更新: 网格现在可以正确显示,但内容丢失
我查了
String text[] = getResources().getStringArray(R.array.main_table);
文本中填充了字符串,如果我使用 activity 文件中的代码,则会显示数据。
任何想法
原问题
我在非 activity 类 中使用 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;
}
}
请注意我没有对此进行测试。