2015-06-03 58 views
-1

我的應用程序能夠拍照並保存並顯示在列表中。它們也可以被刪除並且列表被正確更新。 但是,如果關閉應用程序然後再次啓動它,則列表不會更正確。詳細介紹:在onCreate中加載圖像無法正確填充適配器

這些都是在適配器列表中的元素我拿兩張照片後:

List[0] 20150603_042556 
List[1] 20150603_042601 

這些照片被正確地保存。不過,如果我關閉並重新打開該應用程序,這是發生了什麼:

Loaded selfie:﹕ 20150603_042556 
List[0] 20150603_042556   
Loaded selfie: 20150603_042601 
List[0] 20150603_042601   
List[1] 20150603_042601 

這是附加功能:

public void add(SelfieRecord listItem) { 
    list.add(listItem); 
    for(int k=0;k<list.size();k++) 
     Log.i("List: ", String.valueOf(k) + " " + list.get(k).getPicName()); 
    notifyDataSetChanged(); 
} 

這是我如何加載保存的圖片:

for (int ii = 0; ii < dirFiles.length; ii++) { 
    File file = dirFiles[ii]; 
    String path = file.getAbsolutePath(); 
    selfie.setPic(path); 
    selfie.setPicName(path.substring(path.indexOf("_") + 1, path.lastIndexOf("_"))); 
    Log.i(TAG+" Loaded selfie: ",path); 
    mAdapter.add(selfie); 
} 

無法弄清楚會發生什麼。

編輯:更多代碼如下。

在主要活動:

protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     // setup notifications 
     setNotifications(); 

     mListView = (ListView)findViewById(R.id.selfieList); 
     mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
       Intent intent = new Intent(MainActivity.this, ViewActivity.class); 
       SelfieRecord record = (SelfieRecord) mAdapter.getItem(position); 
       intent.putExtra(SELFIE_KEY, record.getPic()); 
       startActivity(intent); 
      } 
     }); 

     mAdapter = new SelfieAdapter(getApplicationContext()); 
     mListView.setAdapter(mAdapter); 

     // load selfies 
     if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()) && mAdapter.getCount()==0) { 
      // gets the files in the directory 
      File fileDirectory = new File(getExternalFilesDir(Environment.DIRECTORY_PICTURES).getAbsolutePath()); 
      if (!fileDirectory.exists()) { 
       fileDirectory.mkdirs(); 
      } 
      // lists all the files into an array 
      File[] dirFiles = fileDirectory.listFiles(); 

      if (dirFiles.length != 0) { 
       SelfieRecord selfie = new SelfieRecord(); 
       // loops through the array of files, outputing the name to console 
       for (int ii = 0; ii < dirFiles.length; ii++) { 
        File file = dirFiles[ii]; 
        String path = file.getAbsolutePath(); 
        selfie.setPic(path); 
        selfie.setPicName(path.substring(path.indexOf("_") + 1, path.lastIndexOf("_"))); 
        Log.i(TAG+" Loaded selfie: ",path); 
        mAdapter.add(selfie); 
       } 
      } 
     } 

     registerForContextMenu(mListView); 

    } 

在適配器:

@Override 
    public View getView(int position, View convertView, ViewGroup parent) { 

     View newView = convertView; 
     ViewHolder holder; 

     SelfieRecord curr = list.get(position); 
     Log.i("List: ", String.valueOf(position)+" "+list.get(position).getPicName()); 

     if (null == convertView) { 
      holder = new ViewHolder(); 
      newView = inflater.inflate(R.layout.list_record, parent, false); 
      holder.pic = (ImageView) newView.findViewById(R.id.pic); 
      holder.name = (TextView) newView.findViewById(R.id.pic_name); 
      newView.setTag(holder); 

     } else { 
      holder = (ViewHolder) newView.getTag(); 
     } 

     //setPic(holder.pic, curr.getPic()); 
     holder.name.setText(curr.getPicName()); 
     mImageView = holder.pic; 
     new BitmapLoader().execute(curr.getPic()); 

     Log.i("Loader: ", String.valueOf(position) + " " + curr.getPicName()); 

     return newView; 
    } 

    static class ViewHolder { 

     ImageView pic; 
     TextView name; 

    } 

    public void add(SelfieRecord listItem) { 
     list.add(listItem); 
     for(int k=0;k<list.size();k++) 
      Log.i("List: ", String.valueOf(k) + " " + list.get(k).getPicName()); 
     notifyDataSetChanged(); 
    } 

    public void remove(int position) { 
     list.remove(position); 
     notifyDataSetChanged(); 
    } 

位圖裝載機:

public class BitmapLoader extends AsyncTask<String,Integer,Bitmap> { 

     @Override 
     protected Bitmap doInBackground(String... resId) { 

      // Get the dimensions of the View 
      int targetW = Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 160, mContext.getResources().getDisplayMetrics())); 
      int targetH = Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 120, mContext.getResources().getDisplayMetrics())); 

      // Get the dimensions of the bitmap 
      BitmapFactory.Options bmOptions = new BitmapFactory.Options(); 
      bmOptions.inJustDecodeBounds = true; 
      BitmapFactory.decodeFile(resId[0], bmOptions); 
      int photoW = bmOptions.outWidth; 
      int photoH = bmOptions.outHeight; 

      // Determine how much to scale down the image 
      int scaleFactor = Math.min(photoW/targetW, photoH/targetH); 

      // Decode the image file into a Bitmap sized to fill the View 
      bmOptions.inJustDecodeBounds = false; 
      bmOptions.inSampleSize = scaleFactor; 
      bmOptions.inPurgeable = true; 

      Bitmap bitmap = BitmapFactory.decodeFile(resId[0], bmOptions); 

      return bitmap; 

     } 

     @Override 
     protected void onPostExecute(Bitmap result) { 
      mImageView.setImageBitmap(result); 
     } 

    } 

我相信問題是,當我把元素適配器從存儲中回收後:

Loaded selfie:﹕ 20150603_042556 
List[0] 20150603_042556   
Loaded selfie: 20150603_042601 
List[0] 20150603_042601   
List[1] 20150603_042601 

第二次調用適配器的add方法會覆蓋第一個元素。爲什麼?

+0

我在這裏也沒有看到任何問題。你可以添加更多的代碼? –

+0

添加了更多代碼。 – Sharaazad

+0

在帖子結尾處更好地解決了這個問題。 – Sharaazad

回答

0

解決了在創建改變for循環:

if (dirFiles.length != 0) { 
       // loops through the array of files, outputing the name to console 
       for (int ii = 0; ii < dirFiles.length; ii++) { 
        File file = dirFiles[ii]; 
        String path = file.getAbsolutePath(); 
        SelfieRecord selfie = new SelfieRecord(); 
        selfie.setPic(path); 
        selfie.setPicName(path.substring(path.indexOf("_") + 1, path.lastIndexOf("_"))); 
        Log.i(TAG+" Loaded selfie: ",path); 
        mAdapter.add(selfie); 
       } 
      } 

必須通過不同的SelfieRecord對象的add方法。

相關問題