[英]How can I change an ImageView after a ListActivity?
The idea is to go to a ListActivity, click on an item, and if you clicked on item X it displays an especific image. 这个想法是去一个ListActivity,单击一个项目,如果您单击项目X,它将显示一个特定的图像。
That's my Android code. 那是我的Android代码。
package vds.cmc;
public class Dae extends ListActivity {
static final String bigfoot[] = {"Boy", "Girl"};
ImageView iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setListAdapter(new PeopleAdapter(this, bigfoot));
iv = (ImageView) findViewById(R.id.ivID);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
String qlo = bigfoot[position];
try {
if (qlo == "Girl") {
setContentView(R.layout.ppl);
iv.setImageResource(R.drawable.cw);
}
else {
setContentView(R.layout.ppl);
iv.setImageResource(R.drawable.no);
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}
How should I do it? 我该怎么办? Or how do I fix it? 或如何解决?
Thank you, I'm pretty new at this. 谢谢,我对此很陌生。
Spawn off a new activity to view the image. 产生一个新的活动来查看图像。
Intent intent = new Intent(this, ImageViewerActivity.class);
intent.putExtra(EXTRA_IMAGE_ID, R.drawable.boy); //could also pass the resource
startActivity(intent);
and then 接着
public class ImageViewerActivity extends Activity {
private static final String EXTRA_IMAGE_ID = "ImageViewerActivity.EXTRA_IMAGE_ID";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle extras = getIntent().getExtras();
if (extras == null) {
return;
}
Resource resId = extras.getInt(EXTRA_IMAGE_ID);
mImageView.setImageDrawable(resId);
}
You've extended ListActivity
, so it means that the activity only has a ListView and nothing more. 您已经扩展了ListActivity
,因此这意味着该活动仅具有一个ListView,仅此而已。
You can do any of the next things: 您可以执行以下任何操作:
Create a new activity that will show the image. 创建一个将显示图像的新活动。
Use fragments, while one shows the ListView and one shows the image , and switch between them. 使用片段,一个显示ListView,另一个显示图像,然后在它们之间切换。
Use setContentView twice, one before choosing (to show the ListView) and one after (to show the image). 使用setContentView两次,一次选择(显示ListView),然后一次选择(显示图像)。
Use a different layout, which you switch between the ListView and the ImageView (via visibility or via using ViewSwitcher) . 使用不同的布局,可以在ListView和ImageView之间切换(通过可见性或使用ViewSwitcher)。
I'm sure there are other ways too. 我敢肯定,还有其他方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.