Note that there are some explanatory texts on larger screens.

plurals
  1. POChanging state of images inside ListView Items by pressing on row's buttons
    text
    copied!<p>My main purpose is to make some buttons inside a listview, to change the state of some images (inside listview items too). So I worked out this code, creating a custom adapter like this one: </p> <pre><code>public class CategoriesGridViewAdapter extends BaseAdapter { private Activity context; private List&lt;CategoryHolder&gt; mCategoriesList = new ArrayList&lt;CategoryHolder&gt;(); private CategoryHolder mCategoryHolder; // @SuppressLint("UseSparseArrays") // private Map&lt;Integer, Boolean&gt; favVisibilitiesMap = new HashMap&lt;Integer, // Boolean&gt;(); // @SuppressLint("UseSparseArrays") // private Map&lt;Integer, Boolean&gt; cartVisibilitiesMap = new HashMap&lt;Integer, // Boolean&gt;(); private ViewHolder holder = null; private int[] mTestingImagesForCategories = new int[] { R.drawable.pic1, R.drawable.pic2, R.drawable.pic3, R.drawable.pic4, R.drawable.pic5 }; public CategoriesGridViewAdapter(Activity activity, List&lt;CategoryHolder&gt; categoriesList) { this.context = activity; this.mCategoriesList = categoriesList; } @Override public int getCount() { return mCategoriesList.size(); } @Override public Object getItem(int pos) { return mCategoriesList.get(pos); } @Override public long getItemId(int arg0) { return 0; } @Override public View getView(int pos, View convertView, ViewGroup parent) { mCategoryHolder = mCategoriesList.get(pos); if (convertView == null) { convertView = context.getLayoutInflater().inflate( R.layout.cell_categories_gridview, parent, false); holder = new ViewHolder(convertView); holder.btn_cell_categoryAddCart .setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { int getPosition = (Integer) v.getTag(); CategoryHolder localCategoryHolder = mCategoriesList .get(getPosition); localCategoryHolder.setCartVisible(!mCategoriesList .get(getPosition).isCartVisible()); holder.iv_cell_categoryCart .setVisibility(setImageVisibility(localCategoryHolder .isCartVisible())); } }); holder.btn_cell_categoryAddFav .setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { int getPosition = (Integer) v.getTag(); CategoryHolder localCategoryHolder = mCategoriesList .get(getPosition); localCategoryHolder.setFavVisible(!mCategoriesList .get(getPosition).isFavVisible()); holder.iv_cell_categoryFav .setVisibility(setImageVisibility(localCategoryHolder .isFavVisible())); } }); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } holder.btn_cell_categoryAddCart.setTag(pos); holder.btn_cell_categoryAddFav.setTag(pos); holder.iv_cell_categoryCart .setVisibility(setImageVisibility(mCategoryHolder .isCartVisible())); holder.iv_cell_categoryFav .setVisibility(setImageVisibility(mCategoryHolder .isFavVisible())); // just for testing, the final images will be some bitmaps downloaded from a server holder.iv_cell_categoryContent .setBackgroundResource(mTestingImagesForCategories[pos]); return convertView; } private int setImageVisibility(boolean isVisible) { return (isVisible) ? View.VISIBLE : View.INVISIBLE; } static class ViewHolder { public ImageView iv_cell_categoryCart; public ImageView iv_cell_categoryFav; public ImageView iv_cell_categoryContent; public Button btn_cell_categoryAddFav; public Button btn_cell_categoryAddCart; public ViewHolder(View convertView) { iv_cell_categoryContent = (ImageView) convertView .findViewById(R.id.iv_cell_category_image); iv_cell_categoryCart = (ImageView) convertView .findViewById(R.id.iv_cell_category_cart); iv_cell_categoryFav = (ImageView) convertView .findViewById(R.id.iv_cell_category_fav); btn_cell_categoryAddFav = (Button) convertView .findViewById(R.id.btn_cell_category_add_fav); btn_cell_categoryAddCart = (Button) convertView .findViewById(R.id.btn_cell_category_add_cart); } } </code></pre> <p>}</p> <p>I just don't understand what is wrong with my code?!</p> <p>I have this adapter, which I debugged and everything is working ok (changes the boolean state of those images I placed inside the layout), but:</p> <h2>1.</h2> <ul> <li>it doesn't make the images( holder.iv_cell_categoryCart, holder.iv_cell_categoryFav) visible / invisible. What could the problem be? (problem solved using notifyDataSetChanged in each setOnClickListener.. yeah I know.. stupid me )</li> </ul> <h2>2.</h2> <ul> <li>those static images (mTestingImagesForCategories -- R.drawable.pic1, R.drawable.pic2, R.drawable.pic3, R.drawable.pic4, R.drawable.pic5) are not ever shown.</li> </ul> <p>The layout for this GridView looks like this:</p> <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" &gt; &lt;ImageView android:id="@+id/iv_cell_category_image" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_above="@+id/lay_cell_category_content" android:layout_below="@+id/lay_cell_category_header" android:background="@drawable/ic_launcher" android:paddingLeft="5dp" android:paddingRight="5dp" android:paddingTop="5dp" android:scaleType="fitXY" /&gt; &lt;RelativeLayout android:id="@+id/lay_cell_category_header" android:layout_width="fill_parent" android:layout_height="@dimen/activity_categories_gridview_cornerimage_height" android:layout_alignParentTop="true" &gt; &lt;ImageView android:id="@+id/iv_cell_category_fav" android:layout_width="@dimen/activity_categories_gridview_cornerimage_width" android:layout_height="fill_parent" android:layout_above="@+id/lay_cell_category_content" android:layout_alignParentLeft="true" android:background="@drawable/carti" /&gt; &lt;ImageView android:id="@+id/iv_cell_category_cart" android:layout_width="@dimen/activity_categories_gridview_cornerimage_width" android:layout_height="fill_parent" android:layout_alignParentRight="true" android:background="@drawable/star" /&gt; &lt;/RelativeLayout&gt; &lt;LinearLayout android:id="@+id/lay_cell_category_content" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:orientation="horizontal" &gt; &lt;Button android:id="@+id/btn_cell_category_add_cart" android:layout_width="0dip" android:layout_height="wrap_content" android:layout_weight="1" android:focusable="false" android:focusableInTouchMode="false" android:text="Add Cart" android:textSize="12sp" /&gt; &lt;Button android:id="@+id/btn_cell_category_add_fav" android:layout_width="0dip" android:layout_height="wrap_content" android:layout_weight="1" android:focusable="false" android:focusableInTouchMode="false" android:text="Add Fav" android:textSize="12sp" /&gt; &lt;/LinearLayout&gt; </code></pre> <p></p> <p>The idea of this layout is to have a layout main content of an image, with 2 bottom buttons which makes 2 smalls images at the top right and left of the layout visible/invisible. (If user presses on Add Fav, the top right image consisting of a star will be visible. If he presses it again, the image will become invisible -- simulating adding / removing product to favorites). Please any help is appreciated!</p> <p>LE: I should probably mention that I'm creating the GridView programmatically, inside a Fragment (used in a ViewPager).</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload