Reputation: 2919
I have a gridview in my code and a custom adapter. I'm trying to set a setOnItemClickedListener but i have no return.
Can someone help me understand why my setOnItemClickListener does not work?
Thank you!
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<Card> 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<?> arg0, View arg1, int position, long arg3) {
Card item = itens.get(position);
Toast.makeText(context, "Card: " + item.getCardID(), Toast.LENGTH_LONG).show();
}
}
My layout (item for gridview)
<?xml version="1.0" encoding="utf-8"?>
<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" />
Adapter
public class PuzzleAdapter extends BaseAdapter {
private ArrayList<Card> puzzleLevel;
public static Context context;
public PuzzleAdapter(Context ctx, ArrayList<Card> 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;
}
}
UPDATED
I changed my layout to:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<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" />
</LinearLayout>
Everything works. For some reason when I put an ImageButton in my "custom adapter" the 'click' does not work.
Thank you all!
Upvotes: 0
Views: 2275
Reputation: 235
The problem is with the android:clickable="true" in your layout. When you mark the ImageButton clickable and then click on your GridView item, the controller looks for a click handler for the ImageButton and not the GridView item. As a result, your OnItemClick method is never called. When you changed your layout, you did not have clickable=true.
Upvotes: 2
Reputation: 13825
This works in my case
GridView sdcardImages = (GridView) findViewById(R.id.sdcard);
sdcardImages.setAdapter(new ImageAdapter(myImages, myViews));
sdcardImages.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v,
int position, long id) {
Intent i = new Intent(EquipmentViews.this, VIRDemo.class);
i.putExtra("EquipmentId", equipmentIds[position]);
startActivity(i);
}
});
Adapter class
/**
* Adapter for our image files.
*/
private class ImageAdapter extends BaseAdapter {
private int[] myRemoteImages;
private String[] myRemoteViews;
public ImageAdapter(int[] myimages, String[] myviews) {
myRemoteImages = myimages;
myRemoteViews = myviews;
}
public int getCount() {
return this.myRemoteImages.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View v;
if (convertView == null) {
LayoutInflater li = getLayoutInflater();
v = li.inflate(R.layout.imageitem, null);
TextView tv = (TextView) v.findViewById(R.id.icon_text);
tv.setText(myRemoteViews[position]);
ImageView iv = (ImageView) v.findViewById(R.id.icon_image);
iv.setImageResource(myRemoteImages[position]);
} else {
v = convertView;
}
return v;
}
Upvotes: 1
Reputation: 1645
Never working with gridview but I usually set up my adapter like this:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageButton image;
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater) getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.grid__item, null);
}
image = (ImageButton) convertView.findViewById(R.id.image);
image.setImageResource(Constantes.HIDECARD_RESOURCE);
image.setLayoutParams(new GridView.LayoutParams(130, 130));
image.setBackgroundColor(Color.TRANSPARENT);
return convertView;
}
Upvotes: 1