2012-07-05 91 views
1

我想顯示圖像使用GridView。這是我第一次使用GridView,因此我使用here的示例並將其實現爲我的(我嘗試了包含在其中的示例,它的工作原理)。GridView不顯示內容android

但是,我檢查了很多次,沒有錯誤來自LogCat,沒有強制關閉,圖像沒有顯示。我不知道哪裏是錯誤的部分。

這裏是我的代碼:

choosepic.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" 
    android:background="@drawable/bg_inner"> 

     <ImageView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:src="@drawable/book_inner" 
      android:layout_marginTop="50dp" 
     /> 

     <ImageButton 
      android:id="@+id/homeBtn" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:src="@drawable/home_btn" 
      android:background="@null" 
     /> 

     <ImageView 
      android:layout_height="wrap_content" 
      android:layout_width="wrap_content" 
      android:src="@drawable/bg_arrow_btn" 
      android:layout_alignParentRight="true"  
     /> 

     <ImageButton 
      android:id="@+id/nextBtn" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:src="@drawable/right_arrow" 
      android:background="@null" 
      android:layout_alignParentRight="true" 
      android:layout_marginTop="5dp" 
      android:layout_marginRight="7dp" 
      android:layout_marginLeft="7dp" 
     /> 

     <ImageButton 
      android:id="@+id/prevBtn" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:src="@drawable/left_arrow" 
      android:background="@null" 
      android:layout_toLeftOf="@+id/nextBtn" 
      android:layout_marginTop="5dp" 
     /> 

     <GridView 
      android:id="@+id/gridView1" 
      android:numColumns="3" 
      android:gravity="center" 
      android:columnWidth="30dp" 
      android:stretchMode="columnWidth" 
      android:layout_width="300dp" 
      android:layout_height="200dp" 
      android:layout_marginLeft="60dp" 
      android:layout_marginTop="70dp" 
     > 

     </GridView> 
</RelativeLayout> 

animalbutton.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" > 

<ImageView 
    android:id="@+id/grid_item_image" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:src="@drawable/ic_launcher" 
    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true"> 
</ImageView> 
<TextView 
    android:text="TextView" 
    android:layout_height="wrap_content" 
    android:id="@+id/textView1" 
    android:layout_width="wrap_content" 
    android:layout_centerHorizontal="true" 
    android:textSize="18sp" 
    android:visibility="gone">  
</TextView> 

ImageAdapter.java

 public class ImageAdapter extends BaseAdapter{ 

    private Activity activity; 
    private ArrayList<String> listNm; 
    private ArrayList<Integer> listAnim; 

    public ImageAdapter(Activity activity,ArrayList<String> listName, ArrayList<Integer> listImage) { 
     super(); 
     this.listNm = listName; 
     this.listAnim = listImage; 
     this.activity = activity; 
    } 

    public static class ViewHolder 
    { 
     public ImageView imgViewAnim; 
     public TextView txtViewAnim; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     ViewHolder view; 
     LayoutInflater inflator = activity.getLayoutInflater(); 

     if(convertView==null) 
     { 
      view = new ViewHolder(); 
      convertView = inflator.inflate(R.layout.animalbutton, null); 

      view.txtViewAnim = (TextView) convertView.findViewById(R.id.textView1); 
      view.imgViewAnim = (ImageView) convertView.findViewById(R.id.grid_item_image); 

      convertView.setTag(view); 
     } 
     else 
     { 
      view = (ViewHolder) convertView.getTag(); 
     } 

     view.txtViewAnim.setText(listNm.get(position)); 
     view.imgViewAnim.setImageResource(listAnim.get(position)); 

     return convertView; 
    } 


    @Override 
    public int getCount() { 
     // TODO Auto-generated method stub 
     return 0; 
    } 

    @Override 
    public String getItem(int position) { 
     // TODO Auto-generated method stub 
     return listNm.get(position); 
    } 

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

choosepic.java

 public class choosepic extends Activity { 
    /** Called when the activity is first created. */ 

    ImageAdapter mAdapter; 
    GridView gridView; 
    static final String[] animal = new String[] { 
     "cat", "cow","croc", "duck", "elephant", "giraffe", "lion", "moose", "mouse"}; 

    private ArrayList<String> listNm; 
    private ArrayList<Integer> listAnim; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.choosepic); 


     gridView = (GridView) findViewById(R.id.gridView1); 
     prepare_list1(); 
     mAdapter = new ImageAdapter(this,listNm, listAnim); 
     gridView.setAdapter(mAdapter); 

     gridView.setOnItemClickListener(new OnItemClickListener() { 

      @Override 
      public void onItemClick(AdapterView<?> arg0, View v, int position, long id) { 
       Toast.makeText(getApplicationContext(), mAdapter.getItem(position), Toast.LENGTH_SHORT).show(); 

      } 
     }); 
    } 

    public void prepare_list1(){ 
     listNm = new ArrayList<String>(); 
     listAnim = new ArrayList<Integer>(); 
     for (int i = 0; i < animal.length; i++) { 
      listNm.add(animal[i]); 
      listAnim.add(getResources().getIdentifier("anim_"+animal[i], "drawable", getPackageName())); 

     } 
    } 
} 

我需要一些幫助。我感謝任何幫助。先謝謝你!

+0

你參考其中U給了它是正確的,但你的執行是錯誤的鏈接。 – Harish

+0

對不起,我有2個鏈接。我已更新正確的鏈接。 – user1008497

+0

你增加了資源嗎? – Harish

回答

3

我認爲這個問題是在你的getCount()返回0元素 ,而不是使它像這樣

@Override 
    public int getCount() { 
     // TODO Auto-generated method stub 
     return listNm.size(); 
    } 
+0

非常感謝你的幫助。你解決了我的問題。 – user1008497