Tuesday 15 July 2014

android - Gridview with both image and text in each cell - OnClickListener ClassCastException error -


I am working with a gridview where each cell contains both an image and a line of text. I would like to, when the user chooses that item in the gridview, the image changes. I had this work when there was no image, but there was no image, but after changing the cell to include both image and text, I am getting the error:

  java. Lang.ClassCastException: android .widget.LinearLayout   

There is an error on the line where I am trying to get the imageView of selected items.

  imageView ivCheck = (imageView) parent.getChildAt (status);   

In my understanding, this is because what I am currently doing is unable to separate the textview from the image view ... However, in any way I do this I am unsure of

Code below:

grid.xml:

  & lt ;? Xml version = "1.0" encoding = "UTF-8"? & Gt; & Lt; GridView xmlns: android = "http://schemas.android.com/apk/res/android" android: id = "@ + id / Grid" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent" android: padding = "L0dp" Android: VerticalSpacing = "L0dp" Android: HorizontalSpacing = "L0dp" Android: numColumns = "2" Android: columnWidth = "90dp" Android: StretchMode = "ColumnWidth" android: gravity = "center" & gt; & Lt; / GridView & gt;   

icon.xml: & lt; LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: id = "@ + id / widget44" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" android: orientation = "Vertical" Android: layout_x = "201px" Android: layout_y = "165px" Android: gravity = "center_horizontal" & gt; & Lt; ImageView Android: id = "@ + id / icon_image" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" & gt; & Lt; / ImageView & gt; & Lt; TextView android: id = "@ + id / icon_text" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" android: text = "TextView" android: gravity = "center_horizontal" Android: textColorHighlight = "# 656,565" & gt ; & Lt; / TextView & gt; & Lt; / LinearLayout & gt;

The relevant java code:

  gridview.setAdapter (New ImageAdapter (this)); gridview.setOnItemClickListener (New OnItemClickListener () {public void onItemClick (AdapterView & LT ;? & gt; parents, see V, int position, long id) {// tag set, and subsequently failed to match println (parents (+ I.e. + 0; i ++); {imageView ivCheck = (imageView), "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ) parent.getChildAt (i); // error here   

I've read many tutorials on creating GridViews with both images and text, but the difficulty as well find one with a onclicklistener Is the way The image in the selected cell will change. I really appreciate any help you have!

Use of the scene To do this, simply use the visual v parameter that you want to change

just like this:.

  LinearLayout icon = (LinearLayout) V; ImageView image = icon.findViewById (R.id.icon_image); // Here with the image accessories   

Note that if the changes you made will not be saved, Scrolling away Item! The reason for this is that Gridview runs on the screen due to scrolling when going to recycle view . You want to allow users to modify items in GridView and also remember when they scroll away and you want to make changes to adapters to come back It will require that it populates GridView .

No comments:

Post a Comment