简体   繁体   中英

onClickListener onclicklistener Android Java programming

When i click on the images it doesnt change the display textview, where did i go wrong?

    **List<ImageView> fields = new ArrayList<ImageView>();
    for(int i = 0; i < 64; i++) {
        try{
            id = R.id.class.getField("square" + (i+1)).getInt(0);
            views.add((ImageView)findViewById(id));
            ((View) fields).setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View fields) {
                    for(int i=0; i < 64; i++)
                    {
                        display = (TextView) findViewById(R.id.textView1);
                        display.setText("square" + i);
                    }
                }
            });
        }
        catch(Exception e){}

    }**

}

}

It looks like you're adding the onClick listener to the ArrayList of views , not the image you added. Change

 ((View) fields).setOnClickListener

to

 findViewById(id).setOnClickListener

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM