Domanda

So i have populated my listview using a BaseAdapter and I have set a textview as a button. I want to use that button to delete the row in the database and then update the listview

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;
    }

}

Here is my 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

    }

For some reason, I am having trouble coming up with what I need to put into the listener to correctly trigger the deleteGasLog method from the helper.

if i just do db.deleteGasLog(deleteId); I am getting a NPE error

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)
È stato utile?

Soluzione

You have to instantiate your MySQLiteOpenHelper object (db) in the constructor. This is now:

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;
        }

This is how it should be:

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
        }
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top