我想從我的Android活動的佈局使用自定義ArrayAdapter,我在其中覆蓋getView()填充GridView。當我運行應用程序時,GridView顯示在屏幕上,但完全是空的。我推斷問題是getView()永遠不會被調用。我沒有檢查,看到ArrayList不是空的。我使用了一些日誌語句(對於不熟悉Android編程的人來說類似於System.out.print()),並發現我傳遞給適配器的ArrayList不是空的,但是我的適配器的getCount()方法總是返回0.我試圖通過一些android源代碼來查找何時getView()應該被調用,從而導致獲取View()和mObjects。這一切似乎相當簡單,我還沒有發現任何錯誤。除了某種反奇蹟的解釋,到目前爲止我已經空了(可能是因爲我是一個業餘程序員)。任何幫助將不勝感激。ArrayAdapter getView()不被稱爲
活動文件:
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