将数据从一个片段传递到android中的下一个片段

时间:2013-02-14 05:32:43

标签: java android android-fragments onitemclicklistener pass-data

在我的一个项目中,我正在使用2个片段。一个片段正在显示一个项目列表。所以我想在第一个片段中选择主题时,应该在另一个片段中更改细节。我能够通过的数据从一个片段到另一个片段,但视图没有改变。在哪种方法中我应该实现第二个片段的视图,以便它可以相应地改变。 请给我一些很好的教程或示例。

这里我要开发一个android示例,我必须运行应用程序意味着在我的第一个片段的水平列表视图上显示类别名称。我必须单击任何类别意味着如何在第二个片段上传递类别名称。请释放给我解决这个问题。

我使用了以下代码:

public class Home extends FragmentActivity{

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

第一个片段代码:

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

如何在第二个活动中显示类别名称...请给我这些代码...

修改

您好我在第一次活动中更改了代码:

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

关于下一个活动:

public void setText(String item) {
 TextView view = (TextView) getView().findViewById(R.id.cate);
 view.setText(item);
  }
直接提到if(map.containsValue(“notchbolly”))上的名字时,它已经很好了。但是我必须直接显示名称而不提及名称。如果我必须点击任何类别意味着类别名称显示在下一个片段。我可以做什么?????

2 个答案:

答案 0 :(得分:1)

使用接口方法,我们可以在片段到片段之间传输数据

在fragmentA中创建接口并将数据发送到此接口并在fragmentB中实现此接口方法并获取所需数据

以下代码对您来说非常有用

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

        }
    }

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

    }

答案 1 :(得分:0)