我正在尝试使用自定义的ArrayAdapter从我的android活动的布局中填充GridView,其中我覆盖了getView()。当我运行应用程序时,GridView显示在屏幕上但完全清空。我推断问题是getView()永远不会被调用。我确实检查过ArrayList不是空的。我使用了一些日志语句(类似于System.out.print()用于那些不熟悉Android编程的人),并发现我传递给适配器的ArrayList不是空的,但是我的适配器的getCount()方法总是返回0.我试图通过一些android源代码来查找getView()应该被调用,这导致getsView()和mObjects。这一切似乎很直接,我没有发现任何错误。除非出现某种反奇迹的解释,到目前为止,我已经空了(可能是因为我是一名业余程序员)。任何帮助将不胜感激。
活动文件:
import java.util.ArrayList;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.*;
import android.widget.*;
public class LevelMapActivity extends FragmentActivity implements View.OnClickListener{
public final int TOTAL_LEVELS = 12;//TODO Set TOTAL_LEVELS to figure out how many levels exist.
public final int COLUMNS = 5;
GridView grid;
LevelSelectorAdapter adapter;
ArrayList<LevelSelector> levels;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_level_map);
grid = (GridView)findViewById(R.id.GridView_LevelMap);
grid.setNumColumns(COLUMNS);
levels = new ArrayList<LevelSelector>();
for(int i=0; i<TOTAL_LEVELS/5; i++){
for(int j=0; j<5; j++){
LevelSelector levelSelector = new LevelSelector(this, i*COLUMNS+j+1);
levels.add((i*COLUMNS)+j, levelSelector);
}
}
for(int j=0; j<TOTAL_LEVELS%5; j++){
LevelSelector levelSelector = new LevelSelector(this, TOTAL_LEVELS-(COLUMNS-1)+j);
levels.add(j+TOTAL_LEVELS-COLUMNS, levelSelector);
}
adapter = new LevelSelectorAdapter(this, R.layout.level_selector, levels);
grid.setAdapter(adapter);
}//ends onCreate()
@Override
public void onPause(){
// TODO Auto-generated method stub
super.onPause();
}//ends onPause()
@Override
public void onClick(View view){
// TODO Auto-generated method stub
}//ends onClick()
}//ends Class
适配器文件:
import java.util.ArrayList;
import android.content.Context;
import android.util.Log;
import android.view.*;
import android.widget.*;
public class LevelSelectorAdapter extends ArrayAdapter<LevelSelector>{
Context context;
int resource;
ArrayList<LevelSelector> levelSelectors;
public LevelSelectorAdapter(Context context, int resource, ArrayList<LevelSelector> levelSelectors){
super(context, resource);
this.context = context;
this.resource = resource;
this.levelSelectors = levelSelectors;
}//ends LevelSelectorAdapter()
public static class ViewHolder{
public TextView level;
public TextView time;
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
View view = convertView;
ViewHolder holder;
if(view == null){
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(resource, null);
holder = new ViewHolder();
holder.level = (TextView)view.findViewById(R.id.TextView_LevelSelector_Level);
holder.time = (TextView)view.findViewById(R.id.TextView_LevelSelector_Time);
view.setTag(holder);
}
else{
holder = (ViewHolder)view.getTag();
}
final LevelSelector levelSelector = levelSelectors.get(position);
if(levelSelector != null){
holder.level.setText(Integer.toString(levelSelector.getLevel()));
holder.time.setText(Integer.toString(levelSelector.getTime()));
}
return view;
}//ends getView()
}//ends Class
答案 0 :(得分:2)
由于我尝试使用适配器尝试覆盖getView()
方法的方式之一,我之前不需要在其构造函数中将数组传递给适配器。
将数组传递给超级构造函数会导致它被启动,因此可以在getCount()
方法中使用。这就是getCount()
每次出现0的原因,即使数组不是空的。