如何在android中单击自定义Gridview?

时间:2013-09-23 19:01:22

标签: java android listview gridview

我编写包含Gridview的应用程序,用于显示产品,因此当在网格中选择项目时,返回值为nullpointerException。当我使用listview时它没关系,但在Gridview中它的错误。

grid_data.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view,
            int position, long id) {

        title = ((TextView) view.findViewById(R.id.title)).getText()
                .toString();

        categoriid = ((TextView) view.findViewById(R.id.categoryid))
                .getText().toString();

        String productid = ((TextView) view
                .findViewById(R.id.productid)).getText().toString();

    }
});

适配器类:

public View getView(int position, View convertView, ViewGroup parent) {



View vi = convertView;
if (vi  == null)


//LayoutInflater inflater = ((Activity) activity).getLayoutInflater();
vi = inflater.inflate(R.layout.full_image, parent, false);

TextView title = (TextView) vi.findViewById(R.id.txt_title); // title
TextView     maxprice = (TextView) vi.findViewById(R.id.txt_maxprice); // artist
TextView minprice = (TextView) vi.findViewById(R.id.txt_minprice); // artist
TextView productid = (TextView) vi.findViewById(R.id.productid); // artist
TextView categoryid = (TextView) vi.findViewById(R.id.categoryid); // artist
ImageView    thumb_image = (ImageView) vi
        .findViewById(R.id.full_image_view); // thumb
                                                // image

HashMap<String, String> song = new HashMap<String, String>();
song = data.get(position);
title.setText(song.get(Service.Key_Title));
productid.setText(song.get("Productid"));
categoryid.setText(song.get("Categoryid"));
maxprice.setText(song.get("MaxPrice"));
minprice.setText(song.get("MinPrice"));

imageLoader.DisplayImage(song.get("picPath"), thumb_image);
return vi;

}

full_image.xml

<?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/full_image_view"
        android:layout_width="80sp"
        android:layout_height="80sp"
        android:background="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/txt_title"
        android:layout_width="80sp"
        android:layout_height="wrap_content"
        android:text="title" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/txt_maxprice"
            android:layout_width="70sp"
            android:layout_height="wrap_content"
            android:text="maxprice" />

        <ImageView
            android:id="@+id/image_view"
            android:layout_width="10sp"
            android:layout_height="10sp"
            android:background="@drawable/ic_launcher" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/txt_minprice"
            android:layout_width="70sp"
            android:layout_height="wrap_content"
            android:text="minprice" />

        <ImageView
            android:id="@+id/image_view2"
            android:layout_width="10sp"
            android:layout_height="10sp"
            android:background="@drawable/ic_launcher" />
    </LinearLayout>

    <TextView
        android:id="@+id/productid"
        android:layout_width="1sp"
        android:layout_height="1sp"
        android:visibility="gone" />

    <TextView
        android:id="@+id/categoryid"
        android:layout_width="1sp"
        android:layout_height="1sp"
        android:visibility="gone" />

</LinearLayout>

如何解决点击gridview值的title或其他数据保存在变量中的问题 感谢

1 个答案:

答案 0 :(得分:1)

你有拼写错误吗?在你的getView方法中,你是

TextView title = (TextView) vi.findViewById(R.id.txt_title); // title

但是在你的OnItemClickListener中你正在寻找别的东西

title = ((TextView) view.findViewById(R.id.title)).getText().toString()

请参阅?