如何从片段移动到活动

时间:2014-08-28 14:14:13

标签: android android-fragments android-fragmentactivity

现在我正处于片段活动中。现在我想从片段转到Activity。 这是我的代码告诉我的?

public class People extends BaseFragment implements OnClickListener {
    static String name;
    ListView listview;
    TextView et;
    public static String username;
    static public List<SuccessStoreyItem> success_list3 = new ArrayList<SuccessStoreyItem>();
    public static int people_index ;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
        // TODO Auto-generated method stub

        View view= inflater.inflate(R.layout.home,null);
        ParserResult pr = new ParserResult();
        Button b=(Button) view.findViewById(R.id.home1);
        listview = (ListView)view.findViewById(R.id.lv_home);

        et=(TextView) view.findViewById(R.id.editText1);
        b.setOnClickListener(this);

        if(ParserResult.success_list2.size() != 0){
            success_list3 = ParserResult.success_list2;
        }

        listview.setAdapter(new Adapters(success_list3 ,MainActivity.mainactivity_context));
        ParserResult.success_list2 = new ArrayList<SuccessStoreyItem>();


        listview.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                // TODO Auto-generated method stub
//              Toast.makeText(getActivity(), "fuck you", Toast.LENGTH_LONG).show();
                Profile frg3 = new Profile();
                Bundle b3 = new Bundle();
                username = "";
                username = success_list3.get(position).getStorey_id();
                Intent i = new Intent(MainActivity.mainactivity_context,Profile_otheruser.class);
                startActivity(i);
//              b3.putString("username", username);
//              b3.putInt("index_people", position);
                people_index = position;
                frg3.setArguments(b3);
//              ((MainActivity)getActivity()).launchNewFragment(frg3, R.id.tab2);
//              Toast.makeText(getActivity(), "Name :"+name ,1).show();
            }
        });



        return view;
    }
    public void onListItemClick(ListView l, View v, int position, long id) {
        //Do your stuff..

    }
    @Override
    public void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        setTitle(R.string.Home);
    }
    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        name=et.getText().toString();
        Bundle bundle = new Bundle();
        bundle.putString("Name", name);

    Toast.makeText(getActivity(), "Name :"+name ,1).show();


        //((MainActivity)getActivity()).launchNewFragment(new Home2(),R.id.tab1);
    }


}

文字是:     告诉我,我的方法(context,activity.class),启动活动(i)类方法是对的吗?

1 个答案:

答案 0 :(得分:0)

Fragment包含一个接口。 Activity实现了接口。这样,片段可以与实现接口的任意数量的活动一起使用。

public class SettingMap extends Fragment implements OnCheckedChangeListener,
    android.widget.CompoundButton.OnCheckedChangeListener {



public interface BestRidesSettingsDialogListener {
    // change the map type
    void onMapSettingsChange(int mapType);
}

片段中的某处安全地检查活动是否已实现接口,然后在活动上执行代码

    Activity a = getActivity();
    BestRidesSettingsDialogListener activity = (BestRidesSettingsDialogListener.class.isAssignableFrom(a
            .getClass())) ? (BestRidesSettingsDialogListener) a : null;
    if (activity != null) {
        activity.onMapSettingsChange(mapType);
    }

活动实现界面

public class KmlReader extends ActionBarActivity implements
    BestRidesFollowDialogListener {


@Override
public void onMapSettingsChange(int mapType) {
    if (mMap != null) {
        mMap.setMapType(mapType);
    }
}