2013-11-21 16 views
0

我是android.I的新手,在List Adapter中使用HolderView()方法做了一個Listview,即使這樣我的內存異常仍然存在。可以告訴我哪裏有我做錯了。在Android中滾動Listview導致內存異常

代碼

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

您可能想檢查[** this **](http://stackoverflow.com/questions/13618345/avoiding-out-of-memory-error-in-loading-bitmap-in-listview) – swiftBoy

回答

0

,因爲你每次充氣時getView()調用時您的項目佈局這可能是發生了。

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

你不應該這樣做,在你的else聲明,而不是你應該使用指定的convertView並對其進行修改。

0

你在所有情況下都在膨脹視圖,你不應該那樣做。 VieHolders通常用於減少視圖層次結構查找時間。

看看在執行這一答案Onclicklistner not working in fragment listview

做這樣的事情

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

使用RecyclerView而不是ListView和畢加索庫加載圖像。