2011-09-05 47 views
0

我正在實施一個數據庫,其中每個項目(工廠)都有一個圖像庫。對於數據庫中的每個工廠,我創建一個文件夾來存儲圖像。我看不到我的圖庫小部件,沒有圖片顯示

當我查看一個項目時,我希望它的詳細信息與圖像庫一起顯示,但是圖庫所在的部分是空白的。

我不知道如果我的XML或代碼是難辭其咎所以這裏是XML ...

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"> 

     <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:stretchColumns="*"> 

       ...Content Removed... 

     </TableLayout> 

     <Gallery xmlns:android="http://schemas.android.com/apk/res/android" 
       android:id="@+id/plant_gallery" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" /> 

    </LinearLayout>  
</ScrollView> 

...和...代碼

public class Plant extends Activity 
{ 
    private PlantDatabase pDatabase; 
    Cursor plantC; 

    private boolean mExternalStorageAvailable = false; 
    private boolean mExternalStorageWriteable = false; 
    private String state; 
    File plantDir; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.plant); 

     Bundle b = getIntent().getExtras(); 
     String plantID = b.getString("id"); 

     state = Environment.getExternalStorageState(); 
     if (Environment.MEDIA_MOUNTED.equals(state)) 
     { 
      // We can read and write 
      mExternalStorageAvailable = mExternalStorageWriteable = true; 
     } 
     else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) 
     { 
      // We can only read 
      mExternalStorageAvailable = true; 
      mExternalStorageWriteable = false; 
     } 
     else 
     { 
      // We have no access 
      mExternalStorageAvailable = mExternalStorageWriteable = false; 
     } 

     if (mExternalStorageAvailable) 
     { 
      plantDir = new File(Environment.getExternalStorageDirectory() + "/PlantLog/" + plantID); 

      if (!plantDir.exists()) 
      { 
       if (mExternalStorageWriteable) 
       { 
        if (!plantDir.mkdirs()) 
        { 
         Toast.makeText(this, "Unable to make image directory for current plant", Toast.LENGTH_LONG).show(); 
        } 
       } 
      } 

      plantDir = new File(Environment.getExternalStorageDirectory() + "/PlantLog/" + plantID); 

      if (plantDir.exists()) 
      { 
       Gallery g = (Gallery)findViewById(R.id.plant_gallery); 
       g.setAdapter(new ImageAdapter(this, getImageList(plantID))); 

       g.setOnItemClickListener(new OnItemClickListener() 
       { 
        public void onItemClick(AdapterView<?> parent, View v, 
              int position, long id) 
        { 

        } 
       }); 
      } 
     } 

     pDatabase = new PlantDatabase(this); 
     plantC = pDatabase.getPlant(plantID); 

     plantC.moveToFirst(); 

     TextView t; 

     // Display results 
     t = (TextView)findViewById(R.id.plant_dbid); 
     t.setText(plantC.getString(0)); 

     t = (TextView)findViewById(R.id.plant_plantid); 
     t.setText(plantC.getString(1)); 

     t = (TextView)findViewById(R.id.plant_commonname); 
     t.setText(plantC.getString(2)); 

     t = (TextView)findViewById(R.id.plant_family); 
     t.setText(plantC.getString(3)); 

     t = (TextView)findViewById(R.id.plant_genus); 
     t.setText(plantC.getString(4)); 

     t = (TextView)findViewById(R.id.plant_species); 
     t.setText(plantC.getString(5)); 

     t = (TextView)findViewById(R.id.plant_variety); 
     t.setText(plantC.getString(6)); 

     t = (TextView)findViewById(R.id.plant_form); 
     t.setText(plantC.getString(7)); 

     t = (TextView)findViewById(R.id.plant_cultivar); 
     t.setText(plantC.getString(8)); 

     t = (TextView)findViewById(R.id.plant_synonyms); 
     t.setText(plantC.getString(9)); 

     t = (TextView)findViewById(R.id.plant_flowercolour); 
     t.setText(plantC.getString(10)); 

     t = (TextView)findViewById(R.id.plant_datesown); 
     t.setText(plantC.getString(11)); 

     t = (TextView)findViewById(R.id.plant_dateacquired); 
     t.setText(plantC.getString(12)); 

     t = (TextView)findViewById(R.id.plant_pricepaid); 
     t.setText(plantC.getString(13)); 

     t = (TextView)findViewById(R.id.plant_growingnotes); 
     t.setText(plantC.getString(14)); 
    } 

    private List<String> getImageList(String dir) 
    { 
     List<String> tFileList = new ArrayList<String>(); 

     File f = new File(Environment.getExternalStorageDirectory() + "/PlantLog/" + dir);  
     File[] files = f.listFiles(); 

     for (int i = 0; i < files.length; i++) 
     { 
      File file = files[i]; 

      // Check file is jpg image 
      if (file.getPath().endsWith(".jpg")) 
      { 
       tFileList.add(file.getPath()); 
      } 
     } 

     return tFileList; 
    } 

    public class ImageAdapter extends BaseAdapter 
    { 
     private Context mContext; 
     private List<String> fileList; 
     private ImageView i; 
     private int mGalleryItemBackground; 

     public ImageAdapter(Context c, List<String> fList) 
     { 
      mContext = c; 
      fileList = fList; 
      i = new ImageView(mContext); 

      TypedArray a = obtainStyledAttributes(R.styleable.Gallery1); 
      mGalleryItemBackground = a.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 0); 
      a.recycle(); 
     } 

     public int getCount() 
     { 
      return fileList.size(); 
     } 

     public Object getItem(int position) 
     { 
      return position; 
     } 

     public long getItemId(int position) 
     { 
      return position; 
     } 

     public View getView(int position, View convertView, 
          ViewGroup parent) 
     { 
      System.gc(); 

      if (convertView == null) 
      { 
       try 
       { 
        Bitmap bm = BitmapFactory.decodeFile(fileList.get(position).toString()); 

        i.setImageBitmap(bm); 
        i.setLayoutParams(new Gallery.LayoutParams(150, 100)); 
        i.setScaleType(ImageView.ScaleType.FIT_XY); 
        i.setBackgroundResource(mGalleryItemBackground); 
       } 
       catch (Exception e) 
       { 
        // Nothing to see here 
       } 
      } 

      return i; 
     } 
    } 
} 

對不起這是如此之久,但希望我已經包含了足夠的。畫廊主要基於畫廊的例子,但調整,以匹配我的代碼。如果任何人有任何想法爲什麼圖像不顯示,我將不勝感激幫助!

謝謝!

回答

1

默認的LinearLayout方向是水平:)

(編輯:我的意思是,檢查它是不是把你的活動RHS)

+0

謝謝你,所有的固定! –