Android GridView first button not working

て烟熏妆下的殇ゞ 提交于 2019-12-12 20:13:06

问题


I am having weird problems with Android GridView. I create a 3x4 grid and insert buttons into that grid. I want the background of the button to change when the user clicks that button. And this is working just fine for all buttons except the first one (the one with index 0 - top left). OnClick event listener doesn't fire at all for that button no matter what I do.

Here is the code where I create the view:

public View getView(int position, View convertView, ViewGroup parent) {
    Button imageView;

    if (convertView == null) {  // if it's not recycled, initialize some attributes
        Log.w("NOVO", "narejena nova celica");
        imageView = new Button(mContext);

        imageView.setPadding(8, 8, 8, 8);
    } else {
        Log.w("STARO", "stara celica");
        imageView = (Button) convertView;
    }

    imageView.setEnabled(true);

    int visina = parent.getHeight();
    int sirina = parent.getWidth();

    float dip = mContext.getResources().getDisplayMetrics().density;
    float margin = 10*dip;

    int view_height = (int)(visina - 3*margin)/4;
    int view_width = (int)(sirina - 2*margin)/3;

    int view_dim = 0;
    if (view_height <= view_width)
        view_dim = view_height;
    else
        view_dim = view_width;

    imageView.setLayoutParams(new GridView.LayoutParams(view_dim, view_dim));

    imageView.setId(position);
    imageView.setOnClickListener(celice.get(position));
    /*imageView.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            //Toast toast = Toast.makeText(mContext, v.getId() + "bla", Toast.LENGTH_SHORT);
            //toast.show();
            celice.get(v.getId()).celicaVisible(4000);
        }});*/

    celice.get(position).id = position;
    celice.get(position).setButton(imageView);

    return imageView;
}

If I replace

imageView = (Button) convertView;

with

imageView = new Button(mContext);

then the onClick() gets fired but the background still doesn't change. All the other buttons are working as expected.

And here is the custom class "Celica" that takes care of the actual work - changing the background...

public class Celica implements OnClickListener {

public boolean odkrit;
public boolean najden;
public int id;
public Drawable slikca1, slikca2;
public Celica par;
private Timer tim;
public Button but;
public Context con;
static int buttonsVisible = 0;

Celica(Drawable s1, Drawable s2) {
    this.slikca1 = s1;
    this.slikca2 = s2;
}

void celicaVisible(int millis) {
    if (odkrit)
        return;

    Log.w("TEST", "prizganih " + buttonsVisible);
    if (buttonsVisible >= 2)
        return;

    odkrit = true;
    buttonsVisible++;
    tim = new Timer();
    tim.schedule(new timerDone(), millis);

    ((Activity)con).runOnUiThread(new Runnable() {
           @Override
           public void run() {
               but.setBackground(slikca2);
           }
    });

}

void setButton(Button b) {
    but = b;

    ((Activity)con).runOnUiThread(new Runnable() {
        @Override
        public void run() {
           but.setBackground(slikca1);
        }
 });
}

class timerDone extends TimerTask {

    @Override
    public void run() {
        if (!najden) {
            odkrit = false;

            ((Activity)con).runOnUiThread(new Runnable() {
                   @Override
                   public void run() {
                       but.setBackground(slikca1);
                   }
            });
        }

        buttonsVisible--;
        tim.cancel();
    }

}

@Override
public void onClick(View v) {
    celicaVisible(4000);
}

}


回答1:


In Android, ID of any View must be non zero and non negative number. means View ID should be > 0. and there is problem, when you are setting ID to the Button like

 imageView.setId(position)

here ID of a button will be zero when position is zero(means first item). may be due to this, First Button's OnClickListener is not getting fired...try setting a ID that is greater than zero to Button and try once...

you can write like

imageView.setId(position+1) to ensure ID > 0



回答2:


I actually figured it out. Everything works if I use the view that gets provided by the onClick() method instead of saving the actual button at the creation of the Celica object.

So basically adding:

but = (Button) v;

to the onClick() method solved the problem.



来源:https://stackoverflow.com/questions/20471625/android-gridview-first-button-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!