How to get a single SQLite row data with its id?

时间:2019-04-08 13:51:02

标签: android sqlite android-listview android-sqlite android-alertdialog

I have successfully retrieved SQLite data to ListView. Now I am trying to implement OnItemClickListener to show the data in Dialog but I am getting this error

2019-04-08 18:42:53.020 20591-20591/com.app E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.app, PID: 20591
    java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.File android.content.Context.getDatabasePath(java.lang.String)' on a null object reference
        at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:292)
        at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:262)
        at com.app.SQLiteAdaptor.GetUserByUserId(SQLiteAdaptor.java:99)
        at com.app.OrderHistory$1.onItemClick(OrderHistory.java:64)
        at android.widget.AdapterView.performItemClick(AdapterView.java:318)
        at android.widget.AbsListView.performItemClick(AbsListView.java:1181)
        at android.widget.AbsListView$PerformClick.run(AbsListView.java:3165)
        at android.widget.AbsListView$3.run(AbsListView.java:4147)
        at android.os.Handler.handleCallback(Handler.java:794)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:173)
        at android.app.ActivityThread.main(ActivityThread.java:6634)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:822)

This is where I need to retrieve row


 @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {


                final Dialog fullscreenDialog = new Dialog(getApplicationContext(), R.style.Dialog);
                    fullscreenDialog.setContentView(R.layout.dialog_oh);


                SQLiteAdaptor db = new SQLiteAdaptor(c);
                ArrayList<HashMap<String, String>> userList = db.GetUserByUserId(i); //Line 64
                HashMap<String, String> hashDetails = userList.get(0);

                     //Use this index accordingly
                    tvit.setText(hashDetails.get("item"));
                    tvpr.setText(hashDetails.get("price"));
                    tvqu.setText(hashDetails.get("quantity"));
                    tvad.setText(hashDetails.get("address"));
                    tvna.setText(hashDetails.get("name"));
                    tvem.setText(hashDetails.get("email"));
                    tvtim.setText(hashDetails.get("time"));

fullscreenDialog.show();
}
});

Here is the SQLiteAdaptor get row data by id method snippet


 public ArrayList<HashMap<String, String>> GetUserByUserId(int userid ){
        SQLiteDatabase db = this.getReadableDatabase(); \\Line 99
        ArrayList<HashMap<String, String>> userList = new ArrayList<>();
        String  query = "SELECT item, price, quantity, name, address, email, orderid FROM "+ Table_Name;

        Cursor cursor = db.query(Table_Name, new String[]{COL_2, COL_3, COL_4, COL_5, COL_6, COL_7, COL_8, COL_9}, COL_1+ "=?",new String[]{String.valueOf(userid)},null, null, null, null);
        if (cursor.moveToNext()){
            HashMap<String,String> user = new HashMap<>();
            user.put("item",cursor.getString(cursor.getColumnIndex(COL_2)));
            user.put("price",cursor.getString(cursor.getColumnIndex(COL_3)));
            user.put("quantity",cursor.getString(cursor.getColumnIndex(COL_4)));
            user.put("name",cursor.getString(cursor.getColumnIndex(COL_5)));
            user.put("address",cursor.getString(cursor.getColumnIndex(COL_6)));
            user.put("email",cursor.getString(cursor.getColumnIndex(COL_7)));
            user.put("orderid",cursor.getString(cursor.getColumnIndex(COL_8)));
            user.put("time",cursor.getString(cursor.getColumnIndex(COL_9)));
            userList.add(user);
        }
        return  userList;
    }

And it is not null pointer exception because I am nit getting any error while retrieving data in Listview

Or any other way to how to retrieve row with its id

Any help and solutions are appreciated

Thanks in advance

1 个答案:

答案 0 :(得分:1)

尝试

SQLiteAdaptor db = new SQLiteAdaptor(view.getContext());