简体   繁体   中英

How can I access the position of an image in gallery?

I am trying to access the position of an image in gallery. I want to use position in another method other than onClickItem. How can I do that?

gallery.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView parent, View v, int position, long id) {

            switch(position) {

            case 0:{
                imageView1.setImageResource(R.drawable.plate01); 
                break;
            }
            case 1: imageView1.setImageResource(R.drawable.plate02); break;
            case 2: imageView1.setImageResource(R.drawable.plate03); break;

            }
            //Toast.makeText(RenkKorluguTesti.this, "" + position, Toast.LENGTH_SHORT).show();
        }
    });
}

public void tikla(View v) {
    if(editText1.getText().toString().equals("12") /*&& if the first image in gallery is chosen*/)

        Toast.makeText(RenkKorluguTesti.this, "Birinci testi geçtiniz.", Toast.LENGTH_SHORT).show();

}
int j;
gallery.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView parent, View v, int position, long id) {

            j=position;
            switch(position) {

            case 0:{
                imageView1.setImageResource(R.drawable.plate01); 
                break;
            }
            case 1: imageView1.setImageResource(R.drawable.plate02); break;
            case 2: imageView1.setImageResource(R.drawable.plate03); break;

            }
            //Toast.makeText(RenkKorluguTesti.this, "" + position, Toast.LENGTH_SHORT).show();
        }
    });
}

 Now use `j` when you want.....

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