我正在使用GridView并且其中包含很多项目。这是我的gridview.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" >
<GridView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/gridview"
android:numColumns="auto_fit"
android:horizontalSpacing="5dp"
android:verticalSpacing="5dp"
android:layout_margin="5dp"
android:background="#FF8040"
android:clickable="true"/>
</LinearLayout>
这是gridview_layout.xml文件,其中包含要在gridview中显示的元素..
<?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"
android:padding="5dp"
android:background="#FFFFFF">
<RelativeLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/rel1">
<ImageView
android:id="@+id/flag"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"/>
<TextView
android:id="@+id/txt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="15dp"
android:gravity="center_horizontal"
android:layout_below="@+id/flag"/>
<RatingBar
android:id="@+id/rting1"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_below="@+id/txt"
android:numStars="5"
style="@style/foodRatingBar"/>
<LinearLayout
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:orientation="horizontal"
android:layout_below="@+id/rting1"
android:weightSum="2">
<Button
android:layout_width="0dp"
android:layout_height="24dp"
android:id="@+id/btnn1"
android:text="Details"
android:background="#A0A0A0"
android:layout_marginLeft="3dp"
android:layout_weight="1"
android:gravity="center"/>
<Button
android:layout_width="0dp"
android:layout_height="24dp"
android:id="@+id/btnn2"
android:text="Enquiry"
android:background="#A0A0A0"
android:layout_marginLeft="3dp"
android:layout_weight="1"/>
</LinearLayout>
</RelativeLayout>
</LinearLayout>
我正在使用标准的OnItemClickListner(),但这不起作用。 logCat中没有错误。 下面是我的GridViewActivity ..
public class GridViewActivity extends Activity{
GridView gridView;
Context context;
ArrayList itemName;
Button btn11;
public static String [] web={"Butterfly Valve","Globe Valve","Piston Valve","Forged Globe Valve","Swing Non Return Valve","Wafer Non Return Valve","Diapgragm Valve","Three Way Ball Valve","Pp And Pvc Ball Valve",
"Forged Ball Valve","Wafer Ball Valve","Pocket Less Ball Valve","Knife Edge Gate Valve"};
public static int [] imageId={R.drawable.image1,
R.drawable.image1,
R.drawable.image1,
R.drawable.image1,
R.drawable.image1,
R.drawable.image1,
R.drawable.image1,
R.drawable.image1,
R.drawable.image1,
R.drawable.image1};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gridview);
CustomGrid adapter = new CustomGrid(GridViewActivity.this, web, imageId);
gridView = (GridView)findViewById(R.id.gridview);
gridView.setAdapter(adapter);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
Toast.makeText(GridViewActivity.this, "you clicked at..." +web[ + position], Toast.LENGTH_LONG).show();
}
});
}
}
最后这是我的CustomGrid适配器类: -
public class CustomGrid extends BaseAdapter{
private Context mContext;
private final String[] web;
private final int[] imageId;
public CustomGrid(Context c,String[] web,int[] imageId ) {
mContext = c;
this.imageId = imageId;
this.web = web;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return web.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View grid;
LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(convertView == null){
grid = new View(mContext);
grid = inflater.inflate(R.layout.gridview_layout, null);
TextView textview = (TextView)grid.findViewById(R.id.txt);
ImageView imageview = (ImageView)grid.findViewById(R.id.flag);
textview.setText(web[position]);
imageview.setImageResource(imageId[position]);
} else {
grid = (View) convertView;
}
return grid;
}
}
答案 0 :(得分:1)
为什么onItemClickListener
无法正常工作,可能是因为您的Buttons
获得了每个项目的重点。尝试为focusable="false"
中的每个项目Buttons
设置gridview_layout.xml
。
但是,您获得的崩溃是NullPointerException
(下次确保发布Logcat以便其他人可以轻松提供帮助),因为id
中btnn1
没有gridview.xml
你提到的{1}}。 id
位于gridview_layout.xml
内。
因此,您需要创建自定义BaseAdapter
并设置为Adapter
的{{1}}。然后,在此自定义GridView
内的覆盖getView
方法中,您将获得BaseAdapter
,就像您未能做到的那样。
以下是为Button
创建BaseAdapter
的示例。
答案 1 :(得分:1)
您的应用程序崩溃,因为btn11
属于gridview_layout.xml
。
但您已将gridview.xml
设置为内容视图。
您只能引用属于您在setContentView(layoutResID)
看起来你搞砸了。
如果您正在寻找自定义网格视图,请查看以下示例。
http://www.learn2crack.com/2014/01/android-custom-gridview.html