在哪里加载片段数据?

时间:2014-06-02 14:48:32

标签: android parse-platform

我对加载我的片段所需数据的位置感到困惑。我将数据存储在Parse.com中,我能够阅读它。但是我很困惑在哪里这样做,以尽量减少传递数据。我得到的是以下内容:

显示ListFragment的TakeActivity:

public class TakeActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_take);
        if (savedInstanceState == null) {
            getFragmentManager().beginTransaction()
                    .add(R.id.container, new ShiftsFragment())
                    .commit();
        }
    }

然后我有一个ShiftsFragment,显示有关Shifts的详细信息:

public class ShiftsFragment extends ListFragment {

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        ParseQuery<Shift> query = ParseQuery.getQuery(Shift.class);
        query.findInBackground(new FindCallback<Shift>() {
            @Override
            public void done(List<Shift> shifts, ParseException e) {
                ShiftAdapter adapter = new ShiftAdapter(getActivity(), shifts);
                setListAdapter(adapter);
            }
        });
    }

我想这样做,以便当您点击列表视图中的班次时,它会转到详细信息页面。我尝试过这样做:Communicating with Other Fragments

但是我遇到的问题是我的ShiftDetailFragment没有ShiftsFragment所做的数据。我还读过你不应该传递实时ParseObjects的地方。

如何将数据提供给两个片段?

1 个答案:

答案 0 :(得分:1)

您可以将您的列表存储在您的活动中,并为片段编写接口以访问或更新列表。

有些事情:


public Interface ShiftListInterface {
    public List<Shift> getShiftList();
    public void setShiftList(List<Shift> shiftList);
    public Shift getShift(int shiftIndex);
    public void updateShift(int shiftIndex, Shift shift);
}

public class TakeActivity
            extends ActionBarActivity
            implements ShiftListInterface {

    // hold your dataset in your Activity
    private List<Shift> shiftList;


    // load your dataset here
    private void loadShifts() {
        ParseQuery<Shift> query = ParseQuery.getQuery(Shift.class);
        query.findInBackground(new FindCallback<Shift>() {
            @Override
            public void done(List<Shift> shifts, ParseException e) {
                shiftList = shifts;
            }
        });
    }

    // implement methods from the interface to allow access to the list
    @Override
    public List<Shift) getShiftList() {
        return shiftList;
    }

    @Override
    public void setShiftList(List<Shift> shiftList) {
        this.shiftList = shiftList;
    }

    @Override
    public Shift getShift(int shiftindex) {
        return shiftList.get(shidtIndex);
    }

    @Override
    public void updateShift(int shiftIndex, Shift shift) {
        shiftList.set(shiftIndex, shift);
    }

    ...
}

public class ShiftsFragment extends ListFragment {

    // hold a reference to the interface defined
    private ShiftListInterface shiftListInterface;

    @Override
    public void onAttach(Activity activity)
    {
        super.onAttach(activity);
        // make sure the parent activity implements your interface
        try
        {
            shiftListInterface = (ShiftListInterface) activity;
        }
        catch (ClassCastException e)
        {
            Log.e(TAG, "Parent Activity doesn't implement ShiftListInterface");
            throw new ClassCastException(activity.toString()
                    + " must implement ShiftListInterface");
        }
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        ShiftAdapter adapter = new ShiftAdapter(getActivity(),
                                    shiftListInterface.getShiftList());
        setListAdapter(adapter);
    }

    ...
}

public DetailsFragment extends Fragment {

    private ShiftListInterface shiftListInterface;
    private int shiftIndex;
    private Shift shift;

    public DetailsFragment(int shiftIndex) {
        // pass in the index of the Shift you want to display
        this.shiftIndex = shiftIndex;
    }

    @Override
    public void onAttach(Activity activity)
    {
        super.onAttach(activity);
        try
        {
            shiftListInterface = (ShiftListInterface) activity;
        }
        catch (ClassCastException e)
        {
            Log.e(TAG, "Parent Activity doesn't implement ShiftListInterface");
            throw new ClassCastException(activity.toString()
                    + " must implement ShiftListInterface");
        }
        // after you ensure the interface exists you can grab the Shift
        this.shift = shiftListInterface.getShiftList().get(shiftIndex);
    }

    @Override
    public View onCreateView(LayoutInflater inflater,
                             ViewGroup container,
                             Bundle savedInstanceState)
    {
        View view = inflater.inflate(R.layout.fragment_shift_details,
                                     container, false);
        // populate your view using this.shift
        return view;
    }

    ...
}

您必须小心不要将空列表传递给您的片段,并在您的请求返回时通知他们更改并解析它以获取您的列表。我将把这部分留给你,因为我不知道你何时/何时加载/处理你的碎片。