在我的应用程序中,我在SherlockFragmentActivity中使用了一个视图寻呼机,并且我附加了每个代表一天的ViewPager 5 ListFragments!
在每个ListFragment中,您将看到我在ListFragment的根ListView上使用LongClicks的Listener。
问题是,当从第一个片段触发LongClick事件时,问题很奇怪,它代表星期一,一切正常,但是当我从代表星期二的下一个片段触发事件时,我进入即将发生的活动,它被解雇了从第一个片段开始,这意味着它是从MondayListFragment中解雇的!
我似乎无法找到我做错了什么。任何帮助,将不胜感激!提前谢谢!
第一个列表片段
public class Monday扩展了SherlockListFragment实现AdapterView.OnItemLongClickListener {
public String department;
private String gradeString;
public int grade;
private int mSelectedPosition;
public boolean personal;
private ArrayList<String> lessonTitlesList;
private ArrayList<Lessons> lessonsList;
private DatabaseUtils shareOptions;
private ArrayList<String> titlesSet;
private Cursor c;
private SimpleCursorAdapter smc;
private String[] from = {"Lesson_title", "Time", "Room"};
private int[] to = {R.id.lessons,R.id.timeCol,R.id.roomComments};
private final String FRAGMENT_DAY_NAME = "Δευτέρα";
private boolean fromStart = false;
@Override
public void onActivityCreated(Bundle pack) {
super.onActivityCreated(pack);
shareOptions = new DatabaseUtils("LessonsDB",getActivity().getApplicationContext());
shareOptions.instantiatePreferences(getActivity().getApplicationContext());
lessonTitlesList = new ArrayList<String>();
lessonsList = new ArrayList<Lessons>();
titlesSet = new ArrayList<String>();
this.department = getArguments().getString("department");
this.grade = getArguments().getInt("grade");
this.personal = getArguments().getBoolean("personal");
this.gradeString = shareOptions.gradeParser(grade);
shareOptions.finishDb();
if (personal) {
ListPopulatorPersonal();
shareOptions = new DatabaseUtils("LessonsDB",getActivity().getApplicationContext());
c = shareOptions.getLessonsByDayCursor(FRAGMENT_DAY_NAME, titlesSet);
fromStart = true;
} else{
ListPopulatorWeekly(department, this.gradeString);
shareOptions = new DatabaseUtils("LessonsDB",getActivity().getApplicationContext());
c = shareOptions.getLessonsByDeptAndSemesterCursor(department, gradeString, FRAGMENT_DAY_NAME);
}
lessonsListPopulator();
if(shareOptions.isLowerThanIcs()){
smc = new SimpleCursorAdapter(getActivity().getApplicationContext(),R.layout.list_row_lessons,c,from,to);
}else
smc = new SimpleCursorAdapter(getActivity().getApplicationContext(),R.layout.list_row_lessons,c,from,to,0);
smc.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
@Override
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
if(columnIndex == cursor.getColumnIndexOrThrow("Room")){
String comments = shareOptions.commentsAnalyse(cursor.getString(cursor.getColumnIndexOrThrow("Room")),
cursor.getString(cursor.getColumnIndexOrThrow("Comments")));
TextView tv = (TextView) view;
tv.setText(comments);
return true;
}
if(columnIndex == cursor.getColumnIndexOrThrow("Time")){
String comments = "Ώρα: " + cursor.getString(cursor.getColumnIndexOrThrow("Time"));
TextView tv = (TextView) view;
tv.setText(comments);
return true;
}
return false;
}
});
this.getListView().setOnItemLongClickListener(this);
this.setEmptyText(getString(R.string.noLessonDay));
setListAdapter(smc);
this.shareOptions.finishDb();
this.getListView().setBackgroundColor(Color.WHITE);
this.getListView().setDivider(new GradientDrawable(GradientDrawable.Orientation.LEFT_RIGHT, DatabaseUtils.GRADIENT_COLORS));
this.getListView().setDividerHeight(3);
}
private void initialiseAndRefresh(){
if (personal) {
ListPopulatorPersonal();
shareOptions = new DatabaseUtils("LessonsDB",getActivity().getApplicationContext());
c = shareOptions.getLessonsByDayCursor(FRAGMENT_DAY_NAME, titlesSet);
fromStart = true;
} else{
ListPopulatorWeekly(department, this.gradeString);
shareOptions = new DatabaseUtils("LessonsDB",getActivity().getApplicationContext());
c = shareOptions.getLessonsByDeptAndSemesterCursor(department, gradeString, FRAGMENT_DAY_NAME);
}
lessonsListPopulator();
if(shareOptions.isLowerThanIcs()){
smc = new SimpleCursorAdapter(getActivity().getApplicationContext(),R.layout.list_row_lessons,c,from,to);
}else
smc = new SimpleCursorAdapter(getActivity().getApplicationContext(),R.layout.list_row_lessons,c,from,to,0);
smc.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
@Override
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
if(columnIndex == cursor.getColumnIndexOrThrow("Room")){
String comments = shareOptions.commentsAnalyse(cursor.getString(cursor.getColumnIndexOrThrow("Room")),
cursor.getString(cursor.getColumnIndexOrThrow("Comments")));
TextView tv = (TextView) view;
tv.setText(comments);
return true;
}
if(columnIndex == cursor.getColumnIndexOrThrow("Time")){
String comments = "Ώρα: " + cursor.getString(cursor.getColumnIndexOrThrow("Time"));
TextView tv = (TextView) view;
tv.setText(comments);
return true;
}
return false;
}
});
setListAdapter(smc);
}
/**
* Populates a list if the user chose to see his/her personal programm
* */
private void ListPopulatorPersonal() {
shareOptions = new DatabaseUtils("OptionsDBRead",getActivity().getApplicationContext());
titlesSet = shareOptions.getOptionsRows();
shareOptions.finishOptionDb();
shareOptions = new DatabaseUtils("LessonsDB",getActivity().getApplicationContext());
lessonsList = shareOptions.getLessonsByDay(FRAGMENT_DAY_NAME, titlesSet);
shareOptions.finishDb();
}
/**
* Populates the lessonsList for the weekly programm to be shown.
* */
public void ListPopulatorWeekly(String dept, String semester) {
shareOptions = new DatabaseUtils("LessonsDB",getActivity().getApplicationContext());
lessonsList = shareOptions.getLessonsByDeptAndSemester(dept, semester, FRAGMENT_DAY_NAME);
shareOptions.finishDb();
}
/**
* Gets the lesson Titles from the lessonsList and adds them to the lessonTitlesList for further use.
* */
private void lessonsListPopulator() {
for (Lessons a : this.lessonsList) {
lessonTitlesList.add(a.getLesson_Title());
}
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Lessons a = lessonsList.get(position);
ArrayList<String> mArrayList = new ArrayList<String>();
mArrayList.add(0, a.getLesson_Title());
mArrayList.add(1, a.getTime());
mArrayList.add(2, a.getProffessor());
mArrayList.add(3, a.getRoom());
if(a.getComments() != null) mArrayList.add(4, a.getComments());
Intent i = new Intent(getActivity(), ShowLesson.class);
Bundle pack = new Bundle();
pack.putString("lesson", lessonsList.get(position).getLesson_Title());
pack.putStringArrayList("myArray", mArrayList);
pack.putString("department",department);
pack.putInt("grade", grade);
pack.putBoolean("personal", personal);
pack.putInt("day", 0);
i.putExtras(pack);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(i);
}
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
mSelectedPosition = position;
registerForContextMenu(this.getListView());
getListView().showContextMenu();
return true;
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
getActivity().getMenuInflater().inflate(R.menu.list_fragment_options, menu);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
if(item.getItemId() == R.id.edit){
Intent tempIntent = new Intent(getActivity(),EditLessonActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
tempIntent.putExtra("title",lessonsList.get(mSelectedPosition).getLesson_Title());
tempIntent.putExtra("time", lessonsList.get(mSelectedPosition).getTime());
tempIntent.putExtra("professor", lessonsList.get(mSelectedPosition).getProffessor());
tempIntent.putExtra("room",lessonsList.get(mSelectedPosition).getRoom());
tempIntent.putExtra("department",department);
tempIntent.putExtra("day",FRAGMENT_DAY_NAME);
if(lessonsList.get(mSelectedPosition).getComments().equals("")){
tempIntent.putExtra("comments","-");
}else{
tempIntent.putExtra("comments",lessonsList.get(mSelectedPosition).getComments());
}
startActivity(tempIntent);
}else if(item.getItemId() == R.id.add){
Intent addLessonActivityIntent = new Intent(getActivity(),AddLessonActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
if(fromStart){
addLessonActivityIntent.putExtra("department",DatabaseUtils.mDepartment);
addLessonActivityIntent.putExtra("grade",DatabaseUtils.mStringGrade);
addLessonActivityIntent.putExtra("day",FRAGMENT_DAY_NAME);
}else{
addLessonActivityIntent.putExtra("department",department);
addLessonActivityIntent.putExtra("grade",gradeString);
addLessonActivityIntent.putExtra("day",FRAGMENT_DAY_NAME);
}
Bundle pack = new Bundle();
pack.putBoolean("personal",personal);
addLessonActivityIntent.putExtras(pack);
startActivity(addLessonActivityIntent);
}else if(item.getItemId() == R.id.delete){
Lessons temp = lessonsList.get(mSelectedPosition);
shareOptions = new DatabaseUtils("LessonsDBWrite",getActivity().getApplicationContext());
shareOptions.deleteByLessonCustom(temp, FRAGMENT_DAY_NAME);
shareOptions.finishDb();
initialiseAndRefresh();
}
return super.onContextItemSelected(item);
}
}
其余的ListFragments与FRAGMENT_DAY_NAME完全相同,所以没有必要发布它们!
如果您需要我发布任何其他代码部分,请告诉我们!
答案 0 :(得分:0)
您应该为每个片段创建不同的onItemLongClick侦听器。