2011-06-16 57 views
0

手機和模擬器都是2.2。Android Gridview在模擬器上工作,但沒有電話!

嘗試使用自定義SimpleCursorAdapter類創建gridview。 gridview包含一個圖像和文本視圖。

下面的代碼在模擬器上完美工作,但在手機上嘗試時沒有顯示出來。

我注意到,只有構造函數是從手機中調用的(不是newView或bindView)!任何幫助?

public class GridAdapter extends SimpleCursorAdapter { 
     private Context context; 
     private int mLayout; 

     public GridAdapter(Context context, int layout, Cursor c, 
      String[] from, int[] to) { 
      super(context, layout, c, from, to); 

      this.context = context; 
      mLayout = layout; 

     } 

     @Override 
     public View newView(Context context, Cursor cursor, ViewGroup parent) { 
      Cursor c = getCursor(); 

      final LayoutInflater inflater = LayoutInflater.from(context); 
      View v = inflater.inflate(mLayout, null); 

      v.setLayoutParams(new GridView.LayoutParams(150,150)); 


      return v; 

     } 

     @Override 
     public void bindView(View v, Context context, Cursor c) { 

      int nameCol = c.getColumnIndex("show_title"); 

      String name = c.getString(nameCol); 


      TextView tv = (TextView) v.findViewById(R.id.textView1); 
      if (name != null) { 

       tv.setText(name); 
      } 

      ImageView iv = (ImageView) v.findViewById(R.id.album_image); 
      iv.setScaleType(ImageView.ScaleType.CENTER_CROP); 
      iv.setImageResource(R.drawable.icon); 
     } 

    } 

這裏是我的主XML文件:

<?xml version="1.0" encoding="utf-8"?> 
<GridView xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/gridview" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:columnWidth="90dp" 
android:numColumns="auto_fit" 
android:verticalSpacing="10dp" 
android:horizontalSpacing="10dp" 
android:stretchMode="columnWidth" 
android:gravity="center" 
/> 

這裏是我的每個網格位置視野:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    
    android:id = "@+id/single_item_id" 
    android:layout_width="fill_parent" 
android:layout_height="fill_parent"  
android:orientation="vertical" 
> 
    <ImageView 
    android:id = "@+id/album_image" 
    android:adjustViewBounds="true" 
    android:layout_width = "fill_parent"    
    android:layout_height="wrap_content"/> 

    <TextView 
    android:text="TextView" 
    android:id="@+id/textView1" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"/> 

</LinearLayout> 

最後,這裏是我的onCreate(以及它的一些):

gridview = (GridView) findViewById(R.id.gridview); 
Cursor c = mDbHelper.fetchAllShows(); 
int[] to = new int[] {R.id.name}; 
GridAdapter ga = new GridAdapter(this,R.layout.icon,c,new String[] {"show_title"}, 
      to); 
gridview.setAdapter(ga); 

(是的,我知道我是doign沒有任何與每個位置內的圖像視圖)。

回答

0

我最終採取赫爾曼的意見here。我刪除了對上下文和光標的引用。也在我的佈局中重命名了gridview,並在我的代碼中更改了適當的引用。

0

請通過以下鏈接,這是一個網格視圖,列表視圖等教程 - http://mobiforge.com/designing/story/understanding-user-interface-android-part-3-more-views。 我想與大家分享我的代碼:

main.xml 
--------------- 
    <?xml version="1.0" encoding="utf-8"?> 
     <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:id="@+id/container" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"> 
      <GridView 
       android:id="@+id/gridv" 
       android:layout_width="wrap_content" 
       android:layout_height="fill_parent" 
       android:numColumns="auto_fit" 
       android:verticalSpacing="10dp" 
       android:horizontalSpacing="10dp" 
       android:columnWidth="90dp" 
       android:stretchMode="columnWidth" 
       android:gravity="center"/> 

     </FrameLayout> 

Gridview.java 
------------------------ 

     public class Gridview extends Activity { 
      private GridView gridview; 
      /** Called when the activity is first created. */ 
      @Override 
      public void onCreate(Bundle savedInstanceState) { 
       super.onCreate(savedInstanceState); 
       setContentView(R.layout.main); 
       gridview = (GridView)findViewById(R.id.gridv); 
       gridview.setAdapter(new ImageAdapter(this)); 
      } 

     } 



ImageAdapter.java 
    ---------------------- 

     public class ImageAdapter extends BaseAdapter { 
      private int imagpos; 
      private Context mContext; 

      public ImageAdapter(Context c) { 
       mContext = c; 
      } 

      public int getCount() { 
       return mThumbIds.length; 
      } 

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

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

      // create a new ImageView for each item referenced by the Adapter 
      public View getView(final int position, View convertView, ViewGroup parent) { 
       ImageView imageView; 
       if (convertView == null) { // if it's not recycled, initialize some attributes 
        imageView = new ImageView(mContext); 
        imageView.setLayoutParams(new GridView.LayoutParams(85, 85)); 
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); 
        imageView.setPadding(8, 8, 8, 8); 

        imageView.setOnClickListener(new View.OnClickListener() { 

         @Override 
         public void onClick(View view) { 
          Log.d("onClick","position ["+position+"]"); 
          imagpos= position; 
         } 

         }); 

       } else { 
        imageView = (ImageView) convertView; 
       } 

       imageView.setImageResource(mThumbIds[position]); 
       return imageView; 
      } 

      // references to our images 
      private Integer[] mThumbIds = { 
        R.drawable.imag, R.drawable.download, 
        R.drawable.images, R.drawable.imag, 
        R.drawable.images, R.drawable.download 
      }; 


} 

在AndroidManifest.xml中只添加:<activity android:name=".Gridview" />

+0

這是Android開發人員網站上的示例的公然副本!在這種情況下,他們對我沒有幫助:( – IamAlexAlright 2011-06-17 17:02:51

+0

嗨亞歷克斯請嘗試上面給出的代碼。我認爲這將解決您的問題:) – Basil 2011-06-18 04:57:23

相關問題