自定义吐司消息-GridView

时间:2018-11-20 00:16:06

标签: android

图像必须与自定义的祝酒消息一起出现,该消息应标明图片人物的名字

public class MainActivity extends AppCompatActivity {

Integer[] PersonalPhoto = {R.drawable.user1, R.drawable.user2, 
 R.drawable.user3, R.drawable.user4, R.drawable.user5, R.drawable.user6, 
 R.drawable.user7, R.drawable.user8 };

ImageView pic;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    GridView grid = ( GridView)findViewById(R.id.gridView);
    final ImageView pic = (ImageView) findViewById(R.id.imgLarge);
    grid.setAdapter(new ImageAdapter(this));
    grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @RequiresApi(api = Build.VERSION_CODES.O)
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int 
   position, long id ) {



            Toast.makeText(getBaseContext( ), "Selected User " + (position), 
            Toast.LENGTH_SHORT).show();
            pic.setImageResource(PersonalPhoto[position]);





        }
    });

}

GridView必须显示八个图像。当用户选择图片时,名字将出现在祝酒词中

1 个答案:

答案 0 :(得分:0)

似乎没问题,只需添加final关键字即可。

final Integer[] PersonalPhoto = {R.drawable.user1, R.drawable.user2, 
 R.drawable.user3, R.drawable.user4, R.drawable.user5, R.drawable.user6, 
 R.drawable.user7, R.drawable.user8 };

final ImageView pic;