2

我编写了包含 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值或其他数据保存在变量中的问题谢谢

4

1 回答 1

1

你有错字吗?在您的 getView 方法中,您是

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

但是在您的 OnItemClickListener 中,您正在寻找其他东西

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

看?

于 2013-09-23T19:38:16.327 回答