ListView not populating using Cursoradapter

33 Views Asked by At

I have an input page which submits 4 strings into the helper. The helper is supposed to get the data and put it into the listView.

When I insert my data, the app doesnt crash but it leads me to the listview page with no rows inflated. The 4 data inputs are all strings and is submitted through a button. I'm not too sure what is wrong with my code. Would appreciate some advice :)

listview code

public class MyActivitiesPage extends AppCompatActivity {
    private Cursor model = null;
    private ListView list;
    private dbHandler helper = null;
    private activityAdapter adapter = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_activities_page);

        helper = new dbHandler(this);
        list = findViewById(R.id.listView);
        model = helper.getall();
        adapter = new activityAdapter(this, model, 0);
        list.setAdapter(adapter);
    }
    
    public static class activityHolder {
        private TextView activitytext;
        private TextView datetext;
        private TextView timetext;
        private TextView addresstext;

        activityHolder(View row) {
            activitytext = row.findViewById(R.id.activitytext);
            datetext = row.findViewById(R.id.datetext);
            timetext = row.findViewById(R.id.timetext);
            addresstext = row.findViewById(R.id.addresstext);
        }

        void populateFrom(Cursor c, dbHandler helper){
            activitytext.setText(helper.getActi(c));
            datetext.setText(helper.getDate(c));
            timetext.setText(helper.getTime(c));
            addresstext.setText(helper.getAddr(c));
        }
    }

    class activityAdapter extends CursorAdapter {
        activityAdapter(Context context, Cursor cursor, int flags) {
            super(context, cursor, flags);
        }

        public void bindView(View view, Context context, Cursor cursor) {
            activityHolder holder = (activityHolder) view.getTag();
            holder.populateFrom(cursor, helper);
        }
        public View newView(Context context, Cursor cursor, ViewGroup parent) {
            LayoutInflater inflater = getLayoutInflater();
            View row = inflater.inflate(R.layout.row, parent, false);
            activityHolder holder = new activityHolder(row);
            row.setTag(holder);
            return (row);
        }
    }

helper code

public class dbHandler extends SQLiteOpenHelper {
        private Context context;
    private static final String DATABASE_NAME = "handler.db";
    private static final int SCHEMA_VERSION = 1;

    public dbHandler(@Nullable Context context){
        super(context, DATABASE_NAME, null, SCHEMA_VERSION);
        this.context = context;
    }

    public void onCreate(SQLiteDatabase db) {
        //Will be called ones when the database is not created
        db.execSQL("CREATE TABLE dbTable ( _id INTEGER PRIMARY KEY AUTOINCREMENT, acti TEXT, date TEXT, time TEXT, addr TEXT );");
    }

    public void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion) {
        //Will not be called upon until SCHEMA_VERSION increases
        //For future upgrades
    }

    public Cursor getall() {
        return (getReadableDatabase().rawQuery("SELECT * FROM dbTable", null));
    }


    public Cursor getById(String id) {
        String[] args = {id};
        return (getReadableDatabase().rawQuery("SELECT _id, acti, date, time, addr FROM dbTable WHERE _ID = ?", args));
    }

    public void insert(String acti, String date, String time, String addr) {
        ContentValues cv = new ContentValues();

        cv.put("acti", acti);
        cv.put("date", date);
        cv.put("time", time);
        cv.put("addr", addr);

        long result = getWritableDatabase().insert("dbTable", "firstvalue", cv);

        if(result == -1){
            Toast.makeText(context, "Failed", Toast.LENGTH_SHORT).show();
        }else {
            Toast.makeText(context, "Added Successfully!", Toast.LENGTH_SHORT).show();
        }
    }

    public void updateDetails(String id, String acti, String date, String time, String addr) {
        ContentValues cv = new ContentValues();
        String[] args = {id};

        cv.put("acti", acti);
        cv.put("date", date);
        cv.put("time", time);
        cv.put("addr", addr);

        getWritableDatabase().update("dbTable", cv, " _ID = ?", args);
    }

    public String getID(Cursor c) { return (c.getString(0));}

    public String getActi(Cursor c) { return (c.getString(1));}

    public String getDate(Cursor c) { return (c.getString(2));}

    public String getTime(Cursor c) { return (c.getString(3));}

    public String getAddr(Cursor c) { return (c.getString(4));}
}```
0

There are 0 best solutions below