如何在OnItemClickListener范围之外获取ImageView?
mListView2.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
imageView2 = (ImageView) view.findViewById(R.id.applist_item_image);
if (switches==true) {
imageView2.setImageResource(R.drawable.on);
preferences.edit().putBoolean("tgpref", true).commit();
switches = false;
} else {
imageView2.setImageResource(R.drawable.off);
preferences.edit().putBoolean("tgpref", false).commit();
switches = true;
}
}
});
答案 0 :(得分:1)
点击
ImageView imageView2 = (ImageView) view.findViewById(R.id.applist_item_image);
在onItemClickListener范围之前的外部。
换句话说,把它放在洞类活动
的范围内setContentView(R.layout.activity_main);
ImageView imageView2 = (ImageView) findViewById(R.id.applist_item_image);
String[] itemname = { "Wi-Fi", "Bluetooth", "Mobile Data", };
Integer[] imgid = { R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, };
ImageView mListView2 = (ListView) findViewById(R.id.lvAppList1);
preferences=PreferenceManager.getDefaultSharedPreferences(this);
finalRowActivityadapter=newRowActivity(this,itemname,imgid);
mListView2.setAdapter(adapter);
mListView2.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//here you can now access imageView2
}
答案 1 :(得分:1)
尝试在onCreate中使用inflater
...您必须使用addView()将ImageView添加到当前布局。
LayoutInflater inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View vi = inflater.inflate(R.layout.activity_row, null); //activity_row.xml is your file.
imageView2 = (ImageView) view.findViewById(R.id.applist_item_image);
答案 2 :(得分:0)
您可以将imageview全局声明为: - ImageView imageview