不断更新片段中的textview

时间:2015-08-21 12:04:00

标签: android android-fragments android-fragmentactivity

我想定期使用主活动中的值更新片段textview。我使用过接口方法,但是不能让textview定期更新吗?

活动

public class main2 extends ActionBarActivity implements readingdata{

  Button Loge,read;
  static int count=2;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);
    Loge=(Button)findViewById(R.id.btnlog);
    FragmentTransaction ft = getFragmentManager().beginTransaction();
    Fragment frag = new UI();
    ft.add(R.id.fragment, frag, "UIfrag");
    ft.addToBackStack(null);
    ft.commit();

    Timer mytime=new Timer();
        mytime.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                      count=count+1;
                       Log.e("working", "updating");
                       passData();


        }
    }, 1 * 05 * 500, 1 * 10 * 500);

   @Override
     public int passData() {
          return count;

     }


}

片段

public class UI extends Fragment{

    TextView txttemp,txthumid;
    readingdata datais;
      public static int res=0;
     String mydata="2";
         @Nullable
       @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,                  Bundle savedInstanceState) {
                                                                                                    View rootview=inflater.inflate(R.layout.uilayout,container,false);
     txttemp=(TextView)rootview.findViewById(R.id.tvhumid);
    txthumid=(TextView)rootview.findViewById(R.id.tvtemp);
    Log.e("working", "oncreatefrag");
    Log.e("res changedornot",String.valueOf(res));
    txthumid.setText(String.valueOf(res));
    return rootview;
}

@Override
   public void onAttach(Activity activity) {
    super.onAttach(activity);
    datais=(readingdata)activity;
    res=datais.passData();
  }     }


      interface readingdata
       {
         public int passData();
        }

2 个答案:

答案 0 :(得分:0)

试试这个

活动

public class main2 extends ActionBarActivity {

    Button Loge,read;
    static int count=2;
    Fragment frag = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        Loge=(Button)findViewById(R.id.btnlog);
        FragmentTransaction ft = getFragmentManager().beginTransaction();
        frag = new UI();
        ft.add(R.id.fragment, frag, "UIfrag");
        ft.addToBackStack(null);
        ft.commit();

        Timer mytime=new Timer();
        mytime.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                count=count+1;
                Log.e("working", "updating");
                sendDataToFragment();
            }
        }, 1 * 05 * 500, 1 * 10 * 500);

    public void sendDataToFragment(){
        if(frag != null){
            ((readingdata)frag).passData(count);
        }  
        else
            Log.e("Error", "Fragment is null");
    } 
}

片段

public class UI extends Fragment implements readingdata{
    TextView txttemp,txthumid;
    readingdata datais;
    public static int res=0;
    String mydata="2";
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){                                                                               
        View rootview=inflater.inflate(R.layout.uilayout,container,false);
        txttemp=(TextView)rootview.findViewById(R.id.tvhumid);
        txthumid=(TextView)rootview.findViewById(R.id.tvtemp);
        Log.e("working", "oncreatefrag");
        Log.e("res changedornot",String.valueOf(res));
        txthumid.setText(String.valueOf(res));
        return rootview;
    }    

    @Override
    public void passData(int value) {
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                txthumid.setText(String.valueOf(value));
            }
        });    
    }
}

接口

interface readingdata
{
     public void passData(int count);
}

答案 1 :(得分:0)

您刚刚在 main2 中实现了 readingdata 接口,但您没有做任何事情来更新覆盖方法 passData 中片段中的textview

删除所有接口并在片段中编写一个方法并从活动中调用该方法..

public class UI extends Fragment{

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    // What ever you want to do.
    return super.onCreateView(inflater, container, savedInstanceState);
}

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    //Dont update Textview here as it is called once the fragment is associated with its activity.
}


//call this method from activity.
public void updateTextViewInFragment(int count){
    //update your textview here
    txthumid.setText(String.valueOf(count));
}

//Remove Interfaces

 }

从活动

调用updateTextViewInFragment
Fragment frag;

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Loge=(Button)findViewById(R.id.btnlog);
FragmentTransaction ft = getFragmentManager().beginTransaction();
frag = new UI();
ft.add(R.id.fragment, frag, "UIfrag");
ft.addToBackStack(null);
ft.commit();

Timer mytime=new Timer();
    mytime.scheduleAtFixedRate(new TimerTask() {
        @Override
        public void run() {
                  count=count+1;
                  if(frag != null && frag.isAdded() && !frag.isRemoving()){
                      frag.updateTextViewInFragment(count);
                  }


    }
}, 1 * 05 * 500, 1 * 10 * 500);

}