The app: I have an app that creates multiple machines with:
- id, name and location
each of these machines I have to let the user input the income
- id, note, date, money, machines_id
The Problem: I need to populate all the notes from the matching machine_id to a ListView.
What I have tried: Creating DBHelper Method to get the info i need (Notes and Date).
- db.rawQuery("SELECT note, date FROM income WHERE machines_id = "+machinesId+"", null);
This currently works on a DB Browser for SQLite but i do not know how to add it to the ListView Adapter and set it to the corresponding TextView
My Question: What is the best approach to accomplish the union between the DBHelper Method and the Custom Adapter to get only the information required in the TextViews? (Date and Note from matching machines_id)
If you need any other class or have any feedback, please dont hesitate to indulge me!
DBHelper
public class DBHelpter extends SQLiteOpenHelper {
private static final String DB_NAME = "machines.db";
private static final int DB_VERSION = 1;
public static final String TABLE_MACHINES = "machines";
public static final String MACHINES_COLUMN_NAME = "name";
public static final String MACHINES_COLUMN_LOCATION = "location";
public static final String MACHINES_ID = "id";
public static final String TABLE_INCOME = "income";
public static final String INCOME_COLUMN_MONEY = "money";
public static final String INCOME_COLUMN_DATE = "date";
public static final String INCOME_COLUMN_NOTE = "note";
public static final String INCOME_ID = "id";
public static final String INCOME_COLUMN_MACHINES_ID = "machines_id";
private Context mContext;
public DBHelpter(Context context) {
super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String query1 = String.format("CREATE TABLE " + TABLE_MACHINES + "("
+ MACHINES_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ MACHINES_COLUMN_NAME + " TEXT NOT NULL, "
+ MACHINES_COLUMN_LOCATION + " TEXT NOT NULL)",
TABLE_MACHINES, MACHINES_COLUMN_NAME, MACHINES_COLUMN_LOCATION, MACHINES_ID);
String query2 = String.format("CREATE TABLE " + TABLE_INCOME + "("
+ INCOME_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ INCOME_COLUMN_MONEY + " REAL NOT NULL, "
+ INCOME_COLUMN_DATE + " DATE NOT NULL, "
+ INCOME_COLUMN_NOTE + " TEXT NOT NULL, "
+ INCOME_COLUMN_MACHINES_ID + " INTEGER NOT NULL)",
TABLE_INCOME, INCOME_ID, INCOME_COLUMN_MONEY, INCOME_COLUMN_DATE, INCOME_COLUMN_NOTE, INCOME_COLUMN_MACHINES_ID);
db.execSQL(query1);
db.execSQL(query2);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
String query1 = String.format("DROP TABLE IF EXISTS " + TABLE_MACHINES);
String query2 = String.format("DROP TABLE IF EXISTS " + TABLE_INCOME);
db.execSQL(query1);
db.execSQL(query2);
onCreate(db);
}
public void insertNewMachine(String name, String location){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(MACHINES_COLUMN_NAME, name);
values.put(MACHINES_COLUMN_LOCATION, location);
db.insertWithOnConflict(TABLE_MACHINES, null, values, SQLiteDatabase.CONFLICT_REPLACE);
db.close();
}
public void insertNewIncome(Double money, String date, String note, long machines_id){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(INCOME_COLUMN_MONEY, money);
values.put(INCOME_COLUMN_DATE, date);
values.put(INCOME_COLUMN_NOTE, note);
values.put(INCOME_COLUMN_MACHINES_ID, machines_id);
db.insertWithOnConflict(TABLE_INCOME, null, values, SQLiteDatabase.CONFLICT_REPLACE);
db.close();
}
public void getIncomeOfMachine(long machinesId){
SQLiteDatabase db = getReadableDatabase();
Cursor cursor = db.rawQuery("SELECT machines_id, SUM(money) AS total FROM income WHERE machines_id = "+machinesId+"", null);
cursor.moveToFirst();
double total_amount = cursor.getDouble(cursor.getColumnIndex("total"));
return total_amount;
}
public ArrayList<MachinesClass> getAllMachines(){
ArrayList<MachinesClass> machinesList = new ArrayList<>();
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery("SELECT * FROM "+ TABLE_MACHINES, null);
while (cursor.moveToNext()){
final long id = cursor.getLong(cursor.getColumnIndex(MACHINES_ID));
final String name = cursor.getString(cursor.getColumnIndex(MACHINES_COLUMN_NAME));
final String location = cursor.getString(cursor.getColumnIndex(MACHINES_COLUMN_LOCATION));
machinesList.add(new MachinesClass(id, name, location));
}
cursor.close();
db.close();
return machinesList;
}
public ArrayList<String> getInfoFromMachine(long machinesId){
ArrayList<String> all_notes = new ArrayList<>();
SQLiteDatabase db = getReadableDatabase();
Cursor cursor = db.rawQuery("SELECT note, date FROM income WHERE machines_id = "+machinesId+"",null);
cursor.moveToFirst();
while (cursor.moveToNext()){
String note = cursor.getString(cursor.getColumnIndex("note"));
String date = cursor.getString(cursor.getColumnIndex("date"));
all_notes.add(date);
all_notes.add(note);
}
db.close();
cursor.close();
return all_notes;
}
}
ListAdapter
public class ListAdapter extends BaseAdapter {
private Context mContext;
private DBHelpter mDBHelpter;
private ArrayList<IncomeClass> mList;
public ListAdapter(Context mContext, DBHelpter mDBHelper, ArrayList<IncomeClass> mList){
this.mContext = mContext;
this.mDBHelpter = mDBHelper;
this.mList = mList;
}
@Override
public int getCount() {
return mList != null ? mList.size(): 0;
}
@Override
public Object getItem(int position) {
return mList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
mDBHelpter = new DBHelpter(mContext);
SQLiteDatabase db = mDBHelpter.getReadableDatabase();
ViewHolder viewHolder;
if (convertView == null){
convertView = LayoutInflater.from(mContext).inflate(R.layout.notes_list, parent, false);
viewHolder = new ViewHolder(convertView);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
// Cursor info = mDBHelpter.getInfoFromMachine(currentNote.getId());
// viewHolder.mNote.setText(mDBHelpter.getInfoFromMachine(currentNote.getId()).toString());
// viewHolder.mNoteDate.setText(mDBHelpter.getInfoFromMachine(currentNote.getId()).toString());
return convertView;
}
private class ViewHolder {
private TextView mNote, mNoteDate;
public ViewHolder(View view){
mNote = (TextView) view.findViewById(R.id.tvNote);
mNoteDate = (TextView) view.findViewById(R.id.tvNoteDate);
}
}
}