将数据从一个片段发送到另一个片段

时间:2015-02-19 13:12:53

标签: android android-fragments

我有2个片段托管在一个带有框架布局的活动上,我想实现片段通信。我尝试了一些代码,但是我的要求没有得到满足。在此先感谢。!

第一个片段:它包含一个列表。

public class ListWithSearch extends Fragment{

    String[] cityName;
    String[] cityFact;
    int[] cityPic;

    ListView list;
    ListViewAdapter adapter;

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

        // Generate sample data
        cityName = new String[] { "Delhi", "Chandigarh", "Mumbai", "Bangalore", "Chennai" };

        cityFact = new String[] { "China", "India", "United States",
                "Indonesia", "Brazil" };
        cityPic = new int[] { R.drawable.ic_launcher, R.drawable.ic_launcher,
                R.drawable.ic_launcher, R.drawable.ic_launcher,
                R.drawable.ic_launcher };

        list = (ListView) rootView.findViewById(R.id.listView);

        adapter = new ListViewAdapter(getActivity(), cityName,cityPic);

        list.setAdapter(adapter);

        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                //To implement 

            }
        });


        return rootView;

    }



}

第二个片段:第一个片段中列表的项目点击将会膨胀。

public class ResultFragment extends Fragment {

    TextView resCityName;
    TextView resFact;
    ImageView resPic;

    String[] cityName;
    String[] cityFact;
    int[] cityPic;
    int position;

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

    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {


        return super.onCreateView(inflater, container, savedInstanceState);
    }
}

要求: 1.第二个片段中的ImageView与列表中的项目图像具有相同的图像。 2.第二个片段中的TextViews,用于从列表项本身获取数据。

换句话说,第二个片段是列表中单击的项目的详细信息。

2 个答案:

答案 0 :(得分:1)

将以下代码添加到ListWithSearch片段:

OnListItemSelectListener mCallback;

// Your MainActivity must implement this interface so that ResultFragment can use it
public interface OnListItemSelectListener {
    public void onItemSelected(int position, int imageId);
}

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);

    try {
        mCallback = (OnListItemSelectListener) activity;
    } catch (ClassCastException e) {
        throw new ClassCastException(activity.toString()
                + " must implement OnListItemSelectListener");
    }
}

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        // Send the event to the host activity
        // You can pass the image id to below method. Modify the code as required.
        mCallback.onItemSelected(position, imageId);
    }

答案 1 :(得分:0)

  • Google为这种情况提供了一个很好的教程here

  • 有很多方法可以实现这一目标但是看看EventBus,它可能会简化工作