【发布时间】:2018-09-13 23:37:55
【问题描述】:
我的问题是关于“如何从 ListView 设置 OnClick ImageView”
Adapter.java
public class Adapter extends BaseAdapter implements OnClickListener{
Context context;
ArrayList<Contact> list;
LayoutInflater inflater;
public Adapter(Context context, ArrayList<Contact> list) {
super();
this.context = context;
this.list = list;
this.inflater =
LayoutInflater.from(context);
}
public View getView(int arg0, View arg1, ViewGroup arg2) {
ContactView pv=null;
if(arg1==null)
{
arg1= inflater.inflate(R.layout.additem, null);
pv = new ContactView();
pv.iv=(ImageView) arg1.findViewById(R.id.imageView1);
pv.name=(TextView) arg1.findViewById(R.id.textView1);
pv.number=(CheckedTextView) arg1.findViewById(R.id.checkedTextView1);
pv.message=(ImageView) arg1.findViewById(R.id.imageView2);
arg1.setTag(pv);
}
else
{
pv=(ContactView) arg1.getTag();
pv.iv.setImageURI(
list.get(arg0).getImage());
pv.name.setText( list.get(arg0).getName());
pv.number.setText( String.valueOf(list.get(arg0).getNumber()));
pv.message=(ImageView)
arg1.findViewById( R.id.imageView2);
pv.message.setOnClickListener( this);
return arg1;
}
}
static class ContactView{
ImageView iv;
TextView name;
CheckedTextView number;
ImageView message;
}
}
当我从 listview 中设置 OnClick 消息(ImageView)时,它会说
“致命异常:主要”
“无法启动活动”
“java.lang.NullPointerException”
.
MainActivity.java
ArrayList<Contact> list = new ArrayList<Contact>();
Adapter adapter;
ImageView message;
protected void onCreate(Bundle savedInstanceState) {
this.lv = (ListView) this.findViewById(R.id.listView1);
this.adapter=new MyAdapter(this,list);
this.lv.setAdapter(adapter);
}
public void onClick(View arg0) {
// TODO Auto-generated method stub
Toast.makeText(this, "sample", Toast.LENGTH_SHORT).show();
}
有人可以帮助我如何从 listview 设置 OnClick 消息(ImageView)?谢谢
【问题讨论】:
标签: java android listview imageview baseadapter