0

I'm new to android.I did a Listview using HolderView() method in the List Adaptor,even then i'm getting out of memory exception.Can any one tell where i'm doing wrong.

Code

public class CustomView extends Activity {
    private static final String AUDIO_RECORDER_FOLDER = "AudioRecorder";
     ListView listview;
     private List<String> myList;
     private List<String> myDuration;
     private List<String> myListPath;

    @SuppressLint("SimpleDateFormat")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
                myList = new ArrayList<String>();   
                myDuration = new ArrayList<String>();
                myListPath = new ArrayList<String>();
               //Fetching Deatil's from a folder from a SDCard
               String filepath = Environment.getExternalStorageDirectory().getPath();

        File file = new File(filepath, AUDIO_RECORDER_FOLDER);

        File list[] = file.listFiles();
        String filePath = file.getAbsolutePath(); 
        String fileName;
        String absolutePath;
        Date date;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // Set your date format;
        String currentFileName;
        for( int i=0; i< list.length; i++)
        {
            fileName = list[i].getName();
            absolutePath = filePath+"/"+fileName;
            myListPath.add(absolutePath);
            File f = new File(absolutePath);
            MediaPlayer mp = new MediaPlayer();
            FileInputStream fs;
            FileDescriptor fd;
            int duration = 0;
            try {
                fs = new FileInputStream(f);
                fd = fs.getFD();
                mp.setDataSource(fd);
                mp.prepare(); // might be optional

                duration= mp.getDuration();
                mp.release();
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }


            String file1[] =fileName.split(".mp3");
            long mydate = Long.parseLong(file1[0]);
            date = new Date(mydate)  ;
            currentFileName = sdf.format(date);
            currentFileName+=".mp3";
            myList.add(currentFileName);
            StringBuffer buf = new StringBuffer();
            int a = (int) TimeUnit.MILLISECONDS.toMinutes(duration);
            int b = (int) (TimeUnit.MILLISECONDS.toSeconds(duration) -  TimeUnit.MINUTES.toSeconds(a));
            buf.append(String.format("%02d", a));
            buf.append(":");
            buf.append(String.format("%02d", b));
            myDuration.add(buf.toString());
        }

        setContentView(R.layout.activity_list_view);
        listview = (ListView)findViewById(R.id.list);
        listview.setAdapter(new dataListAdapter(myList,myDuration,img));
        setButtonHandlers(); 

    }

    private void setButtonHandlers() {
        ((ImageView) findViewById(R.id.backBtn)).setOnClickListener(btnClick);
    }
    private View.OnClickListener btnClick = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
        //sIntent i = getIntent();
        finishFromChild(getParent());
        }
        };
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_list_view, menu);
                return true;
    }
      class dataListAdapter extends BaseAdapter {
            List<String> Title, Detail;

            dataListAdapter() {
                Title = null;
                Detail = null;
            }

            public dataListAdapter(List<String> text,List<String> text1,int text3) {
                Title = text;
                Detail = text1;

            }

            public int getCount() {
                // TODO Auto-generated method stub
                return Title.size();
            }

            public Object getItem(int arg0) {
                // TODO Auto-generated method stub
                return null;
            }

            public long getItemId(int position) {
                // TODO Auto-generated method stub
                return position;
            }

            public View getView(final int position, View convertView, ViewGroup parent) {
                final Holder mHolder;
                LayoutInflater inflater;
                TextView title, detail;
                ImageView img;
                inflater = getLayoutInflater();
                mHolder = new Holder();
                mHolder.id = position;

                if(convertView == null) {
                    convertView = inflater.inflate(R.layout.list, parent, false);
                    title = (TextView) convertView.findViewById(R.id.name);
                    detail = (TextView) convertView.findViewById(R.id.duration);
                    mHolder.name = (TextView) convertView.findViewById(R.id.name);
                    mHolder.duration = (TextView) convertView.findViewById(R.id.duration);

                    //On Click Listener to play file when File Name is clicked
                    convertView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            // TODO Auto-generated method stub
                            String path = myListPath.get(position);
                            Intent intent = new Intent();  
                            intent.setAction(android.content.Intent.ACTION_VIEW);  
                            File file = new File(path);  
                            intent.setDataAndType(Uri.fromFile(file), "audio/*");  
                            startActivity(intent);

                        }
                    });
                    mHolder.name.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            // TODO Auto-generated method stub
                            String path = myListPath.get(position);
                            Intent intent = new Intent();  
                            intent.setAction(android.content.Intent.ACTION_VIEW);  
                            File file = new File(path);  
                            intent.setDataAndType(Uri.fromFile(file), "audio/*");  
                            startActivity(intent);

                        }
                    });
                    //On Click Listener to play file when  Duration of the File is clicked
                    mHolder.duration.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            // TODO Auto-generated method stub
                            String path = myListPath.get(position);
                            Intent intent = new Intent();  
                            intent.setAction(android.content.Intent.ACTION_VIEW);  
                            File file = new File(path);  
                            intent.setDataAndType(Uri.fromFile(file), "audio/*");  
                            startActivity(intent);

                        }
                    });
                    title.setText(Title.get(position).toString());
                    detail.setText(Detail.get(position).toString());

                    convertView.setTag(mHolder);
                }else{
                    convertView = inflater.inflate(R.layout.list, parent, false);
                    title = (TextView) convertView.findViewById(R.id.name);
                    detail = (TextView) convertView.findViewById(R.id.duration);
                    mHolder.name = (TextView) convertView.findViewById(R.id.name);
                    mHolder.duration = (TextView) convertView.findViewById(R.id.duration);

                    //On Click Listener to play file when File Name is clicked
                    convertView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            String path = myListPath.get(position);
                            Intent intent = new Intent();  
                            intent.setAction(android.content.Intent.ACTION_VIEW);  
                            File file = new File(path);  
                            intent.setDataAndType(Uri.fromFile(file), "audio/*");  
                            startActivity(intent);
                        }
                    });
                    mHolder.name.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            String path = myListPath.get(position);
                            Intent intent = new Intent();  
                            intent.setAction(android.content.Intent.ACTION_VIEW);  
                            File file = new File(path);  
                            intent.setDataAndType(Uri.fromFile(file), "audio/*");  
                            startActivity(intent);
                        }
                    });
                    //On Click Listener to play file when  Duration of the File is clicked
                    mHolder.duration.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            String path = myListPath.get(position);
                            Intent intent = new Intent();  
                            intent.setAction(android.content.Intent.ACTION_VIEW);  
                            File file = new File(path);  
                            intent.setDataAndType(Uri.fromFile(file), "audio/*");  
                            startActivity(intent);
                        }
                    });

                    title.setText(Title.get(position).toString());
                    detail.setText(Detail.get(position).toString());
                    convertView.setTag(mHolder);
                }
                return (convertView);
            }
        }
      class Holder {
            int id;
            TextView  name;
            TextView duration;
        }
}
1
  • You may like to check this Commented Nov 21, 2013 at 12:36

3 Answers 3

0

This maybe happens because you inflate the layout of your items every time getView() is called.

if(convertView == null) {
    convertView = inflater.inflate(R.layout.list, parent, false);
    ...
}else{
    convertView = inflater.inflate(R.layout.list, parent, false);
    ...
}

You shouldn't do that in your else statement, instead you should use the given convertView and modify it.

Sign up to request clarification or add additional context in comments.

Comments

0

You are inflating a view in all cases, you should not do that. VieHolders are generally used to reduce the view hierarchy lookup time.

Take a look at the implementation in this answer Onclicklistner not working in fragment listview

do something like this

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;
    RecipesHolder holder = null;
    //recycling views
    if(null == row){
        row = inflater.inflate(layoutResourceId, parent, false);
        holder = new RecipesHolder();
        holder.imgIcon = (ImageView) row.findViewById(R.id.imageView1);
        holder.txtTitle = (TextView) row.findViewById(R.id.title);
        holder.category = (TextView) row.findViewById(R.id.category);
        holder.source = (TextView) row.findViewById(R.id.source);
        holder.country = (TextView) row.findViewById(R.id.country);
        holder.readytime = (TextView) row.findViewById(R.id.readytime);
        holder.tips = (Button) row.findViewById(R.id.tips);
        holder.fav = (Button) row.findViewById(R.id.fav);
        row.setTag(holder);
    }else{
        holder = (RecipesHolder)row.getTag();
    }
    Recipes ap = data[position];

    imageLoader.DisplayImage(ap.getIMAGENAME240(), holder.imgIcon);
    holder.txtTitle.setText(ap.getNAME());
    holder.category.setText(ap.getCATEGORY());
    holder.source.setText(ap.getSOURCE());
    holder.country.setText(ap.getCOUNTRY());
    holder.readytime.setText(ap.getREADYTIME());

    return row;
}

Comments

0

Use RecyclerView instead of ListView and Picasso library to load images.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.