仅在活动第一次运行时填充ListView。重新创建活动后,不会填充列表。如果我从应用程序退出并再次打开它,它将无法正常工作。但是,如果我从任务菜单中删除它并再次打开它,它的工作原理。此外,ArrayAdapter在onResume和onImagesRetrieved中具有不同的count(getCount)。关于为什么会发生这种情况的任何想法?
public class GalleryActivity extends Activity implements ImagesDatabaseHelper.ImageDatabaseCallbacks {
private static final int NUMEBR_OF_IMAGES = 10;
private ArrayAdapter arrayAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gallery);
arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, new ArrayList());
((ListView)findViewById(R.id.lv_titles)).setAdapter(arrayAdapter);
}
@Override
protected void onResume() {
super.onResume();
ImagesDatabaseHelper.getInstance(this, NUMEBR_OF_IMAGES).retrieveImages();
}
@Override
public void onImagesRetrieved(List<Image> images) {
Toast.makeText(this, "Successfully got " + images.size() + " images", Toast.LENGTH_LONG).show();
arrayAdapter.clear();
arrayAdapter.addAll(images);
arrayAdapter.notifyDataSetChanged();
}
}
答案 0 :(得分:0)
试试这个,
public class GalleryActivity extends Activity implements ImagesDatabaseHelper.ImageDatabaseCallbacks {
private static final int NUMEBR_OF_IMAGES = 10;
private ArrayAdapter arrayAdapter;
List<Image> dataset = new ArrayList();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gallery);
arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, dataset);
((ListView)findViewById(R.id.lv_titles)).setAdapter(arrayAdapter);
}
@Override
protected void onResume() {
super.onResume();
ImagesDatabaseHelper.getInstance(this, NUMEBR_OF_IMAGES).retrieveImages();
}
@Override
public void onImagesRetrieved(List<Image> images) {
Toast.makeText(this, "Successfully got " + images.size() + " images", Toast.LENGTH_LONG).show();
dataset.clear();
dataset.addAll(images);
arrayAdapter.notifyDataSetChanged();
}
}
答案 1 :(得分:0)
试试这个:
public class GalleryActivity extends Activity implements ImagesDatabaseHelper.ImageDatabaseCallbacks {
private static final int NUMEBR_OF_IMAGES = 10;
private ArrayAdapter arrayAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gallery);
ImagesDatabaseHelper.getInstance(this, NUMEBR_OF_IMAGES).retrieveImages();
}
@Override
protected void onResume() {
super.onResume();
ImagesDatabaseHelper.getInstance(this, NUMEBR_OF_IMAGES).retrieveImages();
}
@Override
public void onImagesRetrieved(List<Image> images) {
Toast.makeText(this, "Successfully got " + images.size() + " images", Toast.LENGTH_LONG).show();
arrayAdapter.clear();
arrayAdapter.addAll(images);
arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, images);
((ListView)findViewById(R.id.lv_titles)).setAdapter(arrayAdapter);
// arrayAdapter.notifyDataSetChanged();
}
}
答案 2 :(得分:0)
将ArrayAdapter更改为静态字段可解决此问题。