使用ListView中的删除按钮删除行

时间:2014-02-21 20:48:30

标签: java android listview nullpointerexception baseadapter

所以我使用BaseAdapter填充了我的listview,并将textview设置为按钮。我想使用该按钮删除数据库中的行,然后更新列表视图

public class HistoryAdapter extends BaseAdapter {

    private Context mContext;
    Cursor cursor;
    history historyClass = new history();
    MySQLiteHelper db = new MySQLiteHelper(mContext);

    public HistoryAdapter(Context context, Cursor cur){
        super();
        mContext = context;
        cursor = cur;
    }

    public int getCount(){
        // return the number of records in cursor
        return cursor.getCount();
    }

    // getView method is called for each item of ListView
    public View getView(final int position, View view, ViewGroup parent){

        // inflate the layout for each item of listView
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.history_list_item, null);

        // move the cursor to required position
        cursor.moveToPosition(position);

        final String id = cursor.getString(cursor.getColumnIndex("_id"));
        final long deleteId = Long.parseLong(id);

        // fetch the information for each card
        String pricePerGallon = cursor.getString(cursor.getColumnIndex("pricePerGallon"));
        String gallons = cursor.getString(cursor.getColumnIndex("gallons"));
        String odometer = cursor.getString(cursor.getColumnIndex("odometer"));
        String date = cursor.getString(cursor.getColumnIndex("date"));
        String filledOrNot = cursor.getString(cursor.getColumnIndex("filledOrNot"));
        String comments = cursor.getString(cursor.getColumnIndex("comments"));
        //String milesPerGallon = cursor.getString(cursor.getColumnIndex("miledPerGallon"));
        String totalSpent = cursor.getString(cursor.getColumnIndex("totalSpent"));

        // get the reference of TextViews
        TextView textViewPricePerGallon = (TextView) view.findViewById(R.id.cardPrice);
        TextView textViewGallons = (TextView) view.findViewById(R.id.cardGallons);
        TextView textViewOdometer = (TextView) view.findViewById(R.id.cardOdometer);
        TextView textViewDate = (TextView) view.findViewById(R.id.cardDate);
        TextView textViewFilledOrNot = (TextView) view.findViewById(R.id.cardFilledOrNot);
        TextView textViewComments = (TextView) view.findViewById(R.id.cardComments);
        //TextView textViewMilesPerGallon = (TextView) view.findViewById(R.id.mpg);
        TextView textViewTotalSpent = (TextView) view.findViewById(R.id.usd);
        TextView textViewDeleteButton = (TextView) view.findViewById(R.id.deleteButton);

        // Set the data to each TextView
        textViewPricePerGallon.setText(pricePerGallon);
        textViewGallons.setText(gallons);
        textViewOdometer.setText(odometer);
        textViewDate.setText(date);
        textViewFilledOrNot.setText(filledOrNot);
        textViewComments.setText(comments);
        //textViewMilesPerGallon.setText(milesPerGallon);
        textViewTotalSpent.setText(totalSpent);



        textViewDeleteButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
              Log.d("History Adapter", "" + deleteId);
              //need to delete here


            }
     });

        return view;

    }

    public Object getItem(int position){
        return position;
    }

    public long getItemId(int position){
        return position;
    }

}

这是我的 MySQLiteHelper.java

public void deleteGasLog(long id) {

        // 1. get reference to writable DB
        SQLiteDatabase db = this.getWritableDatabase();

        // 2. delete
        db.delete(TABLE_GASLOG, //table name
                KEY_ID + " = ?",  // selections
                new String[] { String.valueOf(id) }); //selections args

        // 3. close
        db.close();

        //log

    }

出于某种原因,我无法提出我需要放入侦听器以正确触发帮助程序中的deleteGasLog方法。

如果我只是做 db.deleteGasLog(deleteId); 我收到了NPE错误

02-21 15:22:11.694: E/AndroidRuntime(15020): java.lang.NullPointerException
02-21 15:22:11.694: E/AndroidRuntime(15020):    at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224)
02-21 15:22:11.694: E/AndroidRuntime(15020):    at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
02-21 15:22:11.694: E/AndroidRuntime(15020):    at com.rcd.fuelr.MySQLiteHelper.deleteGasLog(MySQLiteHelper.java:165)
02-21 15:22:11.694: E/AndroidRuntime(15020):    at com.rcd.fuelr.HistoryAdapter$1.onClick(HistoryAdapter.java:80)

1 个答案:

答案 0 :(得分:2)

您必须在构造函数中实例化MySQLiteOpenHelper对象(db)。 现在是:

private Context mContext; //Help I'm null
        Cursor cursor;
        history historyClass = new history();
        MySQLiteHelper db = new MySQLiteHelper(mContext); //null is inserted here

        public HistoryAdapter(Context context, Cursor cur){
            super();
            mContext = context;
            cursor = cur;
        }

应该是这样的:

private Context mContext; //Help I'm null
        Cursor cursor;
        history historyClass = new history();
        MySQLiteHelper db; 

        public HistoryAdapter(Context context, Cursor cur){
            super();
            mContext = context;
            cursor = cur;
            db = new MySQLiteHelper(context); //not null anymore
        }