0

I made a custom layout for a list view. The layout has 2 text views , one button and one image view. When i inflate the layout on my listview it works fine , but when i swipe up and down the list view , i check my log cat and find that the memory is leaking , i don't know what the problem is :

here is the logcat :

06-27 20:37:29.321: D/dalvikvm(9034): GC_CONCURRENT freed 1749K, 16% free 15831K/18723K, paused 1ms+10ms

06-27 20:37:32.451: D/dalvikvm(9034): GC_CONCURRENT freed 175K, 7% free 20124K/21475K, paused 2ms+18ms

06-27 20:37:35.591: D/dalvikvm(9034): GC_CONCURRENT freed 45K, 5% free 25948K/27171K, paused 2ms+12ms

my code :

l = (ListView) findViewById(R.id.listView1);
    CustomAdapter adapter = new CustomAdapter(this,songs);
    l.setAdapter(adapter);

CustomAdapter class :

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;
    }

}

I replaced the above code with this , but the problem is still the same .

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;
    }
Ronit Jain
  • 45
  • 2
  • 9
  • Look at http://stackoverflow.com/questions/11387269/listview-gets-out-of-memory-exception-but-with-no-memory-leaks – ditkin Jul 01 '15 at 19:49

1 Answers1

0

In the getView method you want to only inflate a new convertView if it is not null. First check to see that it is not null and if it is the correct type then use it.

From the Adapter getView documentation:

convertView: The old view to reuse, if possible. Note: You should check that this view is non-null and of an appropriate type before using. If it is not possible to convert this view to display the correct data, this method can create a new view. Heterogeneous lists can specify their number of view types, so that this View is always of the right type (see getViewTypeCount() and getItemViewType(int)).

ditkin
  • 6,774
  • 1
  • 35
  • 37