1

I'm unable to find a way to retrieve data from SQLite database to ListView once I save it, yup there are lots of ways out there, but I want to do it my way because I'm new to SQLite.

So, If anyone knows it here please help me out.

Here's the error

Error Log

So, here's what I've done.

DatabaseHelper.java

public class DatabaseHelper extends SQLiteOpenHelper {

    ByteArrayOutputStream byteArrayOutputStream;
    byte[] imagebyte;

    public DatabaseHelper(Context context) {
        super(context, "moderncampus", null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table hodtable(firstname TEXT, lastname TEXT, email EMAIL)");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("drop table if exists hodtable");
        onCreate(db);
    }

    public boolean insert(String name, String last, String email){
        SQLiteDatabase db=this.

        ContentValues contentValues = new ContentValues();
        contentValues.put("firstname", name);
        contentValues.put("lastname", last);
        contentValues.put("email", email);

        long ins = db.insert("hodtable", null, contentValues);
        if (ins == -1)
            return false;
        else
            return true;
    }

    public Cursor getdetails(){
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery("Select firstname, lastname, email from hodtable", null);

        return cursor;
    }

}

HodListAdapter.java - Custom ListView Adapter

public class HodListAdapter extends ArrayAdapter<String> {

    private final Activity context;
    private final ArrayList<String> title, title2;
    private final ArrayList<String> subtitle;

    public HodListAdapter(Activity context, ArrayList<String> title, ArrayList<String> title2, ArrayList<String> subtitle/*, Integer[] imgid*/){
        super(context, R.layout.hod_list, title);

        this.context = context;
        this.title = title;
        this.title2 = title2;
        this.subtitle = subtitle;
    }

    public View getView(int position, View view, ViewGroup parent){
        final viewHolder holder;
        LayoutInflater inflater;
        if (view == null){
            inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.hod_list, null, true);

            holder = new viewHolder();
            holder.name = (TextView) view.findViewById(R.id.title);
            holder.last = (TextView) view.findViewById(R.id.title2);
            ImageView imageView = (ImageView) view.findViewById(R.id.icon);
            holder.email = (TextView) view.findViewById(R.id.subtitle);
            view.setTag(holder);
        } else {
            holder = (viewHolder) view.getTag();
        }

        holder.name.setText(title.get(position));
        holder.last.setText(title2.get(position));
        holder.email.setText(subtitle.get(position));

        return view;
    }

    public class viewHolder{
        TextView name;
        TextView last;
        TextView email;
    }

}

CollegeAdmin.java - Activity

public class CollegeAdmin extends AppCompatActivity {

    DatabaseHelper db = new DatabaseHelper(this);
    SQLiteDatabase sqLiteDatabase;

    ArrayList<String> listitem;

    FloatingActionButton fabplus, fabdept;
    Animation fabOpen, fabClose, fabRC, fabRAC;

    private ListView list;

    ArrayList<String> title = new ArrayList<String>();
    ArrayList<String> subtitle = new ArrayList<String>();
    ArrayList<String> title2 = new ArrayList<String>();

            boolean isOpen = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_college_admin);
        getSupportActionBar().setTitle("Admin");

        fabplus = findViewById(R.id.fab_plus);
        fabdept = findViewById(R.id.fab_department);

        fabOpen = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.fab_open);
        fabClose = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.fab_close);
        fabRC = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.rotate_clockwise);
        fabRAC = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.rotate_anticlock);

        fabplus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isOpen){
                    fabdept.startAnimation(fabClose);
                    fabplus.startAnimation(fabRAC);
                    fabdept.setClickable(false);
                    isOpen = false;
                } else {
                    fabdept.startAnimation(fabOpen);
                    fabplus.startAnimation(fabRC);
                    fabdept.setClickable(true);
                    isOpen = true;
                }
            }
        });

        listitem = new ArrayList<>();

    }

    @Override
    protected void onPostResume() {
        viewData();
        super.onPostResume();
    }

    private void viewData() {
        sqLiteDatabase = db.getReadableDatabase();
        Cursor cursor = sqLiteDatabase.rawQuery("SELECT * FROM hodtable", null);
        title.clear();
        title2.clear();
        subtitle.clear();
        if (cursor.getCount() == 0){
            Toast.makeText(this, "No Entries", Toast.LENGTH_SHORT).show();
        } else {
            if (cursor.moveToFirst()){
                do {
                    title.add(cursor.getString(cursor.getColumnIndex("firstname")));
                    subtitle.add(cursor.getString(cursor.getColumnIndex("lastname")));
                    title2.add(cursor.getString(cursor.getColumnIndex("email")));
                } while (cursor.moveToNext());
            }

            HodListAdapter adapter = new HodListAdapter(this, title, title2, subtitle);
            list.setAdapter(adapter);
            cursor.close();
        }
    }
Cybertronian
  • 473
  • 1
  • 8
  • 17
  • You forgot to instantiate your `DatabaseHelper` in `CollegeAdmin`; e.g., `db = new DatabaseHelper(this);` – Mike M. Aug 27 '19 at 11:27
  • I did as you said but it still shows same. [Mike M](https://stackoverflow.com/users/2850651/mike-m) – Cybertronian Aug 27 '19 at 11:32
  • Exactly where and how did you do it? That is, what is the exact line of code you used, and where did you put it? Btw, it's @MikeM. to notify me. The link won't do it. – Mike M. Aug 27 '19 at 11:41
  • I've update the query you can check there @MikeM. – Cybertronian Aug 27 '19 at 11:50
  • That's a different error. You forgot to assign `list`; e.g., `list = findViewById(R.id.whatever);`. Please have a read through the linked duplicate. It'll help you to understand what a `NullPointerException` means, and how to fix it. – Mike M. Aug 27 '19 at 11:55
  • 2
    never mind I figured it out, thank you all, yup that was the missing part @MikeM. – Cybertronian Aug 27 '19 at 11:56
  • hey @MikeM. by any chance do you know same question but with image retrieval, if you know then please provide a link, it'll be really helpful – Cybertronian Aug 27 '19 at 12:13
  • I'm not sure what you mean. Images in the `ListView`? If so, where are they coming from? – Mike M. Aug 27 '19 at 12:15
  • I've already wrote a code to upload image on sqlite database and all I want to do now is to retrieve it from there @MikeM. – Cybertronian Aug 27 '19 at 12:33
  • I'm still not sure what you mean. You saved an image link in the database? Or you saved a byte array to the database? Or something else? – Mike M. Aug 27 '19 at 12:36
  • took bitmap of image, then converted to byte, and then uploaded the image to sqlite @MikeM. – Cybertronian Aug 27 '19 at 12:38
  • Well, you generally don't want to save images in a database, but you'll use the `Cursor#getBlob()` method to get your `byte[]` back. Then you could use `BitmapFactory.decodeByteArray()`, if you want simple, built-in stuff, but that can cause issues, especially in a `ListView`, where you'll have a bunch of images loaded at once. The usual recommendation is to use an image loading library like Glide or Picasso. There are thousands of examples out there for each. – Mike M. Aug 27 '19 at 12:46

0 Answers0