Gridview OnItemClickListener Not Working But Why?

1.3k views Asked by At

I have a base adapter and gridview. I'm populating items with adapter gridview. When user touches any item, I want to show a toast. But it's never shown.

@petey I update the code and i added AdapterB class for you. Please inspect.

Code:

    @Override
    protected void onPostExecute(final Boolean success)
    {
        final AdapterB ada = new AdapterB(context, WallPaperList);
        gridView.setAdapter(ada);
        if (progressDialog.isShowing())
        {
            progressDialog.dismiss();
        }

        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long identity) {
                Toast.makeText(context,ada.getItem(position).get(POST_ID), LENGTH_LONG).show();
            }
        });
    }

AdapterB Class:

public class AdapterB extends BaseAdapter {
    private Context mContext;
    private ArrayList<HashMap<String, String>> data;
    private static LayoutInflater inflater = null;
    public ImageLoader imageLoader;

    public AdapterB(Context c,ArrayList<HashMap<String,String>> d) {
        // TODO Auto-generated constructor stub
        mContext = c;
        data = d;
        inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        imageLoader = new ImageLoader(mContext,true);
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return data.size();
    }

    public HashMap<String, String> getItem(int position) {
        HashMap<String, String> tar;
        tar = data.get(position);
        return tar;
    }

    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub

        View grid;

        if (convertView == null) {
            grid = new View(mContext);
            LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            grid = inflater.inflate(R.layout.mygrid, parent, false);
        } else {
            grid = (View) convertView;
        }

        ImageView imageView = (ImageView) grid.findViewById(R.id.imagepart);

        HashMap<String,String> tar;
        tar = data.get(position);

        imageLoader.DisplayImage(tar.get(FragmentB.FOTOYOL),imageView);
/*
        grid.setId(Integer.parseInt(tar.get(FragmentB.POST_ID)));

        grid.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(mContext,"Clicked "+position,Toast.LENGTH_LONG).show();
            }
        });*/
        return grid;
    }
}

Layout Xml:

<GridView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/gridview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:numColumns="auto_fit"
    android:verticalSpacing="10dp"
    android:horizontalSpacing="10dp"
    android:columnWidth="90dp"
    android:stretchMode="columnWidth"
    android:gravity="center"
    android:descendantFocusability="blocksDescendants" />
1

There are 1 answers

0
Emre Erol On BEST ANSWER

I found a solution.

My populating item has got a check box. And i give it a property like this;

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

And it's work! But i dont now how can i check the boxes right now.. If i found i will update here.