我是Android开发的新手,我阻止了一点。
我已经执行了一个Android教程,一切都很好,但我希望在我的第二个ImageView
上显示当前选定的Activity
。
这是我的第一个Activity
hotel.java
:
public class hotel extends Activity implements View.OnClickListener
{
final String EXTRA_LOGIN = "user_login";
final String EXTRA_PASSWORD = "user_password";
Button button;
int image_index = 0;
private static final int MAX_IMAGE_COUNT=8;
private Integer[]mImageIds ={
R.drawable.image1,
R.drawable.image2,
R.drawable.image3,
R.drawable.image4,
R.drawable.image5,
R.drawable.image6,
R.drawable.image7,
R.drawable.image8,
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.hotel);
ImageView imgView = (ImageView) findViewById(R.id.pictogramme);
imgView.setImageResource(mImageIds[image_index]);
BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inScaled = false;
opt.inSampleSize = 1;
opt.inDither = true;
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),mImageIds[image_index] ,opt);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
final byte[]b = baos.toByteArray();
final EditText login = (EditText)findViewById(R.id.user_email);
final EditText pass = (EditText)findViewById(R.id.user_password);
final Button loginButton = (Button) findViewById(R.id.connect);
loginButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(hotel.this, hoteldisplay.class);
intent.putExtra("picture", b);
intent.putExtra(EXTRA_LOGIN, login.getText().toString());
intent.putExtra(EXTRA_PASSWORD,pass.getText().toString());
startActivity(intent);
}
});
Intent intent = getIntent();
Button next = (Button)findViewById(R.id.button_next);
next.setOnClickListener(this);
Button previous = (Button)findViewById(R.id.button_previous);
previous.setOnClickListener(this);
showImage();
}
private void showImage() {
ImageView imgView = (ImageView) findViewById(R.id.pictogramme);
imgView.setImageResource(mImageIds[image_index]);
}
@Override
public void onClick(View v)
{
switch(v.getId())
{
case (R.id.button_previous):
image_index--;
if (image_index == -1){
image_index = MAX_IMAGE_COUNT -1;
}
showImage();
break;
case (R.id.button_next):
image_index++;
if (image_index == MAX_IMAGE_COUNT){
image_index = 0;
}
showImage();
break;
}
}
}
和我的第二个Activity
hoteldisplay.java
:
public class hoteldisplay extends Activity {
final String EXTRA_LOGIN = "user_login";
final String EXTRA_PASSWORD = "user_password";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login_display);
Intent intent = getIntent();
Bundle extras = getIntent().getExtras();
byte[] b = extras.getByteArray("picture");
Bitmap bmp = BitmapFactory.decodeByteArray(b, 0, b.length);
ImageView image = (ImageView) findViewById(R.id.image_display);
TextView loginDisplay = (TextView) findViewById(R.id.email_display);
TextView passwordDisplay = (TextView) findViewById(R.id.password_display);
if (intent != null) {
loginDisplay.setText(intent.getStringExtra(EXTRA_LOGIN));
passwordDisplay.setText(intent.getStringExtra(EXTRA_PASSWORD));
image.setImageBitmap(bmp);
}
}
}
目前我正确检索了我的ImageView
,但它是索引中的第一个,我希望选择好的图像。
答案 0 :(得分:0)
通过意图传递二进制对象是一种破解。有一种更容易(虽然也是粗略)的方法来传递对象,这种方法使用Application
类的实例来保存Global Variables
。
您不必手动创建Globals
的实例。它将在您的第一个活动开始时自动创建。
例如:
/* Globals.java */
public class Globals extends Application {
ImageView img;
}
/* Hotel.java */
public class Hotel extends Activity {
public void storeImageView() {
Globals globals = (Globals) getApplicationContext();
globals.img = myImgView; // save a ptr to your ImageView
myImgView = null // **see note below
}
}
/* HotelViewer.java */
public class HotelViewer extends Activity {
public void retrieveImageView() {
Globals globals = (Globals) getApplicationContext();
ImageView image = globals.img; // retrieve ImageView
}
}
**注意:如果您希望Garbage Collector
清除Hotel
活动,则无法生成指向Global
对象的指针。