如何从sqlite db中删除记录并在删除按钮上单击listview。我有nullpointer异常

时间:2016-02-14 10:03:08

标签: android sqlite

> package com.flyct.flyctsofttech.sms;
> 
> import android.content.Context; 
  import android.content.DialogInterface; import android.app.AlertDialog;
> 
> import android.database.sqlite.SQLiteDatabase;
  import android.view.LayoutInflater; import android.view.View;
  import android.view.ViewGroup; import android.app.AlertDialog;
  import android.widget.BaseAdapter; import android.widget.Button;
  import android.widget.TextView; import android.widget.Toast;
> 
> import java.util.ArrayList;
> 
> // Created by FLYCT Softtech on 04/02/2016.
> 
> public class ContactAdapter extends BaseAdapter {
> 
>     private Context mContext;
>     private ArrayList<String> id;
>     private ArrayList<String> Name;
>     private ArrayList<String> Phone;
> 
>     private AlertDialog.Builder build;
>     private SqlDbHelper mHelper;
>     private SQLiteDatabase dataBase;
> 
>     public ContactAdapter(Context c, ArrayList<String> id, ArrayList<String> name, ArrayList<String> phone) {
> 
>         this.mContext = c;
>         this.id = id;
>         this.Name = name;
>         this.Phone = phone;
>     }
> 
>     public int getCount() {
>         // TODO Auto-generated method stub
>         return id.size();
>     }
> 
>     public Object getItem(int position) {
>         // TODO Auto-generated method stub
>         return null;
>     }
> 
>     public long getItemId(int position) {
>         // TODO Auto-generated method stub
>         return 0;
>     }
> 
>     public View getView(final int pos, View child, ViewGroup parent) {
> 
>         final Holder mHolder;
>         LayoutInflater layoutInflater;
> 
>         if (child == null) {
> 
>             layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
>             child = layoutInflater.inflate(R.layout.contact_item_row, null);
> 
>             mHolder = new Holder();
> 
>             mHolder.tv_id = (TextView) child.findViewById(R.id.tv_grp_name);
>             mHolder.tv_Name = (TextView) child.findViewById(R.id.tv_cnt_name);
>             mHolder.tv_phone = (TextView) child.findViewById(R.id.tv_cnt_no);
>             mHolder.btn_Edit = (Button ) child.findViewById(R.id.btn_edit);
>             mHolder.btn_Remove = (Button) child.findViewById(R.id.btn_remove);
>             child.setTag(mHolder);
> 
>         } else {
>             mHolder = (Holder) child.getTag();
>         }
> 
>         mHolder.tv_id.setText(id.get(pos));
>         mHolder.tv_Name.setText(Name.get(pos));
>         mHolder.tv_phone.setText(Phone.get(pos));
> 
>         mHolder.btn_Edit.setTag(pos);
>         mHolder.btn_Remove.setTag(pos);
> 
> 
>         mHolder.btn_Edit.setOnClickListener(new View.OnClickListener() {
>             @Override
>             public void onClick(View v) {
> 
> 
>             }
>         });
> 
>         mHolder.btn_Remove.setOnClickListener(new View.OnClickListener() {
>             @Override
>             public void onClick(View v) {
> 
>                 final int pos = (Integer)v.getTag();
> 
>                 build = new AlertDialog.Builder(mContext);
>                 build.setTitle("Delete " +  mHolder.tv_Name);
>                 build.setMessage("Are you sure to delete.?");
>                 build.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
> 
>                     public void onClick(DialogInterface dialog,   int which) {
> 
>                         //dataBase.execSQL("DELETE FROM "+SqlDbHelper.TABLE_NAME+" WHERE "+SqlDbHelper.KEY_ID+ "='"+pos+"'");
> 
>                         Toast.makeText(mContext, mHolder.tv_Name + " is deleted.", Toast.LENGTH_SHORT).show();
>                         dataBase.delete(SqlDbHelper.TABLE_NAME, SqlDbHelper.KEY_ID + "="+ id.get(pos), null);
> 
>                         notifyDataSetChanged();
> 
>                         Contacts c = new Contacts();
>                         c.displayData();
>                         dialog.cancel();
>                     }
>                 });
> 
>                 build.setNegativeButton("No",
>                         new DialogInterface.OnClickListener() {
> 
>                             public void onClick(DialogInterface dialog,
>                                                 int which) {
>                                 dialog.cancel();
>                             }
>                         });
>                 AlertDialog alert = build.create();
>                 alert.show();
>             }
>         });
> 
>         return child;
>     }
> 
>     public class Holder {
>         TextView tv_id;
>         TextView tv_Name;
>         TextView tv_phone;
>         Button btn_Edit;
>         Button btn_Remove;
>     } }

0 个答案:

没有答案