2 hunanly hunanly 于 2015.07.20 16:34 提问

不停的用simpleadapter加载更新gridview导致内存溢出,怎么解决

public class Myragment extends Fragment {
private GridView grid;
ArrayList> arraylist = null;
int[] temp = new int[] {R.id.cell};
String[] temp1 = new String[] { "cell" };
MyHandler handler = new MyHandler(getActivity()){
@Override

public void handleMessage(Message msg) {

if(msg.what == 1){
InitData();
SimpleAdapter sa = new SimpleAdapter(getActivity(), arraylist, R.layout.subcpc,
temp1, temp);
grid.setAdapter(sa);
}
}
};
private Timer mTimer = new Timer(true);

private TimerTask mTimerTask = new TimerTask() {
@Override
public void run() {
Message message = new Message();
message.what = 1;
handler.sendMessage(message);
}
};

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.cpc, container, false);

grid = (GridView) v.findViewById(R.id.gridView_cpc);

int colnum = (int) (((getResources().getDisplayMetrics().widthPixels)) / 4);
grid.setColumnWidth(colnum);
mTimer.schedule(mTimerTask, 500,1000);

return v;
}

@Override
public void onStop() {
super.onStop();
while(!mTimerTask.cancel()){
mTimer.cancel();
}
}
public void InitData(){
double[] curdata = ((RunActivity)getActivity()).getCPCVI();
arraylist = new ArrayList>();
for(int i=1;i DecimalFormat dcmFmt = new DecimalFormat("0.00");
HashMap map = new HashMap();
map.put("cell", "#"+i);
arraylist.add(map);
map = new HashMap();
map.put("cell", dcmFmt.format(curdata[2*i-2])); //2(i-1)+0
arraylist.add(map);
map = new HashMap();
map.put("cell", dcmFmt.format(curdata[2*i-1]));
arraylist.add(map);
map = new HashMap();
map.put("cell", dcmFmt.format((curdata[2*i-2]*curdata[2*i-1])));
arraylist.add(map);
}
}
static class MyHandler extends Handler {
WeakReference mActivityReference;

MyHandler(Activity activity) {
mActivityReference= new WeakReference(activity);
}
}
}

程序产生内存溢出,详见图片图片说明

各位大牛提供解决方案

1个回答

bdmh
bdmh   Ds   Rxr 2015.07.20 19:53
已采纳

用viewholder解决listview和gridview之类的数据加载,要重用他们的item

Csdn user default icon
上传中...
上传图片
插入图片