将文本从活动传递到另一个片段

时间:2015-05-30 10:53:12

标签: android bundle

在我的Listview中,我设置了OnItemClickListener来获取该位置的纬度和经度,我将其传递给另一个片段调用AddEvent。以下是我的代码。

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

                    double lat = ((Address)parent.getItemAtPosition(position)).getLatitude();
                    double lon = ((Address)parent.getItemAtPosition(position)).getLongitude();
                    String loc = "lat: " + lat + "\n" + "lon: " + lon;


                    AddEvent addEvent = new AddEvent();
                    Bundle b = new Bundle();
                    b.putString("location", loc);
                    addEvent.setArguments(b);

                    getSupportFragmentManager().beginTransaction().replace(R.id.container2, addEvent, "Location").commit();
                    finish();
                }
            });

以下是AddEvent.java中的代码。我已经像下面的代码一样设置了getArguments.getString()。

@Override

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

    View rootView = inflater.inflate(R.layout.add_event, container, false);

    btnTimePicker = (Button) rootView.findViewById(R.id.btntime);
    btnTimePicker.setOnClickListener(this);

    btnDatePicker = (Button) rootView.findViewById(R.id.btndate);
    btnDatePicker.setOnClickListener(this);

    btnLocation = (Button) rootView.findViewById(R.id.btnlocation);
    btnLocation.setOnClickListener(this);

    locationtxt = (EditText)rootView.findViewById(R.id.location_text);

    Bundle b = getArguments();
    if(b != null){
        locationtxt = (EditText)rootView.findViewById(R.id.location_text);
        String location = getArguments().getString("location");
        locationtxt.setText(location);


    }


    return rootView;

}

为什么我设置无法将文本传递到Edittext框?没有错误显示并且没有任何力量关闭完全运行。 Jux什么时候返回AddEvent片段。 location_text仍为空白。

0 个答案:

没有答案