我的GridView应该包含7个元素,结果我只看到一个(数据ArrayList的最后一个)。
LevelsAdapter扩展了BaseAdapter:
private TextView star;
private TextView label;
private TextView percent;
Typeface font;
private ArrayList<Level> levels;
LayoutInflater inflater;
public LevelsAdapter(Activity context, ArrayList<Level> levels) {
super();
this.levels = levels;
font = Typeface.createFromAsset(context.getAssets(), "fonts/Boogaloo-Regular.ttf");
inflater = context.getLayoutInflater();
}
@Override
public int getCount() {
return levels.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
Log.d("LevelsAdapter2", "position is "+position+", convertView is "+convertView);
View vi=convertView;
if(convertView==null) vi = inflater.inflate(R.layout.item_level, null);
star = (TextView) vi.findViewById(R.id.star);
percent = (TextView) vi.findViewById(R.id.percent);
label = (TextView) vi.findViewById(R.id.level_number);
star.setTypeface(font);
label.setTypeface(font);
percent.setTypeface(font);
Level lvl = levels.get(position);
percent.setText(lvl.getPercent() + "%");
label.setText("#"+lvl.getId());
return vi;
}
正如您在每次调用getView方法时所看到的那样,我们记录其当前的内容。然后LogCat返回奇怪的行:
LevelsAdapter2(15508): position is 0, convertView is null
LevelsAdapter2(15508): position is 0, convertView is android.widget.RelativeLayout@41a9d120
LevelsAdapter2(15508): position is 0, convertView is android.widget.RelativeLayout@41a9d120
LevelsAdapter2(15508): position is 0, convertView is android.widget.RelativeLayout@41a9d120
LevelsAdapter2(15508): position is 0, convertView is android.widget.RelativeLayout@41a9d120
LevelsAdapter2(15508): position is 0, convertView is null
LevelsAdapter2(15508): position is 0, convertView is android.widget.RelativeLayout@41ad9770
编辑: 错误是在创建ArrayList并填写错误时。帮助取代这个:
private void constructLevels() {
Level lvl = new Level();
for (int i=0; i<lvls.length; i++) {
int id = i + 1;
lvl.setId(id);
lvl.setWord(lvls[i]);
}
addLevel(lvl);
}
到此:
private void constructLevels() {
Level lvl;
for (int i=0; i<lvls.length; i++) {
lvl = new Level();
int id = i + 1;
lvl.setId(id);
lvl.setWord(lvls[i]);
addLevel(i, lvl);
}
}
答案 0 :(得分:3)
替换它:
@Override
public Object getItem(int position) {
return position;
}
有了这个:
@Override
public Object getItem(int position) {
return levels.get(position);
}
答案 1 :(得分:0)
试试这个 的 1。替换此方法
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView==null){
convertView = inflater.inflate(R.layout.item_level, null);
holder = new ViewHolder();
holder.star = (TextView) vi.findViewById(R.id.star);
holder.percent = (TextView) vi.findViewById(R.id.percent);
holder.label = (TextView) vi.findViewById(R.id.level_number);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
Log.d("LevelsAdapter2", "position is "+position+", convertView is "+holder);
holder.star.setTypeface(font);
holder.label.setTypeface(font);
holder.percent.setTypeface(font);
Level lvl = levels.get(position);
holder. percent.setText(lvl.getPercent() + "%");
holder.label.setText("#"+lvl.getId());
convertView.setTag(holder);
return convertView;
}
<强> 2。将此类添加到适配器
static class ViewHolder{
TextView star;
TextView percent;
TextView label;
}