Kostya  Khuta
Kostya Khuta

Reputation: 683

Grid view setOnItemClickListener does not work

my OnClickListener does not work. MyCode is:

public class ReportAboutProblem extends ActionBarActivity implements View.OnClickListener{
public final int CAMERA_RESULT = 0;
GridView gridView;
Button btnAddPhoto;
ArrayList<Bitmap> myImages;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.report_about_problem_activity);
    getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#85abc4")));
    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
    btnAddPhoto=(Button)findViewById(R.id.btn_add_photo);
    btnAddPhoto.setOnClickListener(this);
    gridView = (GridView) findViewById(R.id.grid_new);
    myImages=new ArrayList<Bitmap>();
    myImages.add(BitmapFactory.decodeResource(getResources(),
            R.drawable.ic_launcher));
    gridView.setOnItemClickListener(mItemClickListener);
    gridView.setAdapter(new CustomGridAdapter(this, myImages));

}
private AdapterView.OnItemClickListener mItemClickListener = new AdapterView.OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {

        Toast.makeText(ReportAboutProblem.this, "You have clicked on item '"  + "'", Toast.LENGTH_SHORT).show();
    }
};
@Override
public void onClick(View v) {
    Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(cameraIntent, CAMERA_RESULT);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == CAMERA_RESULT) {
        Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
        myImages.add(thumbnail);
        gridView.setOnItemClickListener(mItemClickListener);
        gridView.setAdapter(new CustomGridAdapter(this, myImages));

    }
}
}

My Adapter is:

public class CustomGridAdapter extends BaseAdapter {

private Context context;
//private final String[] gridValues;
ArrayList<Bitmap> arrayList;
ImageButton i1;

//Constructor to initialize values
public CustomGridAdapter(Context context, ArrayList<Bitmap> ob) {
    arrayList = ob;
    this.context = context;
    // this.gridValues     = gridValues;
}

@Override
public int getCount() {

    // Number of times getView method call depends upon gridValues.length
    return arrayList.size();
}

@Override
public Object getItem(int position) {

    return null;
}

@Override
public long getItemId(int position) {

    return 0;
}


// Number of times getView method call depends upon gridValues.length

public View getView( int position,  View convertView,  ViewGroup parent) {
    final int pos = position;

    Log.d("myLogs","position="+position);
    // LayoutInflator to call external grid_item.xml file

    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View gridView;
    if (convertView == null) {
        gridView = new View(context);
        gridView = inflater.inflate(R.layout.grid_item, null);
        ImageView imageView = (ImageView) gridView
                .findViewById(R.id.grid_item_image);
        Log.d("myLogs","Set image "+position+" image "+arrayList.get(position));
        imageView.setImageBitmap(arrayList.get(position));
    } else {

        gridView = (View) convertView;
    }

    return gridView;
}
}

Custom gridView element is:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="100dp"
          android:layout_height="100dp"
          android:padding="5dp" >

<ImageView
        android:id="@+id/grid_item_image"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_marginRight="10px"
        android:src="@drawable/ic_launcher" android:layout_alignParentLeft="true" android:layout_marginLeft="4dp"
        android:layout_alignParentTop="true" android:layout_marginTop="4dp">

</ImageView>

<ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageButton" android:src="@android:drawable/ic_delete"
        android:background="@android:color/transparent"
        android:paddingLeft="10dp"
        android:layout_alignParentRight="true" android:layout_alignParentTop="true"/>

Upvotes: 0

Views: 3234

Answers (2)

MPG
MPG

Reputation: 795

Use imageView instead of ImageButton it will run..

[Solution to this question is;

Do not use clickable objects in the grid. That case Android can not handle the click event of grid.

Instead use something to show similar user interface view. Than handle that objects click actions.

false; put button in the grid to perform some click actions.

true; put an imageview instead of imagebutton and handle imageview's click events.]1

OR

Put these

          android:focusable="false"
          android:focusableInTouchMode="false" 

to your ImageButton.

Upvotes: 2

Ankit Bansal
Ankit Bansal

Reputation: 1811

try using android:descendantFocusability="blocksDescendants", ImageButton consumes the focus when a list item is clicked.

Upvotes: 1

Related Questions