9

i am using a grid view in which i am using a text view for each cell. i am using onitemclick to perform some action when clicked on grid cell. i want to disable on item click for particular positions in grid view. how do i do that. i used convertView.setclickable(false) for particular position in getView which is giving null pointer exception. How do i do that?? here s my code.

@Override
public View getView(int position, View convertView, ViewGroup parent) {
  if (convertView == null) {
    textView = new TextView(context);
    textView.setLayoutParams(new GridView.LayoutParams(35, 35));
  } else {
    textView = (TextView) convertView;
  }

        textView.setTextSize(10);
        day_color = list.get(position).split("-");
        textView.setText(day_color[0]);

        if (day_color[1].equals("GREY")) {
            textView.setTextColor(Color.LTGRAY);
            //greyvalues.get(position)CalendarEvents
            convertView.setClickable(false);

        }
        if (day_color[1].equals("BLACK")) {
            textView.setTextColor(Color.BLACK);
        }
        if ((day_color[1].equals("BLUE"))) {
            textView.setTextColor(Color.RED);
        }

        setColor = Color.TRANSPARENT;
        if (position >= startPos && position <= endPos
                && selectdayselected != true) {
            setColor = Color.DKGRAY;
        }
        if (startPos == position && selectdayselected == true)
            setColor = Color.DKGRAY;
        textView.setBackgroundColor(setColor);


        return textView;
    }
Brian Cooley
  • 11,622
  • 4
  • 40
  • 39
user562237
  • 775
  • 5
  • 15
  • 24

5 Answers5

40

In your adapter override

@Override
public boolean areAllItemsEnabled() {
    return false;
}

and implement

@Override
public boolean isEnabled(int position) {
   // Return true for clickable, false for not
   return false;
}
David Manpearl
  • 12,362
  • 8
  • 55
  • 72
slund
  • 6,367
  • 2
  • 26
  • 19
2

I used textView.setEnabled(false) to disable its onItemClick(). It works for me.

axel22
  • 32,045
  • 9
  • 125
  • 137
harleyw
  • 31
  • 2
1

Like example your code about the condition:

 @Override
public View getView(int position, View convertView, ViewGroup parent) {

...

        if (day_color[1].equals("GREY")) {
            textView.setTextColor(Color.LTGRAY);
            //greyvalues.get(position)CalendarEvents
            convertView.setClickable(false);
...

    }

your condition like example may be:

                if (position == 0){
                    isEnabled(position)
                }

the code functionally:

 @Override
public View getView(int position, View convertView, ViewGroup parent) {

      ...

      if (position == 0){
          isEnabled(position)
      }
      ...

    }

@Override
public boolean isEnabled(int position){
    if(position == 0)
        return false;

    else
        return true;
}

@Override
public boolean areAllItemsEnabled(){

    return true;
}

Code adapted to your needs, I hope its work for you.

Alex Zaraos
  • 6,443
  • 2
  • 26
  • 21
1

You need to set it on textView. There may not be a convert view, which will cause an NPE when you call setClickable on it.

axel22
  • 32,045
  • 9
  • 125
  • 137
Brian Cooley
  • 11,622
  • 4
  • 40
  • 39
0

I have searched through this list and could not find any answer that worked for me. Apparently, I tried the following code and it worked.

I have a textview within the grid and the text view is disabled, however the onClick method is for the Grid (as we need to capture the position of the click) and it is always enabled. So, I decided to check whether the child element of the grid is enabled or disabled like this...

grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> view, View cell, int position, long id)
        {
            if (grid.getChildAt(position).isEnabled()) //do whatever you want
}
});