title: how to retrieve data from sqlite to ListView
body: <p>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.</p>
  
  <p>So, If anyone knows it here please help me out.</p>
  
  <p>Here's the error</p>
  
  <p><a href="https://i.stack.imgur.com/iXLOH.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/iXLOH.jpg" alt="Error Log"></a></p>
  
  <p>So, here's what I've done.</p>
  
  <p>DatabaseHelper.java</p>
  
  <pre><code>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;
      }
  
  }
  </code></pre>
  
  <p>HodListAdapter.java  - Custom ListView Adapter</p>
  
  <pre><code>public class HodListAdapter extends ArrayAdapter&lt;String&gt; {
  
      private final Activity context;
      private final ArrayList&lt;String&gt; title, title2;
      private final ArrayList&lt;String&gt; subtitle;
  
      public HodListAdapter(Activity context, ArrayList&lt;String&gt; title, ArrayList&lt;String&gt; title2, ArrayList&lt;String&gt; 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;
      }
  
  }
  </code></pre>
  
  <p>CollegeAdmin.java - Activity</p>
  
  <pre><code>public class CollegeAdmin extends AppCompatActivity {
  
      DatabaseHelper db = new DatabaseHelper(this);
      SQLiteDatabase sqLiteDatabase;
  
      ArrayList&lt;String&gt; listitem;
  
      FloatingActionButton fabplus, fabdept;
      Animation fabOpen, fabClose, fabRC, fabRAC;
  
      private ListView list;
  
      ArrayList&lt;String&gt; title = new ArrayList&lt;String&gt;();
      ArrayList&lt;String&gt; subtitle = new ArrayList&lt;String&gt;();
      ArrayList&lt;String&gt; title2 = new ArrayList&lt;String&gt;();
  
              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&lt;&gt;();
  
      }
  
      @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();
          }
      }
  </code></pre>