在我的应用程序中,我有三个选项卡(课程列表、下载和历史记录),在我的sqlite数据库中,我有一些列,其中两列名为 LECTURE_HISTORY_FLAG
以及 LECTURE_DOWNLOAD_FLAG
. 默认值为零 Course List
如果用户想下载或点击任何项目,我可以更新它们。我可以从弹出菜单中更新它们,弹出菜单嵌入在标签片段的recyclerview中。我很容易更新 LECTURE_HISTORY_FLAG
以及 LECTURE_DOWNLOAD_FLAG
从零到我想要的任何值成功地从我的 Course List
制表符和 History
选项卡和查询来显示和删除它们。我的问题是在一个片段(历史选项卡)我可以更新我的数据库,但从另一个片段(下载选项卡)我不能更新我的数据库。我在“下载”选项卡中使用的代码与在“历史记录”选项卡中使用的代码相同。这是我的密码。。。
downloadfragment.java选项卡
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View view = inflater.inflate(R.layout.activity_download_list_fragment, container, false);
download_layout = view.findViewById(R.id.download_layout);
return view;
}
@Override
public void onViewCreated(@NonNull View view, Bundle savedInstanceState){
super.onViewCreated(view, savedInstanceState);
sampleData();
}
public void sampleData(){
vRecyclerView = Objects.requireNonNull(getView()).findViewById(R.id.download_RecyclerView_fragment);
try {
CourseActivityDatabase hCourseDB;
hCourseDB = new CourseActivityDatabase(context);
db = hCourseDB.getReadableDatabase();
vdCourseList = new ArrayList<>();
cursor = db.query(
DB_TABLES.COURSE_TABLE_NAME,
new String[]{DB_TABLES.COURSE_ID, DB_TABLES.LECTURE_IMAGE_URL, DB_TABLES.LECTURE_TOPIC, DB_TABLES.LECTURE_GENERAL_INFO, DB_TABLES.LECTURE_DOWNLOAD_FLAG},
DB_TABLES.LECTURE_DOWNLOAD_FLAG + " > 0",
null, null, null, null);
if (cursor != null && cursor.getCount() != 0) {
vdCourseList.clear();
while (cursor.moveToNext()) {
SingleItemModel courseInfo = new SingleItemModel();
int lecture_id = cursor.getInt(cursor.getColumnIndex(DB_TABLES.COURSE_ID));
int lectureImg = cursor.getInt(cursor.getColumnIndex(DB_TABLES.LECTURE_IMAGE_URL));
String lectureTopic = cursor.getString(cursor.getColumnIndex(DB_TABLES.LECTURE_TOPIC));
String lectureInfo = cursor.getString(cursor.getColumnIndex(DB_TABLES.LECTURE_GENERAL_INFO));
courseInfo.setFlagId(lecture_id);
courseInfo.setLectureImg(lectureImg);
courseInfo.setLectureTopic(lectureTopic);
courseInfo.setLectureInfo(lectureInfo);
vdCourseList.add(courseInfo);
if (vdCourseList.size() != 0){
download_layout.setVisibility(View.INVISIBLE);
}else {
download_layout.setVisibility(View.VISIBLE);
}
}
}
}catch (SQLiteException e){
Toast.makeText(context, "Database is not available..!!!", Toast.LENGTH_LONG).show();
}
vAdapter = new HistoryRecyclerView(context, vdCourseList);
vRecyclerView.setLayoutManager(new GridLayoutManager(context,1));
vRecyclerView.setAdapter(vAdapter);
}
@Override
public void onDestroy() {
super.onDestroy();
cursor.close();
db.close();
}
我的downloadrecyclerview.java嵌入的downloadfragment.java片段选项卡。。
@NonNull
@Override
public DownloadRecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.download_recylerview_activity,parent,false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull DownloadRecyclerView.ViewHolder holder, int position) {
final Intent downIntent = new Intent(downContext, PlayActivity.class);
SingleItemModel downMain = downOneRowList.get(position);
holder.download_lectureImg.setImageResource(downMain.getLectureImg());
holder.download_lectureTopic.setText(downMain.getLectureTopic());
holder.download_lectureInfo.setText(downMain.getLectureInfo());
holder.download_vMenu.setOnClickListener(v -> {
PopupMenu popup = new PopupMenu(downContext, holder.download_vMenu);
popup.inflate(R.menu.download_menu_options);
popup.setOnMenuItemClickListener(item -> {
switch (item.getItemId()){
case (R.id.download_watch):
Toast.makeText(downContext, "starting playActivity...", Toast.LENGTH_LONG).show();
break;
case (R.id.download_remove):
//put all the codes for item "add to list" here
CourseActivityDatabase downloadDB = new CourseActivityDatabase(downContext);
SQLiteDatabase db = downloadDB.getWritableDatabase();
ContentValues downloadValues = new ContentValues();
downloadValues.put(DB_TABLES.LECTURE_HISTORY_FLAG, 0);
db.update(DB_TABLES.COURSE_TABLE_NAME,
downloadValues,
DB_TABLES.COURSE_ID + " = ?", new String[]{Integer.toString(downMain.getFlagId())});
Toast.makeText(downContext, "Video has been removed from history list!" + " " + position, Toast.LENGTH_SHORT).show();
db.close();
downOneRowList.remove(position);
notifyDataSetChanged();
break;
default:
break;
}
return true;
});
popup.show();
});
holder.download_card.setOnClickListener(v ->{
downIntent.putExtra("downloadItem", position);
downContext.startActivity(downIntent);
});
}
historyfragment.java片段。。
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View view = inflater.inflate(R.layout.activity_history_list_fragment, container, false);
his_layout = view.findViewById(R.id.history_layout);
return view;
}
@Override
public void onViewCreated(@NonNull View view, Bundle savedInstanceState){
super.onViewCreated(view, savedInstanceState);
sampleData();
vAdapter.notifyItemChanged(vhCourseList.size());
}
public void sampleData(){
vRecyclerView = Objects.requireNonNull(getView()).findViewById(R.id.history_RecyclerView_fragment);
try {
CourseActivityDatabase hCourseDB;
hCourseDB = new CourseActivityDatabase(context);
db = hCourseDB.getReadableDatabase();
vhCourseList = new ArrayList<>();
cursor = db.query(
DB_TABLES.COURSE_TABLE_NAME,
new String[]{DB_TABLES.COURSE_ID, DB_TABLES.LECTURE_IMAGE_URL, DB_TABLES.LECTURE_TOPIC, DB_TABLES.LECTURE_GENERAL_INFO, DB_TABLES.LECTURE_HISTORY_FLAG},
DB_TABLES.LECTURE_HISTORY_FLAG + " > 0", null, null, null, null);
if (cursor != null && cursor.getCount() != 0) {
vhCourseList.clear();
while (cursor.moveToNext()) {
SingleItemModel courseInfo = new SingleItemModel();
int lecture_id = cursor.getInt(cursor.getColumnIndex(DB_TABLES.COURSE_ID));
int lectureImg = cursor.getInt(cursor.getColumnIndex(DB_TABLES.LECTURE_IMAGE_URL));
String lectureTopic = cursor.getString(cursor.getColumnIndex(DB_TABLES.LECTURE_TOPIC));
String lectureInfo = cursor.getString(cursor.getColumnIndex(DB_TABLES.LECTURE_GENERAL_INFO));
courseInfo.setFlagId(lecture_id);
courseInfo.setLectureImg(lectureImg);
courseInfo.setLectureTopic(lectureTopic);
courseInfo.setLectureInfo(lectureInfo);
vhCourseList.add(courseInfo);
if (vhCourseList.size() != 0){
his_layout.setVisibility(View.INVISIBLE);
}else {
his_layout.setVisibility(View.VISIBLE);
}
}
}
}catch (SQLiteException e){
Toast.makeText(context, "Database is not available..!!!", Toast.LENGTH_LONG).show();
}
vAdapter = new HistoryRecyclerView(context, vhCourseList);
vRecyclerView.setLayoutManager(new GridLayoutManager(getActivity(),1));
vRecyclerView.setAdapter(vAdapter);
}
@Override
public void onDestroy() {
super.onDestroy();
cursor.close();
db.close();
}
historyrecyclerview.java嵌入historyfragment.java。。
@NonNull
@Override
public HistoryRecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.history_recylerview_activity,parent,false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull HistoryRecyclerView.ViewHolder holder, int position) {
final Intent hisIntent = new Intent(hisContext, PlayActivity.class);
SingleItemModel vMain = hisOneRowList.get(position);
holder.history_lectureImg.setImageResource(vMain.getLectureImg());
holder.history_lectureTopic.setText(vMain.getLectureTopic());
holder.history_lectureInfo.setText(vMain.getLectureInfo());
holder.history_vMenu.setOnClickListener(v -> {
PopupMenu popup = new PopupMenu(hisContext, holder.history_vMenu);
popup.inflate(R.menu.history_menu_option);
popup.setOnMenuItemClickListener(item -> {
switch (item.getItemId()){
case (R.id.history_watch):
Toast.makeText(hisContext, "starting playActivity..." + " " + position, Toast.LENGTH_LONG).show();
break;
case (R.id.history_remove):
//put all the codes for item "add to list" here
CourseActivityDatabase historyDB = new CourseActivityDatabase(hisContext);
SQLiteDatabase db = historyDB.getWritableDatabase();
ContentValues historyValues = new ContentValues();
historyValues.put(DB_TABLES.LECTURE_HISTORY_FLAG, 0);
db.update(DB_TABLES.COURSE_TABLE_NAME,
historyValues,
DB_TABLES.COURSE_ID + " = ?", new String[]{Integer.toString(vMain.getFlagId())});
Toast.makeText(hisContext, "Video has been removed from history list!" + " " + position, Toast.LENGTH_SHORT).show();
db.close();
hisOneRowList.remove(position);
notifyDataSetChanged();
break;
default:
break;
}
return true;
});
popup.show();
});
holder.history_card.setOnClickListener(v ->{
hisIntent.putExtra("historyItem", position);
hisContext.startActivity(hisIntent);
});
}
我哪里做错了?我找不到我在这里的逻辑,有人能帮帮我吗
暂无答案!
目前还没有任何答案,快来回答吧!