Java Android是否基于listview posistion在另一个活动中更改图像?
我有一个包含100个左右项目的列表视图,单击每个项目会打开另一个包含按钮和图像视图的活动。计划是为listview中的每个位置提供不同的图片 因此,我想知道,当用户单击listview中的某个项目时,其他活动中的imageview是否会更改其图像?(来自可绘制文件夹) 例如Java Android是否基于listview posistion在另一个活动中更改图像?,java,android,eclipse,listview,imageview,Java,Android,Eclipse,Listview,Imageview,我有一个包含100个左右项目的列表视图,单击每个项目会打开另一个包含按钮和图像视图的活动。计划是为listview中的每个位置提供不同的图片 因此,我想知道,当用户单击listview中的某个项目时,其他活动中的imageview是否会更改其图像?(来自可绘制文件夹) 例如 我真的不想做100种不同的活动。在意图中传递id。在列表中,活动的onItemClick侦听器具有以下内容: startActivity(new Intent(this, DisplayImageActivity.class
我真的不想做100种不同的活动。在意图中传递id。在列表中,活动的onItemClick侦听器具有以下内容:
startActivity(new Intent(this, DisplayImageActivity.class).putExtra("imageId", clickedImageId)); //clickedImageId should be R.drawable.my_pic_20 or something
然后在其他活动的onCreate中,将其拉出并设置:
onCreate {
final int imageId = getIntent().getExtra("imageId");
imageView.setImageResource(imageId);
...
}
下面是另一篇关于传递额外信息的SO帖子:您可以使用
intent将ListView中选定行的位置发送给另一个活动。putExtra
作为:
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setClass(ListActivityw.this,SecondActivity.class);
intent.putExtra("position",Integer.toString(arg2));
ListActivityw.this.startActivity(intent);
}
});
而不是使用if-else条件使数组可绘制,这将像
int[]myImageList=newint[]{R.drawable.thingOne,R.drawable.thingTwo};
然后在列表项上单击发送意图,如
@凌驾
公共单击(适配器视图arg0,视图arg1,内部位置,
长id){
//TODO自动生成的方法存根
//游戏计数
意图b=新意图(配置游戏列表,这个,UpdateGame.class);
b、 putExtra(“名称”,myImageList.get[position]);
b、 设置标志(意图、标志、活动、清除、顶部);
完成();
星触觉(b);
}
并将其视为
int imageID=getIntent().getIntExtra(“名称”,1);
并设置图像
作为
myImageView.setImageResource(imageID);
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setClass(ListActivityw.this,SecondActivity.class);
intent.putExtra("position",Integer.toString(arg2));
ListActivityw.this.startActivity(intent);
}
});
//obtain Intent Object send from SenderActivity
Intent intent = this.getIntent();
/* Obtain String from Intent */
if(intent !=null)
{
String position = intent.getExtras().getString("position");
(if position == "1") {
imageview src = "pic 1;
}
///your code here
}
else
{
// DO SOMETHING HERE
}
Rather using if else condition make array of drawable which will be easy to use like
int[] myImageList = new int[]{R.drawable.thingOne, R.drawable.thingTwo};
and on list item click send the intent like
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long id) {
// TODO Auto-generated method stub
// game_count
Intent b = new Intent(Configure_Game_List.this, UpdateGame.class);
b.putExtra("Name", myImageList.get [position]);
b.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
finish();
startActivity(b);
}
and recieve it as
int imageID = getIntent().getIntExtra("Name", 1);
and set the image
as
myImageView.setImageResource(imageID );