本文介绍了凡加载片段数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有点困惑,要加载的数据,我的片段需求。我有存储在Parse.com我的数据,我可以读取它。不过,我很困惑,在哪里做,以尽量减少周围传递数据。我所得到的是以下内容:

I'm a bit confused as to where to load the data that my fragment needs. I've got my data stored in Parse.com and I'm capable of reading it. However I'm confused as to where to do that in order to minimize passing data around. What I've got is the following:

这显示ListFragment一个TakeActivity:

A TakeActivity that displays a ListFragment:

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:

Then I've got a ShiftsFragment which displays details about the 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);
            }
        });
    }

我想使它这样,当你点击它把你的详细信息页面列表视图的转变。我试图做到这一点,就像他们在说:与其他片段通讯

不过,我碰到,我的ShiftDetailFragment不具有ShiftsFragment做数据的问题。我读的地方,你不应该通过左右现场ParseObjects。

However I run into the problem that my ShiftDetailFragment doesn't have the data that ShiftsFragment does. I also read somewhere that you shouldn't be passing live ParseObjects around.

如何让我的数据可用于两个片段?

How do I make my data available to both fragments?

推荐答案

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

You can store your List in your Activity and write an Interface for the Fragments to access or update the list.

线沿线的东西:

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;
    }

    ...
}

您只是要小心,不要传递一个空表到你的片段,当你要求回来,通知他们变化的,你解析它让你的列表中。我会离开的那部分给你,因为我不知道如何/当加载/处理您片段做的。

You will just have to be careful to not pass a null List to your fragments and to notify them of changes when your request comes back and you parse it to get your list. I'll leave that part to you as I don't know how/when you are loading/handling your Fragments.

这篇关于凡加载片段数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 23:24