GridView控件不显示在Android的内容 [英] GridView doesn't display content in android

查看:498
本文介绍了GridView控件不显示在Android的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用GridView控件来显示图像。这是我使用的GridView的第一次,所以我用从这里和实施的例子它煤矿(我曾经尝试包含有这个例子,它的作品)。

i am trying to display image using GridView. This is the first time i using GridView, so i using the example from here and implement it to mine (i have tried the example that contained there, and it's works).

不过,我已经检查了很多时间,也没有错误来自LogCat中,没有强制关闭,图像没有显示。我不知道哪来的错误部分。

But, i have checked it many time, there's no error comes from LogCat, no Force Closed, the image didn't show. i have no idea where's the wrong part.

下面是我的code:

choosepic.xml

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

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

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

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

        }
    }
}

我需要一些帮助。我AP preciate任何帮助。预先感谢您!

i need some help. i appreciate any help. thank you in advance!

推荐答案

我认为这个问题是在你的 getCount将(),返回0元
而不是使它像这样

i think the problem is in your getCount() that returns 0 elements instead of that make it like this

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

这篇关于GridView控件不显示在Android的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆