使用Parcelable对象将数据发送到Fragment而不是Activity

时间:2014-06-06 09:19:47

标签: java android android-intent android-fragments parcelable

我想将数据发送到我已经制作的Parcelable类的Fragment。

我知道这是你在使用Activity时所做的事情(ObjectA是扩展Parcelable的类):

ObjectA obj = new ObjectA();   
// Set values etc.   
Intent i = new Intent(this, MyActivity.class); 
i.putExtra("com.package.ObjectA", obj);   
startActivity(i); 

但是由于你无法使用碎片的意图,我不知道如何做到这一点。

我甚至可以使用Parcelable类吗?如果没有,我应该使用什么?我有可能,怎么样?

编辑/更新:

我已根据您的推荐进行了更新,但仍然无法获得所有内容。

这是我们用来创建列表视图的片段:

package com.tsjd.HotMeals;

import java.util.ArrayList;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.example.tabtest.R;

public class RecipeListViewFragment extends Fragment {

    ArrayList<String> titles;
    ArrayList<String> descriptions;
    ArrayList<String> images;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        RecipeListViewParcer object = getArguments().getParcelable("extraObject");
        titles = object.getTitles();
        descriptions = object.getDescriptions();
        images = object.getImages();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_recipelistview, container,
                false);

        final ListView listview = (ListView) v.findViewById(R.id.listview);


        RecipeListViewAdapter adapter = new RecipeListViewAdapter(
                getActivity(), titles);
        listview.setAdapter(adapter);

        return v;
    }

    public void onListItemClick(ListView l, View v, int position, long id) {
        // do something with the data
    }
}

我想将此ListView用于不同的事情,所以我希望能够将三个ArrayLists发送给它。为此,我创建了一个实现Parcelable的类,就是这样:

package com.tsjd.HotMeals;

import java.util.ArrayList;

import android.os.Parcel;
import android.os.Parcelable;

public class RecipeListViewParcer implements Parcelable{

    private ArrayList<String> titles;
    private ArrayList<String> descriptions;
    private ArrayList<String> images;

     @SuppressWarnings("unchecked")
    private RecipeListViewParcer(Parcel in) {
            titles = (ArrayList<String>) in.readSerializable();
            descriptions = (ArrayList<String>) in.readSerializable();
            images = (ArrayList<String>) in.readSerializable();
        }

    public int describeContents() {
        return 0;
    }

    public ArrayList<String> getTitles()
    {
        return titles;
    }

    public void setTitles(ArrayList<String> titles){
        this.titles = titles;
    }

    public ArrayList<String> getDescriptions()
    {
        return descriptions;
    }

    public void setDescriptions(ArrayList<String> descriptions){
        this.descriptions = descriptions;
    }

    public ArrayList<String> getImages()
    {
        return images;
    }

    public void setImages(ArrayList<String> images){
        this.images = images;
    }

    public void writeToParcel(Parcel out, int flags) {
        out.writeSerializable(titles);
        out.writeSerializable(descriptions);
        out.writeSerializable(images);
    }

    public static final Parcelable.Creator<RecipeListViewParcer> CREATOR
            = new Parcelable.Creator<RecipeListViewParcer>() {
        public RecipeListViewParcer createFromParcel(Parcel in) {
            return new RecipeListViewParcer(in);
        }

        public RecipeListViewParcer[] newArray(int size) {
            return new RecipeListViewParcer[size];
        }
    };



}

最后,这是一个片段,它使用listview显示最喜欢的食谱,这里我想发送三个ArrayLists,这是目前唯一有错误的代码。我不知道如何为我的Parcelable类的构造函数声明我需要的包,我应该怎么做呢? (我还没有在我的ArrayLists中放任何东西,但我希望这个想法很明确。)

package com.tsjd.HotMeals;

import java.util.ArrayList;

import android.os.Bundle;
import android.os.Parcel;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

public class MyRecipesFragment extends Fragment {


    ArrayList<String> titles = new ArrayList<String>();
    ArrayList<String> descriptions = new ArrayList<String>();
    ArrayList<String> images = new ArrayList<String>();



    Parcel in = new Parcel();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        Fragment fragment = new RecipeListViewFragment();

        RecipeListViewParcer obj = new RecipeListViewParcer(in);

        obj.setDescriptions(descriptions);
        obj.setTitles(titles);
        obj.setImages(images);

        Bundle bundle = new Bundle();
        bundle.putParcelable("extraObject", obj);
        fragment.setArguments(bundle);

        fragmentTransaction.commit();
    }

}

1 个答案:

答案 0 :(得分:5)

您必须使用FragmentManager:

在Activity中发送3个ArrayLists:

       FragmentManager fragmentManager = getFragmentManager();
       FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
       Fragment fragment = new ViewControllerFragment();

       ObjectA obj = new ObjectA();
       Bundle bundle = new Bundle();
       bundle.putStringArrayList("Titles", obj.getTitles());
       bundle.putStringArrayList("Descriptions", obj.getDescriptions());
       bundle.putStringArrayList("Images", obj.getImages());
       fragment.setArguments(bundle);

       fragmentTransaction.commit();

在片段中获取对象:

Bundle bundle = getArguments();                    
 titles = bundle.getStringArrayList("Titles");            
descriptions= bundle.getStringArrayList("Descriptions");            
images= bundle.getStringArrayList("Images");

或者只是尝试其他帖子:Send data from activity to fragment in android