如何从adapter.java内的列表视图中设置onclick ImageView

时间:2018-09-13 23:37:55

标签: java android listview imageview baseadapter

我的问题是关于“如何从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;
    }
}

当我从列表视图中设置OnClick消息(ImageView)时说

“致命异常:主要”

“无法启动活动”

“ java.lang.NullPointerException”


MainActvity.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();
}

有人可以帮助我如何从列表视图设置onClick消息(ImageView)?谢谢

3 个答案:

答案 0 :(得分:1)

尝试一下,

  

holder.vehicle_address.setOnClickListener(new View.OnClickListener(){               @Override               public void onClick(View v){

            Toast.makeText(context,"Click",Toast.LENGTH_SHORT).show();
        }
    });

答案 1 :(得分:1)

我认为您应该在适配器内部设置de click ...在每个项目上,活动在开始时就不会找到该视图

类似

pv.message.setClickable(true);
pv.message.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            Toast.makeText(arg0.getContext(), "sample", Toast.LENGTH_SHORT).show();
        }
    });

答案 2 :(得分:1)

尝试:pv.message.setOnClickListener(new View.OnClickListener() { @Override public void OnClick(View v) { // Your Code } }); 。 或者:

{{1}}