【问题标题】:Binding an ImageView with a GridView将 ImageView 与 GridView 绑定
【发布时间】:2013-08-20 06:35:05
【问题描述】:

我关注了the tutorial about the GridView。我正在尝试通过适配器将 ImageView 与布局 xml 文件中的 GridView 绑定。我从 mainActivity 中捕获了 ImageView,并尝试了两种方法:通过 ImageAdapter 的构造函数输入 ImageView 或将 ImageView 设为静态。它们都返回运行时异常。

    //capturing imageView in the mainActivity

        public static ImageView IMAGE_VIEW;
            IMAGE_VIEW=(ImageView) findViewById(R.id.imageView1);



public class ImageAdapter extends BaseAdapter{
    private Context mContext;

public ImageAdapter(Context c) {
        mContext = c;
    }
 public int getCount() {
        // It should return 16 ImageViews
        return 16;
    }
.
.
.

  public View getView(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(150, 150));
                imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                imageView.setPadding(20, 20, 4, 4);
            } else {
                imageView = (ImageView) convertView;
            }


              imageView=MainActivity.IMAGE_VIEW; //I suppose here is the problem

     // the code underneath works fine for an Image File not for the ImageView     
    // imageView.setImageResource(R.drawable.crazy);


            return imageView;
        }
}

这里的错误是什么?解决办法是什么?

【问题讨论】:

    标签: android gridview imageview adapter


    【解决方案1】:

    你可以试试下面的自定义gridview教程

    http://www.androidhive.info/2012/02/android-gridview-layout-tutorial/

    http://www.mkyong.com/android/android-gridview-example/

    你会更多关于gridview的想法...

    【讨论】:

    • 谢谢,我在第二个链接中找到了解决方案。我不得不打电话给 LayoutInflater。
    【解决方案2】:

    尝试这样做。 在您的主 xml 文件中:

    <GridView
        android:id="@+id/gridView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </GridView>
    

    在描述网格项目的 xml 文件中:

    <TextView
        android:id="@+id/item_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/ic_launcher">
    </TextView>
    

    适配器:

     // references to our images
    public int[] mThumbIds = {
            R.drawable.icon_1,
            R.drawable.icon_2,
            R.drawable.icon_3,
            R.drawable.icon_4,
            R.drawable.icon_5,
            R.drawable.icon_6
    };
    

    在getView方法中:

    TextView item = (TextView) view.findViewById(R.id.item_title);
        item.setBackgroundResource(mThumbIds[position]);
    

    在活动中:

    // set grid menu
    GridView gridview = (GridView) view.findViewById(R.id.gridView1);
    YourAdapter adapter = new YourAdapter(); //put here your arguments
    gridview.setAdapter(leftMenuAdapter);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-05
      • 1970-01-01
      • 1970-01-01
      • 2013-03-27
      • 2023-04-01
      • 1970-01-01
      相关资源
      最近更新 更多