Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to disable item click for particular positions in grid view in android

Tags:

android

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;
    }
like image 999
user562237 Avatar asked Apr 01 '11 14:04

user562237


1 Answers

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.

like image 106
Alex Zaraos Avatar answered Sep 28 '22 06:09

Alex Zaraos