【问题标题】:how to click in custom Gridview in android?如何在android中点击自定义Gridview?
【发布时间】:2013-09-23 19:01:21
【问题描述】:

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

【问题讨论】:

  • 你为什么要使用第二个充气机?[vi = inflater.inflate(R.layout.full_image, parent, false);] 你已经在if语句中做了。
  • 是的,它的附加功能...vi = inflater.inflate(R.layout.full_image, parent, false);是的...但是错误代码
  • 只用上一行 vi = inflater.inflate(R.layout.full_image,null)
  • 能否贴出错误日志和full_image的xml布局

标签: java android listview gridview


【解决方案1】:

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

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

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

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

看到了吗?

【讨论】:

  • 如果这对您有帮助或者是答案,请您将其标记为这样或支持它吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-06
  • 1970-01-01
  • 2013-07-14
  • 1970-01-01
相关资源
最近更新 更多