大家好,我是Android新手。我正在開發包含ListAdapter的應用程序,並且我想從onResume中清除它。onResume不更新適配器
在Activity中指的是levelsView。
public class MyActivity extends Activity{
...
@Override
protected void onResume(){
super.onResume();
levelsView.onResume();
}
}
LevelsView指FontsSelect在那裏我有myAdapter
public class FontsSelect extends LinearLayout {
private List<FontLevels> fontsLevels;
public FontsSelect(Context context, AttributeSet attr, int defStyle){
super(context, attr, defStyle);
inflate(context, R.layout.list_view_of_fonts, this);
listViewFonts = (ListView)findViewById(R.id.list_of_fonts);
listViewFonts.setDivider(null);
fontsLevels = new ArrayList<FontLevels>();
levels = new ArrayList<Level>();
createLevels();
myAdapter = new MyAdapter(context);
listViewFonts.setOnItemClickListener(levelSelectListener);
listViewFonts.setAdapter(myAdapter);
}
public void createLevels(){
...
fontsLevels.add(new FontLevels(context, "fonts/TNR.ttf"))}
class MyAdapter extends BaseAdapter {
final LayoutInflater inflater;
public MyAdapter(Context context){
inflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return fontsLevels.size();
}
@Override
public Object getItem(int position) {
return fontsLevels.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if(convertView == null){
convertView = inflater.inflate(R.layout.item_level, parent, false);
holder = new ViewHolder((TextView)convertView.findViewById(R.id.level_name_typeface), (TextView)convertView.findViewById(R.id.level_score_number));
convertView.setTag(holder);
}
else {
holder = (ViewHolder)convertView.getTag();
}
...
return convertView;
}
}
public void onResume(){
listViewFonts.setAdapter(null);
fontsLevels.clear();
fontsLevels.add(new FontLevels(context, "fonts/TNR.ttf");
fontsLevels.add(new FontLevels(context, "fonts/TNR.ttf");
myAdapter = new MyAdapter(getContext(), fontsLevels);
listViewFonts.setAdapter(myAdapter);
myAdapter.notifyDataSetChanged();
}
}
這裏是ckass FontLevel
public class FontLevels{
private String fontFullName;
private int _id;
private List<Level> easyLevels;
private List<Level> hardLevels;
public FontLevels(Context context, String fontFullName){
this.fontFullName = fontFullName;
typeface = Typeface.createFromAsset(context.getAssets(), FONTS_DIR + fontFullName);
easyLevels = new ArrayList<Level>(levelNumber);
hardLevels = new ArrayList<Level>(levelNumber);
}
,並重新啓動後,我的應用程序,適配器不更新,並顯示舊列表。 請幫忙)) Thanx !!!
你確定你的數組列表正在當你重新啓動應用程序時更新?如果不是那麼它的一般行爲,以顯示舊值:) –
我debuged應用程序,並看到,重新啓動後,構造函數初始化數組(''createLevels()')和'getCount()'返回正確的值,但然後'onResume '清除數組和'getCount()= 0',最後我不明白getCount()是否返回舊值,因爲沒有人初始化數組 – IgorB
其實你已經定製了類並且隱藏了一些你的代碼無法獲取流和映射。你能用他們的參數和迴應發佈最少的方法嗎? –