0

在我的應用程序中,我使用了SherlockFragmentActivity內的視圖分頁器,並附加到ViewPager 5 ListFragments,每個代表一天!ViewPager和帶有onLongClick奇怪行爲的ListFragment

在每個ListFragment中,您將看到我在ListFragment的根ListView上使用LongClicks的Listener。

問題是,奇怪的是,當從第一個Fragment觸發LongClick事件時,它表示星期一一切正常,但是當我從下一個表示星期二的片斷中引發事件時,我在即將到來的Activity中得到它是從第一片片段發射出來,這意味着它是從MondayListFragment發射的!

我似乎無法找到我做錯了什麼。任何幫助,將不勝感激!先謝謝你!

首先列出片段

公共類週一擴展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

您應該爲每個片段創建不同的onItemLongClick偵聽器。

+0

每個片段都有一個onItemLongClickListener! – Pavlos