【发布时间】:2015-06-28 12:13:13
【问题描述】:
我为列表视图制作了自定义布局。布局有 2 个文本视图、一个按钮和一个图像视图。当我在我的列表视图上膨胀布局时它工作正常,但是当我向上和向下滑动列表视图时,我检查我的日志猫并发现内存泄漏,我不知道问题是什么:
这里是日志:
06-27 20:37:29.321: D/dalvikvm(9034): GC_CONCURRENT 释放 1749K,16% 释放 15831K/18723K,暂停 1ms+10ms
06-27 20:37:32.451: D/dalvikvm(9034): GC_CONCURRENT 释放 175K,7% 释放 20124K/21475K,暂停 2ms+18ms
06-27 20:37:35.591: D/dalvikvm(9034): GC_CONCURRENT freed 45K, 5% free 25948K/27171K, paused 2ms+12ms
我的代码:
l = (ListView) findViewById(R.id.listView1);
CustomAdapter adapter = new CustomAdapter(this,songs);
l.setAdapter(adapter);
CustomAdapter 类:
class CustomAdapter extends ArrayAdapter<String>
{
Context c;
LayoutInflater li;
List<String> names = new ArrayList<String>();
ImageView i;
TextView t1,t2;
Button b;
public CustomAdapter(Context context, List<String> resource)
{
super(context,R.id.textView1, resource);
c = context;
names = resource;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
li = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
convertView = li.inflate(R.layout.layout_listview, null);
i = (ImageView) convertView.findViewById(R.id.imageView1);
t1 = (TextView) convertView.findViewById(R.id.textView1);
t2 = (TextView) convertView.findViewById(R.id.textView2);
b = (Button) convertView.findViewById(R.id.button1);
t1.setText(songs.get(position));
t2.setText(songs.get(position));
b.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View arg0)
{
}
});
return convertView;
}
}
我用这个替换了上面的代码,但问题还是一样。
public View getView(int position, View convertView, ViewGroup parent)
{
pos = position;
Log.i("in getView",Integer.toString(pos));
//li = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
li = LayoutInflater.from(getContext());
View customView = li.inflate(R.layout.layout_listview, parent,false);
i = (ImageView) customView.findViewById(R.id.imageView_filter);
t1 = (TextView) customView.findViewById(R.id.textView1);
t2 = (TextView) customView.findViewById(R.id.textView2);
LinearLayout linearonclick = (LinearLayout) customView.findViewById(R.id.LinearlayoutOnClickListView);
b = (Button) customView.findViewById(R.id.button_eq);
t1.setText(names.get(position));
t2.setText(names.get(position));
linearonclick.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Log.i("in on Click",Integer.toString(pos));
Toast.makeText(getContext(),songs.get(pos), Toast.LENGTH_SHORT).show();
}
});
b.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
Toast.makeText(getContext(), "You selected modd Button", Toast.LENGTH_SHORT).show();
}
});
return customView;
}
【问题讨论】:
标签: android memory-leaks