SetImageDrawable列表视图 [英] SetImageDrawable in a list view
问题描述
在我的列表视图中,我想使它这样它会改变图像视图我所做的绘制,但我每次运行它时,我得到一个错误。
In my list view, I want to make it so that it will change the drawable in the image view i made, but I get an error every time I run it.
ImageView Player;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Player = (ImageView)findViewById(R.id.bob);
setListAdapter(new ArrayAdapter<String>(this, R.layout.pointguards, PointGuards));
final ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View view, int position, long id) {
String item = (String) lv.getAdapter().getItem(position);
//decide how to launch `Intent` depending on `item`
if (item == "bob"){
Intent intent = new Intent("com.example.nba.playerinfo");
startActivity(intent);
Player.setImageDrawable(R.drawable.joe);
}
};
这完美运行,但是当我添加了setImageDrawable崩溃。
编辑:绘制,我想改变的是在我准备
This runs perfectly, but when I add the setImageDrawable it crashes. The drawable that I would like to change is in the intent that I am going to
推荐答案
您需要设置在活动图像绘制
,你与你的<$ C $启动C>意图,而不是在的ListView
。你可以在 intent.putExtras(名,名称)传递的名称;
。然后在活动
使用基于您从意图获取名称
setImageDrawable
code>额外的费用。
You need to set the image drawable in the Activity
that you are launching with your Intent
, not in the ListView
. You could pass the name in with intent.putExtras("name", name);
. Then in the Activity
use setImageDrawable
based on the name that you get from the Intent
extra.
修改:
意图在活动
检索数据>
Intent intent = getIntent();
String name = intent.getStringExtra("name");
然后像这样设置播放器的ImageView
:
if (name.equalsIgnoreCase("joe")
Player.setImageDrawable(R.drawable.joe);
这篇关于SetImageDrawable列表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!