我遇到以下问题:我很确定代码是干净的,但是仿真器仍然没有显示我尝试保存的数据。我检查了两次,无法弄清楚是什么。也许你有个主意。
这是我的AppDatabase类:
@Database(entities = {Projekt.class}, version = 1, exportSchema = false)
@TypeConverters({Converters.class}) public abstract class AppDatabase
extends RoomDatabase {
public abstract RoomDAO getRoomDAO();
}
这是我的DAO界面:
@Dao
public interface RoomDAO {
@Insert
public void insert(Projekt... projekte);
@Update
public void update(Projekt... projekte);
@Delete
public void delete(Projekt... projekte);
@Query("SELECT * FROM projekt")
public List<Projekt> getProjektname();
@Query("SELECT * FROM projekt WHERE standort= :name")
public Projekt getProjektWithId(String name);
}
这是我的MainActivity:
private static final int CREATE_PROJEKT = 1;
private static final int UPDATE_PROJEKT = 2;
private RoomDAO mRoomDAO;
private RecyclerView ProjekteRecyclerview;
private ProjektRecyclerAdapter mProjektRecyclerAdapter;
private FloatingActionButton mAddProjektFloatingActionButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRoomDAO = Room.databaseBuilder(this, AppDatabase.class, "db-projekte")
.allowMainThreadQueries()
.build()
.getRoomDAO();
ProjekteRecyclerview = findViewById(R.id.ProjektRecyclerView);
ProjekteRecyclerview.setLayoutManager(new LinearLayoutManager(this));
mAddProjektFloatingActionButton = findViewById(R.id.addProjektFloatingActionButton);
int colors [] = {ContextCompat.getColor(this, R.color.colorAccent),
ContextCompat.getColor(this, android.R.color.holo_red_light),
ContextCompat.getColor(this, android.R.color.holo_orange_light),
ContextCompat.getColor(this, android.R.color.holo_green_light),
ContextCompat.getColor(this, android.R.color.holo_blue_dark),
ContextCompat.getColor(this, android.R.color.holo_purple)};
mProjektRecyclerAdapter = new ProjektRecyclerAdapter(this, new ArrayList<Projekt>(), colors);
mProjektRecyclerAdapter.addActionCallback(new ProjektRecyclerAdapter.ActionCallback() {
@Override
public void onLongClickListener(Projekt projekt) {
Intent intent = new Intent (MainActivity.this, UpdateProjektActivity.class);
intent.putExtra(UpdateProjektActivity.EXTRA_PROJEKT_ID, projekt.getStandort());
startActivityForResult(intent, UPDATE_PROJEKT);
}
});
ProjekteRecyclerview.setAdapter(mProjektRecyclerAdapter);
mAddProjektFloatingActionButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, CreateProjektActivity.class);
startActivityForResult(intent, CREATE_PROJEKT);
}
});
loadProjekte();
}
private void loadProjekte(){
mProjektRecyclerAdapter.updateData(mRoomDAO.getProjektname());
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == CREATE_PROJEKT && resultCode == RESULT_OK){
loadProjekte();
} else if (requestCode == UPDATE_PROJEKT && resultCode == RESULT_OK){
loadProjekte();
}
}
}
这是我的RecylcerAdapter,我认为这是问题所在。
public class ProjektRecyclerAdapter extends RecyclerView.Adapter<ProjektRecyclerAdapter.ViewHolder> {
interface ActionCallback {
void onLongClickListener(Projekt projekt);
}
private Context context;
private List<Projekt> projektList;
private int[] colors;
private ActionCallback mActionCallback;
ProjektRecyclerAdapter(Context context, List<Projekt> projektList, int[] colors){
this.context = context;
this.projektList = projektList;
this.colors = colors;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
View view = LayoutInflater.from(context).inflate(R.layout.item_recylcer_projekt, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
holder.bindData(position);
}
@Override
public int getItemCount() {
return projektList.size();
}
void updateData(List<Projekt> projekte){
this.projektList = projekte;
notifyDataSetChanged();
}
//ViewHolder Class
class ViewHolder extends RecyclerView.ViewHolder implements View.OnLongClickListener{
private TextView mNameTextView;
private TextView mInitialsTextView;
private GradientDrawable mInitialsBackground;
ViewHolder(View itemView){
super(itemView);
itemView.setOnLongClickListener(this);
mInitialsTextView = itemView.findViewById(R.id.initialsTextView);
mNameTextView = itemView.findViewById(R.id.nameTextView);
mInitialsBackground = (GradientDrawable) mInitialsTextView.getBackground();
}
void bindData(int position){
Projekt projekt = projektList.get(position);
String fullName = projekt.getProjektname() + " " + projekt.getArbeitername();
mNameTextView.setText(fullName);
String initial = projekt.getProjektname().toUpperCase().substring(0, 1);
mInitialsTextView.setText(initial);
mInitialsBackground.setColor(colors[position % colors.length]);
}
@Override
public boolean onLongClick(View v) {
if(mActionCallback != null){
mActionCallback.onLongClickListener(projektList.get(getAdapterPosition()));
}
return true;
}
}
void addActionCallback(ActionCallback actionCallbacks){
mActionCallback = actionCallbacks;
}
}
这是我的buil.gradle:
apply plugin: 'com.android.application'
android {
compileSdkVersion 26
defaultConfig {
applicationId "com.adesso.lklein.roomcontacttest"
minSdkVersion 16
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
implementation 'android.arch.persistence.room:runtime:1.0.0'
annotationProcessor "android.arch.persistence.room:compiler:1.0.0"
implementation 'com.android.support:design:27.0.2'
}
构建良好,没有错误或任何警告。即使在Logcat中,当我单击“保存”按钮时,它也不会显示任何内容。 知道这是什么吗?
预先感谢
答案 0 :(得分:0)
尝试这种方式..并根据更改代码。 进行如下所示的应用程序级活动
public class AppActivity extends Application {
static AppDatabase db;
@Override
public void onCreate() {
super.onCreate();
db = Room.databaseBuilder(getApplicationContext(), AppDatabase.class, "database-name").build();
}
public static AppDatabase getDatabase() {
return db;
}
}
并且此活动将android清单文件定义到应用程序标签中。如下所示。
android:name=".db.AppActivity"
之后创建数据库..
@Database(entities = {MyTable.class}, version = 1)
public abstract class AppDatabase extends RoomDatabase {
public abstract MyTableDao getTableDao();
}
制作道..
@Dao
public interface MyTableDao {
@Insert
void insertData(MyTable myTable);
@Query("SELECT * FROM MyTable where id=:id and ItemPrice=:price")
List<MyTable> getData(int id,int price);
}
然后执行如下所示的db操作。
AppActivity.getDatabase().getTableDao().insertData(user); // pass object