我已經實現了此示例(2.自定義適配器示例) http://www.mkyong.com/android/android-gridview-example/ 它的工作原理。Android - GridView強制更新數據更改
我當時就想「刷新」用一組新的數據,直到我這樣做,沒有工作:
imageAdapter.notifyDataSetChanged();
,然後取出在ImageAdapter getView方法如下檢查:
if (convertView == null) {
這是我目前的getView方法
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gridView;
// if (convertView == null) { // stopped my GridView from updating !!
if (true)
{
gridView = new View(context);
// get layout from mobile.xml
gridView = inflater.inflate(R.layout.mobile, null);
// set value into textview
TextView textView = (TextView) gridView
.findViewById(R.id.grid_item_label);
textView.setText(mobileValues[position]);
// set image based on selected text
ImageView imageView = (ImageView) gridView
.findViewById(R.id.grid_item_image);
imageView.setImageResource(R.drawable.square);
} else {
gridView = (View) convertView;
}
return gridView;
}
我擔心它現在正在做不必要的親一遍又一遍地重複 - 多次膨脹視圖?
我應該在每次調用這個函數時創建一個新的GridView嗎?
只是通知arraylist您在adapter構造函數中傳遞的不是整個arrayadapter。 –