简体   繁体   English

在Android中将数据从一个片段传递到下一个片段

[英]Passing data from one fragment to next fragment in android

In one of my project I am using 2 fragments.One fragment is displaying a list of items.So I want when the topic will be selected in first fragment the details should be changed in another fragment.Data I am able to pass from one fragment to other but the view is not changing.In which method I should implement the view of second fragment so that it can change accordingly. 在我的一个项目中,我正在使用2个片段。一个片段正在显示项目列表,所以我想在第一个片段中选择主题时,应该在另一个片段中更改详细信息。我可以从一个片段传递数据到另一个视图,但视图不变。在哪种方法中,我应该实现第二个片段的视图,以便可以相应地更改。 Please give me good tutorial or example for these. 请给我很好的教程或示例。

Here I have to develop one android example, I have to run the app means displayed category name well on horizontal listview of my first fragment.I have to click any category means how can I pass the category name on 2nd fragment.Please give me solution for this problem. 在这里我必须开发一个android示例,我必须运行该应用程序,这意味着在第一个片段的水平listview上很好地显示了类别名称。我必须单击任何类别意味着我该如何在第二个片段上传递类别名称。请给我解决方案对于这个问题。

I have used below code: 我用下面的代码:

public class Home extends FragmentActivity{

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
}

1st fragment code: 第一个片段代码:

public class MainActivity extends Fragment {
static final String URL = "http://dev2.mercuryminds.com/webservices/new_feed_articls.xml";

 static String KEY_CATEGORY = "Categories";

 static final String KEY_TITLE = "Category";
 LazyAdapter adapter;

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


    final ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
    XMLParser parser = new XMLParser();
        String xml = parser.getXmlFromUrl(URL); // getting XML from URL
        Document doc = parser.getDomElement(xml); // getting DOM element

        NodeList nl = doc.getElementsByTagName(KEY_TITLE);


        // looping through all song nodes &lt;song&gt;
        for (int i = 0; i < nl.getLength(); i++) {
            // creating new HashMap
            HashMap<String, String> map = new HashMap<String, String>   ();
            Element e = (Element) nl.item(i);
            map.put( KEY_TITLE,((Element)nl.item(i)).getAttribute("name"));

           // map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE));
            songsList.add(map);
        }
        HorizontalListView list = (HorizontalListView) view.findViewById(R.id.listView1);
    adapter = new LazyAdapter(getActivity(), songsList);
      list.setAdapter(adapter);

       list.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            HashMap<String, String> map = songsList.get(position);
            Intent in = new Intent();
            in.setClass(getActivity(), SubCate.class);
           // in.putExtra(KEY_TITLE, map.get(KEY_TITLE));

                     startActivity(in); 
        }  

     });
        return view;
    }}

How can i display category name on 2nd Activity...pls give me code for these... 我如何在第二个活动中显示类别名称...请给我这些代码...

EDIT: 编辑:

Hi i have changed code like on my first activity: 嗨,我在第一次活动中更改了代码:

   HashMap<String, String> map = songsList.get(position);
            SubCate frag = (SubCate) getFragmentManager().findFragmentById(R.id.frag_2);
            if (frag != null && frag.isInLayout()) {
            frag.setText(getCapt(map));
            }
            }
            private String getCapt(HashMap<String, String> map) {
            if (map.containsValue("notchbolly")) {
            return "dadad";
            }
                return "veeman"; 

on next activity: 在下一个活动中:

public void setText(String item) {
 TextView view = (TextView) getView().findViewById(R.id.cate);
 view.setText(item);
  }

its woked well while directly mention the name on if (map.containsValue("notchbolly")).but i have to display the name without mention name directly.if i have to click any category means that category name is displayed on next fragment.how can i do ????? 当直接在if(map.containsValue(“ notchbolly”))上提及名称时,它的功能很好。但是我必须直接显示名称而不提及名称。如果我必须单击任何类别,则意味着该类别名称显示在下一个片段上。我能怎么做 ?????

Using interface method we can transfer data between fragment to fragment 使用接口方法,我们可以在片段之间传输数据

In fragmentA create interface and sent data to this interface and implement this interface method in fragmentB and get required data 在fragmentA中创建接口并将数据发送到此接口,并在fragmentB中实现此接口方法并获取所需的数据

the following code is useful for u 以下代码对您有用

Code in FragmentA: FragmentA中的代码:

    public class FragmentA extends Fragment  {

    // interface method
    public    InterfaceDataCommunicatorBetweenfragmentAToFragmentB     iInterfaceDataCommunicatorBetweenfragmentAToFragmentB;

    // Data to send
    String mDataString;

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

        // use this line where u send data  
        iInterfaceDataCommunicatorBetweenfragmentAToFragmentB.upDatedData(mDataString);

        View view = inflater.inflate(R.layout.sent_box, container, false);


        return view ;
    }

    /**
         * Interface method to send data to fragmentB
         * 
         */
        public interface InterfaceDataCommunicatorBetweenfragmentAToFragmentB                                       {
            public void upDatedData(String data);

        }
    }

Code in FragmentB: FragmentB中的代码:

   class FragmentB extends Fragment implements InterfaceDataCommunicatorBetweenfragmentAToFragmentB {

    // interface method
    public InterfaceDataCommunicatorBetweenfragmentAToFragmentB iInterfaceDataCommunicatorBetweenfragmentAToFragmentB;

    // fragmentA data
    String mgetUpdatedData


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


            View view = inflater.inflate(R.layout.get_box, container, false);


        return view ;
    }

   /**
     * Interface method coming from fragment a to fragmentB after  implement interface in fragmentB
     */
    public void upDatedData(String data) {
        this.mgetUpdatedData = data;
    }

    @Override
    public void onAttach(Activity activity) {
        // TODO Auto-generated method stub
        super.onAttach(activity);
        try {
            iInterfaceDataCommunicatorBetweenfragmentAToFragmentB = (InterfaceDataCommunicatorBetweenfragmentAToFragmentB ) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement interface");
        }

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM