使用OnclickListener在自定义适配器类中

时间:2015-06-02 07:19:39

标签: android android-intent listadapter

我必须在listview的自定义适配器类中使用onclick侦听器。但是每当我尝试点击它时它都会崩溃应用程序。

这是适配器类:: -

public class ClassAdapter extends BaseAdapter{

    ArrayList<Class_Beans> feedList;
    LayoutInflater inflater;
    Activity activity;

    Context context;
    ImageLoader imageloader;



    public ClassAdapter(ArrayList<Class_Beans> feedList, Activity activity) {
        // TODO Auto-generated constructor stub
        super();
        this.feedList = feedList;
        this.activity = activity;
        inflater = (LayoutInflater )activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        imageloader = ImageLoader.getInstance();
        imageloader.init(ImageLoaderConfiguration.createDefault(activity));
    }

    ********unimplemented methods calling********


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        View vi = convertView;
        ViewHolder holder;

        if(convertView==null)
            //convertView = inflater.inflate(R.layout.ozone_item, null);

            convertView = inflater.inflate(R.layout.ozone_item, null);
        holder = new ViewHolder();

        holder.tvDate = (TextView)convertView.findViewById(R.id.class_name);
        holder.tvContent = (TextView)convertView.findViewById(R.id.ozone_content);
        holder.tvUser = (TextView)convertView.findViewById(R.id.class_date);
        holder.image_user_image = (ImageView)convertView.findViewById(R.id.class_user_image);
        holder.postImage = (ImageView)convertView.findViewById(R.id.class_post_image);
        holder.replyEdittext = (EditText)convertView.findViewById(R.id.class_writesomething);



        holder.tvUser.setText(feedList.get(position).getName());
        holder.tvDate.setText(feedList.get(position).getCreated_date());
        holder.tvContent.setText(feedList.get(position).getContent());

        imageloader.displayImage(feedList.get(position).getUser_image(), holder.image_user_image);

         holder.replyEdittext.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                    Intent replyClassIntent =  new Intent(context,Reply_Class.class);

                    activity.startActivity(replyOzoneIntent);
            }
        });

        return convertView;
    }

    public class ViewHolder {
        public EditText replyEdittext;
        public ImageView image_user_image;
        public ImageView postImage;
        TextView tvContent , tvUser, tvDate;
    }

请告诉我我在这里犯了什么错误。

1 个答案:

答案 0 :(得分:1)

使用Activity代替上下文,因为Context为空,您没有在构造函数中为Context分配任何内容,但是您要为Activity分配值

holder.replyEdittext.setOnClickListener(new View.OnClickListener()
{
   @Override
   public void onClick(View v) 
    {
      Intent replyClassIntent =  new Intent(activity,Reply_Class.class); 
      activity.startActivity(replyOzoneIntent);
    }
 });