我正在设置一个RecyclerView来查看数据库中数据的ArrayList(最初为空)。但是,它在Android虚拟设备中不断崩溃。我是Android Studio的新手,正在做一个个人项目。我使用了不同的书籍和视频来创作自己的作品。
我试图按字母顺序列出所有具有共同类别的数据。但如果数据不存在,则显示id / emptyView textView。
我认为我的错误可能是在适配器中,因为我在ViewHolder中选择了不正确的textView或在onCreateViewHolder中选择了不正确的通货膨胀布局,因为我对此感到有些困惑,他们应该指出这一点。或我的db.query游标中的错误。
Logcat:
FATAL EXCEPTION: main
Process: com.HSC.calendarfit, PID: 10164
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.HSC.calendarfit/com.HSC.calendarfit.ExerciseList}: android.database.sqlite.SQLiteException: unrecognized token: ""Arms ORDER BY exerciseName ASC" (code 1 SQLITE_ERROR): , while compiling: SELECT exerciseName FROM exerciseList WHERE muscleGroup = "Arms ORDER BY exerciseName ASC
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: android.database.sqlite.SQLiteException: unrecognized token: ""Arms ORDER BY exerciseName ASC" (code 1 SQLITE_ERROR): , while compiling: SELECT exerciseName FROM exerciseList WHERE muscleGroup = "Arms ORDER BY exerciseName ASC
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:903)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:514)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:46)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1408)
at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1255)
at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1126)
at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1294)
at com.HSC.calendarfit.dbExerciseListHandler$override.getExercises(dbExerciseListHandler.java:65)
at com.HSC.calendarfit.dbExerciseListHandler$override.access$dispatch(Unknown Source:64)
at com.HSC.calendarfit.dbExerciseListHandler.getExercises(Unknown Source:15)
at com.HSC.calendarfit.ExerciseList.onCreate(ExerciseList.java:43)
at android.app.Activity.performCreate(Activity.java:7136)
at android.app.Activity.performCreate(Activity.java:7127)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
在活动中调用适配器和方法:
dbHandler = new dbExerciseListHandler(this, null, null, 1);
muscleCategory = "Arms";
List<dbExerciseList> list = dbHandler.getExercises(muscleCategory);
recyclerView = (RecyclerView) findViewById(R.id.rvExerciseList);
emptyView = (TextView) findViewById(R.id.emptyView);
if (list.isEmpty()){
recyclerView.setVisibility(View.GONE);
emptyView.setVisibility(View.VISIBLE);
}else{
recyclerView.setVisibility(View.VISIBLE);
emptyView.setVisibility(View.GONE);
}
recyclerView.setHasFixedSize(true);
mAdapter = new ExerciseListAdapter(list);
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(mAdapter);
数据库处理程序中用于获取数据并创建Arraylist的方法:
public List<dbExerciseList> getExercises(String muscleGroup){
List<dbExerciseList> list = new ArrayList<>();
SQLiteDatabase db = this.getWritableDatabase();
String[] columnsToUse = {COLUMN_EXERCISENAME};
Cursor cursor = db.query(TABLE_EXERCISE, columnsToUse, COLUMN_MUSCLEGROUP + " = \"" +
muscleGroup, null, null, null, COLUMN_EXERCISENAME + "ASC");
while (cursor.moveToNext()){
String exerciseName = cursor.getString(0);
dbExerciseList name = new dbExerciseList(exerciseName);
list.add(name);
}
return list;
}
dbExerciseList有我的获取器和设置器。
RecyclerView适配器:
public class ExerciseListAdapter extends RecyclerView.Adapter<ExerciseListAdapter.ExerciseListViewHolder>{
private List<dbExerciseList> mDataset;
public class ExerciseListViewHolder extends RecyclerView.ViewHolder {
public TextView tvExerciseName;
public ExerciseListViewHolder(@NonNull View itemView) {
super(itemView);
tvExerciseName = itemView.findViewById(R.id.tvExerciseName);
}
}
public ExerciseListAdapter(List<dbExerciseList> mDataset) {
this.mDataset = mDataset;
}
@Override
public ExerciseListViewHolder onCreateViewHolder( ViewGroup viewGroup, int i) {
TextView v = (TextView) LayoutInflater.from(viewGroup.getContext()).
inflate(R.layout.activity_exercise_list, viewGroup, false);
return new ExerciseListViewHolder(v);
}
@Override
public void onBindViewHolder(ExerciseListViewHolder exerciseListViewHolder, int position) {
dbExerciseList item = mDataset.get(position);
exerciseListViewHolder.tvExerciseName.setText(item.get_exerciseName());
}
@Override
public int getItemCount() {
return mDataset.size();
}
}
谢谢。
答案 0 :(得分:0)
在getExercises()
内部,在此行:
Cursor cursor = db.query(TABLE_EXERCISE, columnsToUse, COLUMN_MUSCLEGROUP + " = \"" +
muscleGroup, null, null, null, COLUMN_EXERCISENAME + "ASC");
这是什么:\"
????
这将使该字符串:
SELECT exerciseName FROM exerciseList WHERE muscleGroup = "Arms ORDER BY exerciseName ASC
由于双引号在语法上是错误的。
更改为:
Cursor cursor = db.query(TABLE_EXERCISE, columnsToUse, COLUMN_MUSCLEGROUP
+ " = '" + muscleGroup + "'", null, null, null, COLUMN_EXERCISENAME + "ASC");
(您要这样做吗?)
但是由于这种方式易于进行sql注入,因此更好地使用这种方式将参数传递给sql语句:
String strQuery = "SELECT " + COLUMN_EXERCISENAME + " FROM " + TABLE_EXERCISE +
" WHERE " + COLUMN_MUSCLEGROUP + " = ? ORDER BY " + COLUMN_EXERCISENAME + " ASC";
Cursor cursor = db.rawQuery(strQuery, new String[] {muscleGroup});
这样,您不必担心传递的参数类型。