王学岗Fragment(六)——Activity与fragment之间数据的传递

这里写图片描述
我们说一下我们的项目需求,当我们点击fragment中的条目的时候,activitu中的textView显示相应的文本。

package com.example.fragment;

import android.os.Bundle;
import android.app.Activity;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;

public class MainActivity extends FragmentActivity implements OnItemClickListener {
    private String hobby = "guy";
    private MyListFragment mlf;
    private TextView tv_text;

    public String getHobby() {
        return hobby;
    }

    public void setHobby(String hobby) {
        this.hobby = hobby;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv_text = (TextView) findViewById(R.id.tv_text);
        //拿到fragment的数据
        FragmentManager manager=getSupportFragmentManager();
        mlf = (MyListFragment) manager.findFragmentById(R.id.fg);
    }
    //该方法调用表示该activity可见
    @Override
    protected void onResume() {
        int count=mlf.getListAdapter().getCount();
//      Toast.makeText(this, "hobbies"+count, 3000).show();
        System.out.println("hobbies"+count);
        super.onResume();
    }
    // 当我们点击fragment的时候,改变Activity中的textView内容
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
        String hobby=mlf.hobbies[position];
        tv_text.setText(hobby);

    }
}
package com.example.fragment;

import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class MyListFragment extends ListFragment  {
    String[] hobbies = new String[] { "a", "b", "c", "d", "e", "f" };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // 得到所依附的activity
        MainActivity activity = (MainActivity) getActivity();
        // MainActivity的hobby值可以拿到,不是空值。
        hobbies[3] = activity.getHobby();

        // 产生空指针异常,以为tv是空值,至于为什么参照fragment的生命周期自己思考
        // TextView tv=(TextView) activity.findViewById(R.id.fg);
        // hobbies[4]=tv.getText().toString();

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
                android.R.layout.simple_list_item_1, android.R.id.text1,
                hobbies);
        this.setListAdapter(adapter);
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        MainActivity activity = (MainActivity) getActivity();
//      TextView tv = (TextView) activity.findViewById(R.id.tv_text);
//      hobbies[4] = tv.getText().toString();
        getListView().setOnItemClickListener(activity);
        super.onActivityCreated(savedInstanceState);
    }



}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/tv_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <fragment
        android:name="com.example.fragment.MyListFragment"
        android:id="@+id/fg"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

</LinearLayout>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值