Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6834661
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:08:19+00:00 2026-05-26T23:08:19+00:00

I have a simple list view with adapter. I create 10+ listviewitems dynamically. Then

  • 0

I have a simple list view with adapter.
I create 10+ listviewitems dynamically. Then I scroll up and down again and again and again….
I can see that available memory keeps going down…

Where do I need to free and what?
Note – there is an imageview – but in my test I have not used any images so it is View.GONE.

Also – with which tool can I profile the memory usage on the android. I have found yourKit,but how do I configure it for the android (I run the application on the device)/

The Activity class

package org.BJ.Food4All.Activities.NewRecipe;


import org.BJ.Food4All.R;
import org.BJ.Food4All.Recipe;
import org.BJ.Food4All.Recipe.Instruction;
import org.BJ.Food4All.Activities.RecipeBook.RecipeInstructionsListViewAdapter;
import org.BJ.Food4All.Activities.RecipeBook.SharedData;
import org.BJ.Food4All.utils.CameraUtil;
import org.BJ.Food4All.utils.ImageUploadItem;

import android.app.ListActivity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.EditText;

public class Instructions extends ListActivity implements OnClickListener
{
    private final static String mTAG = "Instructions";
    private EditText mInstructionEditText = null;
    private RecipeInstructionsListViewAdapter mListViewAdapter = null;
    private Recipe mEditRecipe = PrivateResources.GetRecipe();

    private CameraUtil  mCameraUtil = new CameraUtil(this);

    private int mSelectedEntryIndex = -1;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.new_recipe_instruction_tab);

        mInstructionEditText = (EditText)findViewById(R.id.newRecipeInstructionEditTextId);
        View addInstructionButton = findViewById(R.id.naddInstructionButtonId);

        // Sanity check
        if(mInstructionEditText == null || addInstructionButton == null)
        {
            Log.e(mTAG, "NULL pointers");
            // secure exit
            finish();
        }

        // Set up click listeners for all the buttons
        addInstructionButton.setOnClickListener(this);

        mListViewAdapter = new RecipeInstructionsListViewAdapter(this, R.layout.recipes_instruction_list_single_view_entry, mEditRecipe.GetInstructions());

        setListAdapter(mListViewAdapter);

        registerForContextMenu(getListView());
    }

    public void onClick(View v)
    {
        switch(v.getId())
        {
            case R.id.naddInstructionButtonId:
                AddInstructionToRecipe(v);
                break;

            default:
                Log.e(mTAG, "Invalid ID:" + v.getId());
                // secure exit
                finish();

        }
    }

    private void AddInstructionToRecipe(View v)
    {
        String instructionText = mInstructionEditText.getText().toString();

        if(instructionText == null)
        {
            return;
        }

        Instruction newInstruction = new Instruction(   mEditRecipe.GetInstructions().size() + 1,   // Index
                                                        instructionText,                            // The instruction
                                                        null,
                                                        true);

        if( mEditRecipe.AddInstruction(newInstruction) != true)
        {
            // TODO - ERROR
        }
        else
        {
            mListViewAdapter.notifyDataSetChanged();
        }
    }

    /*
     * (non-Javadoc)
     * @see android.app.Activity#onCreateContextMenu(android.view.ContextMenu, android.view.View, android.view.ContextMenu.ContextMenuInfo)
     */
    @Override
    public void onCreateContextMenu(ContextMenu menu, 
                                    View v,
                                    ContextMenuInfo menuInfo) 
    {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.instructions_ctx_menu, menu);

        super.onCreateContextMenu(menu, v, menuInfo);
    }

    /*
     * (non-Javadoc)
     * @see android.app.Activity#onContextItemSelected(android.view.MenuItem)
     */
    @Override
    public boolean onContextItemSelected(MenuItem item) 
    {
        super.onContextItemSelected(item);

        AdapterView.AdapterContextMenuInfo menuInfo;
        menuInfo = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
        mSelectedEntryIndex = menuInfo.position;

        switch(item.getItemId()) 
        {
            case R.id.deleteId:
                mEditRecipe.RemoveInstruction(mSelectedEntryIndex);
                mListViewAdapter.notifyDataSetChanged();
                return true;

            case R.id.takePictureId:
                mCameraUtil.TakePicture();
                return true;
        }

        return false;
    }

    /*
     * (non-Javadoc)
     * @see android.app.Activity#onActivityResult(int, int, android.content.Intent)
     */
    @Override
    protected void onActivityResult(int requestCode, 
                                    int resultCode, 
                                    Intent data) 
    {
//      String imageLocation = mCameraUtil.onActivityResult(requestCode, resultCode, data);
        Bitmap imageBitmap = mCameraUtil.onActivityResult(requestCode, resultCode, data);
        // TODO - switch to parameter passed in the intent!!!! like TakePicture(index);
//      mEditRecipe.GetInstructions().get( mSelectedEntryIndex ).SetBitmap( imageBitmap ); //SetInstructionImageLocation(imageLocation);
        mSelectedEntryIndex = -1;

        // Update the listviewitem with the picture
        mListViewAdapter.notifyDataSetChanged();
    }
}

The adapter class

package org.BJ.Food4All.Activities.RecipeBook;

import java.util.ArrayList;

import org.BJ.Food4All.R;
import org.BJ.Food4All.Recipe.Instruction;
import org.BJ.Food4All.utils.GlobalDefs;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Typeface;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class RecipeInstructionsListViewAdapter extends ArrayAdapter<Instruction> 
{
    private Context mContext;
    private ArrayList<Instruction> mItems;
    private LayoutInflater mInflater;

    public RecipeInstructionsListViewAdapter(Context context, int textViewResourceId, ArrayList<Instruction>items) 
    {
        super(context, textViewResourceId, items);

        mContext = context;
        mItems  = items;

        mInflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public View getView(int position, 
                        View convertView, 
                        ViewGroup parent) 
    {
          ViewHolder holder = new ViewHolder();

          if (convertView == null) 
          {
              convertView = mInflater.inflate(R.layout.recipes_instruction_list_single_view_entry, null);
          }

          if(super.getItem(position) != null)
          {
              holder.instructionIndex = (TextView) convertView.findViewById( R.id.listUp_RecipeInstructionNumberTextBoxId);
              holder.instructionText = (TextView) convertView.findViewById( R.id.listUp_RecipeInstructionTextTextBoxId);
              holder.instructionImage = (ImageView)convertView.findViewById( R.id.listUp_RecipeInstructionImageViewId);

              Typeface tf = Typeface.createFromAsset(mContext.getAssets(), "Eras_Bold.ttf");
              holder.instructionIndex.setTypeface(tf);
              holder.instructionIndex.setTextSize(30);
              holder.instructionIndex.setTextColor(GlobalDefs.GetHeadlineColor());
              holder.instructionIndex.setText( Integer.toString(mItems.get(position).getIndex()));

              tf = Typeface.createFromAsset(mContext.getAssets(), "Arial.ttf");
              holder.instructionText.setTypeface(tf);
              holder.instructionText.setTextSize(14);
              holder.instructionText.setTextColor(Color.BLACK);
              holder.instructionText.setText(mItems.get(position).getText());

              Bitmap imageBitmap = mItems.get(position).GetBitmap();
//              String imageLocation = mItems.get(position).GetInstructionImageLocation();
              if(imageBitmap != null)
              {
                  holder.instructionImage.setImageBitmap(imageBitmap);// setImageURI( Uri.parse(imageLocation ));
                  holder.instructionImage.setVisibility(View.VISIBLE);
              }
              else
              {
                  holder.instructionImage.setVisibility(View.GONE);
              }

              convertView.setTag(holder);
              convertView.setLayoutParams(new ListView.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
          } 
          else 
          {
          }

          return convertView;
    }

    @Override
    public boolean isEnabled(int position) 
    {
        return true;
    }

    static class ViewHolder 
    {
          TextView  instructionIndex;
          TextView  instructionText;
          ImageView instructionImage;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T23:08:20+00:00Added an answer on May 26, 2026 at 11:08 pm

    I’m not sure if it is correct to classify this as a bug, but every time you use Typeface.createFromAsset if creates a new font asset and does not release it. See this.

    What you can do is load the typefaces when you load your app and reference them statically. I put my typefaces in Application.

    public class YourApp extends android.app.Application {
        public void onCreate() {
            super.onCreate();
    
            // typeface caching
            initializeTypefaces();
        }
    
        public static class Fonts {
            public static Typeface THEOREM;
        }
    
        private void initializeTypefaces(){
            Fonts.THEOREM   = Typeface.createFromAsset(getAssets(), "fonts/theorem.otf");
        }
    }
    

    And then I do this in my adapter:

    textView.setTypeface(YourApp.Fonts.THEOREM);
    

    You can go here to see how to use Application in Android.

    Lastly, it looks like your still creating your ViewHolder every time instead of only when convertView is null. I would review this video to get the whole picture. http://www.google.com/events/io/2010/sessions/world-of-listview-android.html

    Here is an example of how I use the ViewHolder method:

    @Override
    public View getView(int pos, View convertView, ViewGroup parent) {
        ViewHolder holder;
    
        if(convertView == null || convertView.getTag() == null){
            convertView = inflater.inflate(R.layout.list_item, parent, false);
            holder = new ViewHolder();
    
            holder.text1  = (TextView)convertView.findViewById(R.id.list_item_text1);
            holder.text2  = (TextView)convertView.findViewById(R.id.list_item_text2);
            holder.text1.setTypeface(YourApp.Fonts.THEOREM); // only happens once when recycling!
    
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }
    
        holder.text1.setText("someText");
        holder.text2.setText("someText");
        return convertView;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently have a simple list view adapter that holds two rows of text.
I have created the simple list view with base adapter.Now I want to add
I have a simple list view with some check boxes in an alert dialog.
I have a ListView powered by a custom adapter that dynamically changes the background
I have a simple list I am using for a horizontal menu: <ul> <h1>Menu</h1>
I have a simple list like this: <ul id=cyclelist> <li>Item 1</li> <li>Item 2</li> <li>Item
I have a simple list of strings, which may be of arbitrary length. I'd
I have a simple html form. On php page. A simple list is placed
I have a simple unordered list with list items as menu item i created
I have a simple ul list. the li's contain simple a href's. I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.