我想启动相机,Listview项目中使用的onClick事件。但是显示方法startActivity(Intent)未定义类型new View.OnClickListener()。我的适配器代码在这里。
public class MessageListAdapter extends ArrayAdapter<RowItem1> {
Context context;
public MessageListAdapter(Context context, int resourceId,
List<RowItem1> items) {
super(context, resourceId, items);
this.context = context;
}
/*private view holder class*/
private class ViewHolder {
ImageView imagep ;
TextView name;
TextView date1;
TextView time;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
RowItem1 rowItem = getItem(position);
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.single_list_message, null);
ImageView imgcamera= (ImageView) convertView.findViewById(R.id.img_camera);
imgcamera.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
startActivity(intent);
}
});
holder = new ViewHolder();
holder.name = (TextView) convertView.findViewById(R.id.txt_name);
holder.date1 = (TextView) convertView.findViewById(R.id.txt_Date);
holder.time = (TextView) convertView.findViewById(R.id.txt_time);
holder.imagep = (ImageView) convertView.findViewById(R.id.img_person);
holder.imagecam = (ImageView) convertView.findViewById(R.id.img_camera);
convertView.setTag(holder);
} else
holder = (ViewHolder) convertView.getTag();
holder.name.setText(rowItem.getName());
holder.date1.setText(rowItem.getDate1());
holder.imagep.setImageResource(rowItem.getImagep());
holder.imagecam.setImageResource(rowItem.getImagecam());
holder.time.setText(rowItem.getTime());
return convertView;
}
}
答案 0 :(得分:0)
我建议您将imgcamera.setOnClickListener
移到if
块之外。此回调仅在convertview
为空(仅限第一次)时运行,但每次都应该运行,而不管convertview
的值。
希望有所帮助。
答案 1 :(得分:0)
在线 imgcamera.setOnClickListener(new OnClickListener(){ 你还没有添加view.onclickListner。 有关为imageview添加onclick listner的详细信息,请查看此内容。 How to Set onClicklistener Method of ImageView in Android? imgcamera.setOnClickListener(new View.OnClickListener(){
答案 2 :(得分:-1)
在startActivity context.startActivity