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