问题描述
在我的应用程序中,我有三个选项卡(课程列表、下载和历史记录),在我的 sqlite 数据库中,我有两列名为 LECTURE_HISTORY_FLAG
和 LECTURE_DOWNLOAD_FLAG
。默认值为零 从 Course List
开始,如果用户想要下载或单击任何项目,我可以更新它们。我从嵌入在 RecyclerView 标签片段中的弹出菜单更新它们。我可以很容易地将 LECTURE_HISTORY_FLAG
和 LECTURE_DOWNLOAD_FLAG
从零更新为我想要从我的 Course List
选项卡和 History
选项卡中成功获得的任何值,并查询以显示和删除它们。我的问题是在一个片段(历史选项卡)中,我可以更新我的数据库,但在另一个片段(下载选项卡)中,我无法更新我的数据库。我在下载选项卡中使用与历史选项卡相同的代码。这是我的代码...
下载Fragment.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);
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,Bundle savedInstanceState){
View view = inflater.inflate(R.layout.activity_history_list_fragment,false);
his_layout = view.findViewById(R.id.history_layout);
return view;
}
@Override
public void onViewCreated(@NonNull View 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,DB_TABLES.LECTURE_HISTORY_FLAG},DB_TABLES.LECTURE_HISTORY_FLAG + " > 0",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,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,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,historyValues,new String[]{Integer.toString(vMain.getFlagId())});
Toast.makeText(hisContext,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);
});
}
我可能做错了什么? 我找不到我在这里做的逻辑,请有人帮助我
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)