Android GridView不显示带有文本和图像的项目onclick

时间:2013-11-05 05:52:28

标签: android gridview onclick

我是新来的,我对我的Android项目感到困惑。我做了一些研究,但我仍然没有答案。我的问题是:当我点击GridView中的项目时,它显示我只是黑屏。当我改变了一些东西时,现在它在Eclipse中显示了一个错误。我不知道我的错误在哪里。

我使用了这个答案Gridview with two columns and auto resized images来使我的网格视图更好,适合多个屏幕。

我使用了这个内容:I want the Images which are on the gridView should show in full View

我的代码:FirstActivity:

public class FirstActivity extends SherlockActivity {

    ActionBar actionbar;
    List<Item> items = new ArrayList<Item>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setTheme(R.style.Theme_Sherlock);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.first);
        actionbar = getSupportActionBar();

        items.add(new Item("Name1", "Text1", R.drawable.pic_12));
        items.add(new Item("Name2", "Text2", R.drawable.pic_13));
        items.add(new Item("Name3", "Text3", R.drawable.pic_14));
        items.add(new Item("Name4", "Text4", R.drawable.pic_15));
        items.add(new Item("Name5", "Text5", R.drawable.pic_2));

        GridView gridView = (GridView) findViewById(R.id.gridview);
        gridView.setAdapter(new ImageAdapter(this, items));

        gridView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View v,
                    int position, long id) {
                // Sending image id to SecondActivity
                Intent i = new Intent(getApplicationContext(), SecondActivity.class);
                // passing index (position of clicked item)
                i.putExtra("id", position);
                startActivity(i);
            }
        });

    }

我的ImageAdapter类:

public class ImageAdapter extends BaseAdapter {


    private List<Item> items = new ArrayList<Item>();
    private LayoutInflater inflater;

    public ImageAdapter(Context context, List<Item> items) {
        inflater = LayoutInflater.from(context);
        this.items = items;
    }

    @Override
    public int getCount() {
        return items.size();
    }

    @Override
    public Object getItem(int i) {
        return items.get(i);
    }

    @Override
    public long getItemId(int i) {
        return items.get(i).drawable;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        View v = view;
        ImageView picture;
        TextView name;

        if (v == null) {
            v = inflater.inflate(R.layout.squareimageview, viewGroup, false);
            v.setTag(R.id.picture, v.findViewById(R.id.picture));
            v.setTag(R.id.text, v.findViewById(R.id.text));
        }

        picture = (ImageView) v.getTag(R.id.picture);
        name = (TextView) v.getTag(R.id.text);

        Item item = (Item) items.get(i);

        picture.setImageResource(item.getDrawable());
        name.setText(item.name);

        return v;
    }

}

SecondActivity:

 public class SecondActivity extends Activity {

        List<Item> items = new ArrayList<Item>();

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.second);

            // get intent data
            Intent i = getIntent();
            // Selected image id
            int position = i.getExtras().getInt("id");

            TextView name = (TextView) findViewById(R.id.text_small);
            ImageView imageView = (ImageView) findViewById(R.id.picture);
            TextView text = (TextView) findViewById(R.id.text_big);

            Item item = (Item) items.get(position);



      //***************HERE ARE MY ERRORS***************************************
            name.setText(items.name);
            text.setText(items.text);
            imageView.setImageResource(items.drawableId);

//**********************************************************************

        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            getMenuInflater().inflate(R.menu.second, menu);
            return true;
        }
    }

项目类:

public class Item {
    String name;
    int drawable;
    String text;

    public int getDrawable() {
        return drawable;
    }

    public void setDrawable(int drawable) {
        this.drawable = drawable;
    }

    public Item(String name, String text, int id) {
        this.name = name;
        this.text = text;
        this.drawable = id;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

我的布局: 第一:

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

    <GridView
        android:id="@+id/gridview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:horizontalSpacing="10dp"
        android:numColumns="2"
        android:stretchMode="columnWidth"
        android:verticalSpacing="0dp" />

</FrameLayout>

第二

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

    <ImageView
        android:id="@+id/picture"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerCrop" />

    <TextView
        android:id="@+id/text_small"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

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

</LinearLayout>

squareimageview:

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

    <com.zva.app.SquareImageView
        android:id="@+id/picture"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerCrop" />

    <TextView
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:background="#55000000"
        android:paddingBottom="10dp"
        android:paddingLeft="7dp"
        android:paddingRight="7dp"
        android:paddingTop="10dp"
        android:textColor="@android:color/white" />

</FrameLayout>

2 个答案:

答案 0 :(得分:0)

在您的SecondActivity.java项目列表为空。因此,每当您尝试从空列表中的某个位置获取项目时,它都会抛出错误。

首先,尝试在列表中添加几个项目,并检查列表的大小是否大于您希望从列表中获取的位置。

完成上述更正后,您还必须更改SecondActivity中的以下行,

name.setText(items.name);
text.setText(items.text);
imageView.setImageResource(items.drawableId);

要,

name.setText(item.getName());
text.setText(item.getText());
imageView.setImageResource(item.getDrawable());

Bcs,您可以获取单项的名称,文字,可绘制值,而不是整个项目列表

编辑1:

gridView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View v,
                    int position, long id) {
                // Sending image id to SecondActivity
                Intent i = new Intent(getApplicationContext(), SecondActivity.class);
                Item item = (Item) items.get(position);
                // passing index (position of clicked item)
                i.putExtra("id", position);
                i.putExtra("Name",item.getName());
                i.putExtra("Text",item.getText());
                i.putExtra("image",item.getDrawable());
                startActivity(i);
            }
        });

编辑2:

i.putExtra("img", R.drawable.ic_launcher);

<强> NextActivity

int img;全球

Intent i = getIntent();

if(i != null)
    img = i.getIntExtra("img", 0);

LinearLayout main_lay = (LinearLayout) findViewById(R.id.main_lay);
main_lay.setBackgroundResource(img);

答案 1 :(得分:0)

首先,您必须传递具有该位置的项目列表

这里你只传递了点击项目的位置。

Intent i = new Intent(getApplicationContext(), SecondActivity.class);
  i.putExtra("id", position);
  startActivity(i);

您正在传递空值,即显示空白页。

首先你必须像这样传递

Intent i = new Intent(getApplicationContext(), SecondActivity.class);
i.putExtra("item", items);
i.putExtra("id", position);
startActivity(i);

你可以像这样使用list作为静态

static List<Items> items=new List<Items>();

在第二个活动中你必须像这样导入

import static here declare package name.items;

和像这样的代码

items.get(position).getName();