Android Gridview 可在模拟器上运行,但不能在手机上运行!

发布于 2024-11-15 23:25:22 字数 2917 浏览 8 评论 0 原文

手机和模拟器都是2.2的。

尝试使用自定义 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);

是的,我知道我对每个位置内的 imageview 没有做任何事情)。

Both phone and emulator are 2.2.

Trying to create a gridview with a custom SimpleCursorAdapter class. The gridview contains a image and text view.

The code below works perfectly on the emulator but nothing shows up when I try it on the phone.

I have noticed that only the constructor is called from the phone (not newView or bindView)! Any help?

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);
       }

    }

Here is my main xml file:

<?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"
/>

Here is my view for each grid position:

<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>

And finally here is my onCreate(well some of it):

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);

(and yes, I know I am doign nothing with the imageview inside of each position yet).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

阿楠 2024-11-22 23:25:23

请浏览以下链接,这是网格视图、列表视图等的教程 - 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 中仅添加:

Please go through the following link this is a tutorial for the grid view, listview etc- http://mobiforge.com/designing/story/understanding-user-interface-android-part-3-more-views .
Let me share my code with you:

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
            };


 }

In the AndroidManifest.xml only add: <activity android:name=".Gridview" />

暖阳 2024-11-22 23:25:23

我最终在此处采纳了赫尔曼的建议。我删除了对上下文和光标的引用。还重命名了我的布局中的 gridview 并更改了我的代码中的适当引用。

I ended up taking Herrmann's advice here. I removed the reference to the context and to the cursor. Also renamed the gridview in my layout and changed the appropriate references in my code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文