0

我有一个包含 100 个左右项目的列表视图,每个项目在单击时都会打开另一个具有按钮和图像视图的活动。计划是为列表视图中的每个位置提供不同的图片。

所以,我想知道当用户单击列表视图中的项目以使其他活动中的图像视图更改其图像时,你有什么办法吗?(来自可绘制文件夹)

例如,

(if position == 1) {

     otheractivity imageview src = "pic 1;

}


(if position == 2) {

      otheractivity imageview src = "pic 2;

}

我真的不想做100个不同的活动。

4

3 回答 3

1
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 );
于 2012-07-15T06:40:05.120 回答
1

在 Intent 中传递 id。在您的 List Activity 的 onItemClick 侦听器中有以下内容:

startActivity(new Intent(this, DisplayImageActivity.class).putExtra("imageId", clickedImageId)); //clickedImageId should be R.drawable.my_pic_20 or something

然后在其他 Activity 的 onCreate 中将其拉出并设置:

onCreate {
  final int imageId = getIntent().getExtra("imageId");
  imageView.setImageResource(imageId);
  ...
}

这是另一篇关于传递额外内容的 SO 帖子:如何从 Android 上的意图中获取额外数据?

于 2012-07-15T06:04:37.020 回答
0

You can send position of selected row in ListView to another Activity using intent.putExtra as:

@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); 
}
});

In Second Activity:

//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
  }
于 2012-07-15T06:11:34.793 回答