我有一个扩展FragmentActivity的MainActivity。有两个片段。第一个包含一个GridView,它由一个来自SQLite适配器类的Cursor填充。我已经分配了一个onItemClick事件来获取被点击的磁贴的数据。现在我需要将这些数据从片段传递给父活动,以便在第二个片段中使用它(根据Android开发最佳实践)。我已经看了几个实现回调的例子,并试图将它编码到我的应用程序中,但我必须遗漏一些非常明显的东西,因为我无法让它工作。这是第一个片段(我用/ * * / tags编辑了Callback位):
public class TimetableFragment extends Fragment {
private MobileClassDbAdapter mDbHelper;
private Cursor mTimetableCursor;
private ActionMode mActionMode;
/* OnItemClickListener mListener;
public interface OnItemClickListener {
public void OnItemClick(AdapterView<?> parent, View v, int position, long id);
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mListener = (OnItemClickListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString() + " must implement OnItemClickListener");
}
}
*/
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.timetable_fragment, container, false);
GridView gridView = (GridView)view.findViewById(R.id.timetable_grid);
fillTimetable(gridView);
return view;
}
@SuppressWarnings("deprecation")
private void fillTimetable(GridView gridView) {
mDbHelper = new MobileClassDbAdapter(getActivity().getApplicationContext());
mDbHelper.open();
// Get all of the rows from the database and create the item list
mTimetableCursor = mDbHelper.fetchAllTimetableActivities();
// check the length of the cursor
int count = mTimetableCursor.getCount();
int days = 5;
int lessons = 9;
int test = days * lessons;
if (count < test) {
Toast.makeText(getActivity(), "Not filled with timetable activities", Toast.LENGTH_LONG).show();
for (int i = 1; i <= days; i++) {
for (int j = 1; j <= lessons; j++) {
String day = Integer.toString(i);
String lesson = Integer.toString(j);
if (mDbHelper.findTimetableActivity(day, lesson, null, null) == false) {
mDbHelper.createTimetableActivity(day, lesson, null, null, null);
}
}
mTimetableCursor = mDbHelper.fetchAllTimetableActivities();
}
}
String[] from = new String[]{MobileClassDbAdapter.KEY_TIMETABLE_ACTIVITY,MobileClassDbAdapter.KEY_TIMETABLE_NOTE};
int[] to = new int[]{R.id.timetable_text1,R.id.timetable_text2};
// Now create a simple cursor adapter and set it to display
final SimpleCursorAdapter timetable = new SimpleCursorAdapter(getActivity(), R.layout.timetable_card, mTimetableCursor, from, to);
gridView.setAdapter(timetable);
// create click listener
gridView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
//JUST A TEST TO SEE IF THE ClickListener RETURNS THE CORRECT DATA
Cursor item = (Cursor)timetable.getItem(position);
String text = item.getString(item.getColumnIndexOrThrow(MobileClassDbAdapter.KEY_TIMETABLE_DAY))
+ " " + item.getString(item.getColumnIndexOrThrow(MobileClassDbAdapter.KEY_TIMETABLE_LESSON))
+ " (id: " + item.getString(item.getColumnIndexOrThrow(MobileClassDbAdapter.KEY_TIMETABLE_ID)) + ")";
Toast.makeText(getActivity(), text, Toast.LENGTH_LONG).show();
//END OF TEST...
/* mListener.OnItemClick(parent, v, position, id);*/
}
});
}
}
以下是MainActivity:
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
我没有包含第二个片段,因为我还没有开始编码 - 我首先需要从片段中获取数据到父活动。我真的需要一些建议...谢谢!
答案 0 :(得分:2)
您的MainActivity
需要实现片段的界面。
无论如何,你应该在Fragment中处理你的onItemClick,并且只通知Activity关于结果。在您的情况下,您将获得有关AdapterView的信息,项目的视图和位置/ ID。但是你的Activitiy不知道GridView ......
处理片段内部的onClick并将Activity传递给所需的结果。
因此,您应该添加一个类似“itemClicked(InformationForActivity ...)”的接口
试试这个:
public class TimetableFragment extends Fragment {
ItemClickedListener mListener;
public interface ItemClickedListener {
public void onItemClicked(Item clickedItem);
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mListener = (CursorClickedListener ) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement CursorClickedListener ");
}
}
@SuppressWarnings("deprecation")
private void fillTimetable(GridView gridView) {
....
Item item = ... ;// build your item
mListener.onItemClick(item); //this informs the activity
}
}
的活动:
public class MainActivity extends FragmentActivity
implements TimetableFragment.ItemClickedListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public void ItemClickedListener (Item clickedItem){
//inform the other fragment here!
}
}