0

내 응용 프로그램에서는 SherlockFragmentActivity 내에서 뷰 호출기를 사용하고 ViewPager 5 ListFragments에 연결하여 각각 하루를 나타냅니다!ViewPager 및 ListFragment with onLongClick 이상한 동작

모든 ListFragment에서 볼 수 있듯이 ListFragment의 루트 ListView에서 Listener를 사용합니다.

문제는 이상하게도 LongClick 이벤트가 월요일을 나타내는 첫 번째 조각에서부터 시작될 때 충분하지만, 화요일을 나타내는 다음 조각에서 이벤트가 발생하면 곧 나오는 Activity에 도착합니다. 첫 번째 Fragment에서 해고되었으므로 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