我已经实现了SQL数据库和Recycler View。我无法让Recycler查看数据库中的数据。我做了一些阅读,根据我的理解,我需要为recycler视图实现SimpleCursorAdapter。我看了一些指南,但都没有成功。谢谢!
以下是Recycler View Java代码:
public class RVAdapter extends RecyclerView.Adapter<RVAdapter.MyViewHolder> {
private LayoutInflater inflater;
List<MainInfo> data = Collections.emptyList();
public RVAdapter (Context context, List<MainInfo> data) {
inflater = LayoutInflater.from(context);
this.data = data;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.custom_row, parent, false);
MyViewHolder holder = new MyViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(MyViewHolder viewHolder, int i) {
}
@Override
public int getItemCount() {
return 0;
}
class MyViewHolder extends RecyclerView.ViewHolder {
TextView Title, Body;
MyViewHolder(View itemView) {
super(itemView);
Title = (TextView) itemView.findViewById(R.id.Row_Header);
Body = (TextView) itemView.findViewById(R.id.Row_Footer);
}
}
}
这是数据库java代码
public class ManagerDatabaseAdapter{
ManagerHelper helper;
public ManagerDatabaseAdapter(Context context) {
helper = new ManagerHelper(context);
}
public long inserData(String name, String code) {
SQLiteDatabase db = helper.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(ManagerHelper.NAME, name);
contentValues.put(ManagerHelper.CODE,code);
long id = db.insert(ManagerHelper.TABLE_NAME, null, contentValues);
return id;
}
public String getAllData () {
String[] columns = {ManagerHelper.UID,ManagerHelper.NAME,ManagerHelper.CODE};
SQLiteDatabase db = helper.getWritableDatabase();
Cursor cursor = db.query(ManagerHelper.TABLE_NAME, columns, null, null, null, null, null);
StringBuffer buffer = new StringBuffer();
while (cursor.moveToNext()){
int cid = cursor.getInt(0);
String name = cursor.getString(1);
String code = cursor.getString(2);
buffer.append(cid+" "+name+" "+code+"/n");
}
return buffer.toString();
}
public String getData(String name) {
String[] columns = {ManagerHelper.NAME,ManagerHelper.CODE};
SQLiteDatabase db = helper.getWritableDatabase();
Cursor cursor = db.query(ManagerHelper.TABLE_NAME, columns, ManagerHelper.NAME+" = '"+name+"'", null, null, null, null);
StringBuffer buffer = new StringBuffer();
while (cursor.moveToNext()){
int index1 = cursor.getColumnIndex(ManagerHelper.NAME);
int index2 = cursor.getColumnIndex(ManagerHelper.CODE);
String placename = cursor.getString(index1);
String code = cursor.getString(2);
buffer.append(name+" "+code+"/n");
}
return buffer.toString();
}
public int updatePlace(String oldName, String newName) {
SQLiteDatabase db = helper.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(ManagerHelper.NAME, newName);
String[] whereArgs= {oldName};
int count = db.update(ManagerHelper.TABLE_NAME, contentValues, ManagerHelper.NAME+" =?", whereArgs);
return count;
}
public long deleteRow(String delete) {
SQLiteDatabase db = helper.getWritableDatabase();
String[] whereArgs = {delete};
long id = db.delete(ManagerHelper.TABLE_NAME, ManagerHelper.NAME + " =?", whereArgs);
return id;
}
static class ManagerHelper extends SQLiteOpenHelper {
private static final String EXTRA_MESSAGE = "com.nick.mowen.receiptmanager.LOCATION";
private static final String DATABASE_NAME = "Main_Data";
private static final String TABLE_NAME = "DATA";
private static final int VERSION = 2;
private static final String UID = "_id";
private static final String NAME = "Codes";
private static final String CODE = "Code";
private static final String ADDRESS = "Address";
private static final String CREATE_TABLE = "CREATE TABLE "+TABLE_NAME+" ("+UID+" INTEGER PRIMARY KEY AUTOINCREMENT, "+NAME+" VARCHAR(255), "+CODE+" VARCHAR(255));";
private static final String DROP_TABLE = "DROP_TABLE IF EXISTS"+TABLE_NAME;
private Context context;
public ManagerHelper(Context context) {
super(context, DATABASE_NAME, null, VERSION);
this.context=context;
//Message.message(context,"Constructor Called");
}
@Override
public void onCreate(SQLiteDatabase db) {
Intent intent = new Intent(context,LocationAdder.class);
intent.putExtra(EXTRA_MESSAGE,true);
context.startActivity(intent);
try {
MainActivity.firstRun();
db.execSQL(CREATE_TABLE);
//Message.message(context,"onCreate called");
} catch (SQLException e) {
//Message.message(context,""+e);
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
try {
//Message.message(context,"onUpgrade called");
db.execSQL(DROP_TABLE);
onCreate(db);
}catch (SQLException e){
//Message.message(context,""+e);
}
}
}
}