该应用:我有一个可以创建多台计算机的应用:
我必须让这些机器输入收入
问题:我需要将匹配的machine_id中的所有注释填充到 ListView 。
我尝试过:创建DBHelper方法以获取我需要的信息(注释和日期)。
目前适用于数据库浏览器for SQLite ,但我不知道如何将其添加到 ListView 适配器并将其设置为相应的 TextView < /强>
我的问题: DBHelper方法与自定义适配器之间的联合的最佳方法是什么才能获得所需的信息在 TextViews ? (匹配machines_id的日期和注释)
如果您需要任何其他课程或有任何反馈,请随意放纵我!
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);
}
}
}
答案 0 :(得分:1)
基本上,在执行查询时,数据库将返回Cursor对象。此Cursor将包含数据库中的所有返回行。您可以使用CursorAdapter或只是将Cursor传递给自定义适配器的构造函数。
这是一个非常简单的示例,说明如何使用RecyclerView适配器实现这一目标。
public class RecyclerViewCursorAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private Cursor mCursor;
private int mRowIdColumn;
// Pass the cursor to your adapter.
public void RecyclerViewCursorAdaper(Cursor cursor) {
mCursor = cursor;
mRowIdColumn = mCursor.getColumnIndex("_id");
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
// Inflate your view here.
return null;
}
@Override
public long getItemId(int position) {
if (mCursor != null && mCursor.moveToPosition(position)) {
return mCursor.getLong(mRowIdColumn);
}
return 0;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
mCursor.moveToPosition(position);
// Todo: Bind your custom view holder.
}
@Override
public int getItemCount() {
if (mCursor != null) {
return mCursor.getCount();
}
return 0;
}
public Cursor getCursor() {
return mCursor;
}
}