当我点击自定义列表视图删除按钮时,我得到了NullPointerException

时间:2015-08-31 13:16:59

标签: android

当我点击自定义列表视图删除按钮时,我收到NullPointerException

public class ContactListAdapter extends BaseAdapter
    {
        private ContactListAdapter adapter;
        ListView listView;
        SecondActivity sc;
        Context context;
        private ArrayList<ContactList> contactList;
        private LayoutInflater listItemsLayoutInflater;
        Cursor cursor;
        UserDetailsDB db;
        Bundle dataBundle;
        public ContactListAdapter(Context context,ArrayList<ContactList> contactList)
        {
        this.contactList=contactList;
        listItemsLayoutInflater=LayoutInflater.from(context);
        this.context=context;
        this.adapter=this;
        }
        @Override
        public int getCount() 
        {
            return contactList.size();
        }

        @Override
        public ContactList getItem(int position) 
        {
            return contactList.get(position);
        }
        @Override
        public long getItemId(int position) 
        {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) 
        {
            ViewHolder viewHolder;
            if(convertView==null)
            {
                viewHolder=new ViewHolder();
                convertView=this.listItemsLayoutInflater.inflate(R.layout.list_items,parent,false);
                viewHolder.imageView=(ImageView)convertView.findViewById(R.id.image);
                viewHolder.name=(TextView)convertView.findViewById(R.id.name_editText);
                viewHolder.edit=(Button)convertView.findViewById(R.id.edit_button);
                viewHolder.delete=(Button)convertView.findViewById(R.id.delete_button);
                convertView.setTag(viewHolder);
            }
            else
            {
                viewHolder=(ViewHolder)convertView.getTag();
            }
            ContactList contact=contactList.get(position);
            viewHolder.name.setText(contact.getName());
            viewHolder.edit.setOnClickListener(new OnClickListener() 
            {
                @Override
                public void onClick(View v)
                {
                    int id=(int)getItemId(position);
                    id=id+1;
                    dataBundle = new Bundle();
                    dataBundle.putInt("id",id);
                    Intent intent=new Intent(context,ThirdActivity.class);
                    intent.putExtras(dataBundle);
                    context.startActivity(intent);  
                }
            });
            viewHolder.delete.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) 
                {
                    int id=(int)getItemId(position);
                    id=id+1;
                    db.deleteContact(id);
                    db.close();
                    adapter.notifyDataSetChanged();
                }
            });
            return convertView;
        }
        private class ViewHolder
        {
            ImageView imageView;
            TextView name;
            Button edit;
            Button delete;
        }
    }

0 个答案:

没有答案