Java 在非活动类中创建时,GridView中未显示任何数据
当我在Java 在非活动类中创建时,GridView中未显示任何数据,java,android,gridview,simpleadapter,Java,Android,Gridview,Simpleadapter,当我在活动类中使用以下代码时,它可以正常工作,但当移到单独的类中时,会绘制表格,但不会显示任何内容 String text[] = getResources().getStringArray(R.array.main_table); 我检查了所有变量(文本、填充贴图、从到),正确的数据可用。如果我使用标准的SimpleAdapter,结果也是一样的 如果有人有主意,我将不胜感激 public class MainStatisticGridView extends GridView { p
活动
类中使用以下代码时,它可以正常工作,但当移到单独的类中时,会绘制表格,但不会显示任何内容
String text[] = getResources().getStringArray(R.array.main_table);
我检查了所有变量(文本、填充贴图、从到),正确的数据可用。如果我使用标准的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;
}
}
公共类主统计GridView扩展了GridView{
公共主统计GridView(上下文){
超级(上下文);
GridView统计=(GridView)((活动)上下文).findViewById(R.id.statistic_grid);
String[]from=新字符串[]{“text”};
int[]to=新int[]{R.id.statistic\u cell\u text};
List fillMaps=new ArrayList();
字符串文本[]=getResources().getStringArray(R.array.main_表);
for(int i=0;i数据,int资源,字符串[]从,int[]到){
超级(上下文、数据、资源、从、到);
this.context=上下文;
充气器=充气器。从(上下文);
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
if(convertView==null){
convertView=充气机。充气(R.layout.statistic\u单元格,父项,false);
}
Set VALUES=新的HashSet(Arrays.asList(
新整数[]{0,1,2,3,4,5,10,15,20}
));
if(VALUES.contains(Integer.valueOf(position))){
setBackgroundColor(getResources().getColor(R.color.colorPrimary));
TextView tv=(TextView)convertView.findViewById(R.id.statistic\u cell\u text);
tv.setTextColor(getResources().getColor(R.color.statistic_title));
setPadding(0,0,0,0);
}否则{
setBackground(getResources().getDrawable(R.drawable.border_intern));
}
返回视图;
}
}
}确保AdapterAndroid Studio中存在
getCount()
,如果该IDE只是IDE,则仅对IDE中的问题使用该标记。如果您是从Eclipse或命令行编译的,则会遇到相同的运行时问题。@ρцσѕρєK,很抱歉,我不明白您所说的“确保getCount()在适配器中存在”的意思。它应该做什么?