Note that there are some explanatory texts on larger screens.

plurals
  1. POsetOnItemClickedListener not working (gridview)
    primarykey
    data
    text
    <p>I have a gridview in my code and a custom adapter. I'm trying to set a setOnItemClickedListener but i have no return. </p> <p>Can someone help me understand why my setOnItemClickListener does not work?</p> <p>Thank you!</p> <pre><code>import java.util.ArrayList; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.GridView; import android.widget.Toast; import br.com.m.bpuzzle.data.Card; import br.com.m.bpuzzle.data.ImageLevel; import br.com.m.bpuzzle.data.PuzzleAdapter; import br.com.m.bpuzzle.util.Constantes; public class BPuzzleActivity extends Activity { private GridView puzzle; private ImageLevel puzzleLevel; private PuzzleAdapter adapter; private static BPuzzleActivity context; private static ArrayList&lt;Card&gt; itens; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.game); context = this; setupPuzzle(); } @Override public void onBackPressed() { super.onBackPressed(); context.overridePendingTransition(R.anim.anim__fadeout, R.anim.anim__fadein); } private void setupPuzzle() { Log.i(Constantes.APPTAG, "Setup puzzle"); puzzleLevel = new ImageLevel(8, ImageLevel.LEVEL_ANIMALS); itens = puzzleLevel.getListElements(); adapter = new PuzzleAdapter(itens, context); puzzle = (GridView) findViewById(R.id.puzzle); puzzle.setAdapter(adapter); puzzle.setClickable(true); puzzle.setOnItemClickListener(new ItemGridViewClickListener()); } private static class ItemGridViewClickListener implements OnItemClickListener { @Override public void onItemClick(AdapterView&lt;?&gt; arg0, View arg1, int position, long arg3) { Card item = itens.get(position); Toast.makeText(context, "Card: " + item.getCardID(), Toast.LENGTH_LONG).show(); } } </code></pre> <p>My layout (item for gridview)</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;ImageButton xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/image" android:layout_width="100dp" android:layout_height="100dp" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:background="@drawable/card" android:clickable="true" android:contentDescription="@string/app_name" android:focusable="false" android:focusableInTouchMode="false" /&gt; </code></pre> <p>Adapter</p> <pre><code>public class PuzzleAdapter extends BaseAdapter { private ArrayList&lt;Card&gt; puzzleLevel; public static Context context; public PuzzleAdapter(Context ctx, ArrayList&lt;Card&gt; puzzleData) { context = ctx; puzzleLevel = puzzleData; } @Override public int getCount() { return this.puzzleLevel.size(); } @Override public Card getItem(int position) { return puzzleLevel.get(position); } @Override public long getItemId(int position) { return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { ImageButton image; if (convertView == null) { image = new ImageButton(context); convertView = LayoutInflater.from(context).inflate(R.layout.grid__item, null); } else { image = (ImageButton) convertView; } image.setImageResource(Constantes.HIDECARD_RESOURCE); image.setLayoutParams(new GridView.LayoutParams(130, 130)); image.setBackgroundColor(Color.TRANSPARENT); return image; } } </code></pre> <p><strong>UPDATED</strong></p> <p>I changed my layout to:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" &gt; &lt;ImageView android:id="@+id/image_button" android:layout_width="110dp" android:layout_height="110dp" android:layout_gravity="center" android:contentDescription="@string/app_name" android:scaleType="fitCenter" android:src="@drawable/card" /&gt; &lt;/LinearLayout&gt; </code></pre> <p>Everything works. For some reason when I put an ImageButton in my "custom adapter" the 'click' does not work.</p> <p>Thank you all!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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