2014年7月10日 星期四

Android Tab分頁 手動更新其中一個畫面

http://developer.android.com/training/implementing-navigation/lateral.html


package com.foxlink.wearable.hrm;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

import com.foxlink.wearable.R;

public  class HistoryListSectionFragment extends Fragment {

private View rootView;
static int createCount2 = 0;
private static Context contextAct;
private TableLayout tl;
private LayoutInflater inf;
private ViewGroup con;
private Bundle bl;

public HistoryListSectionFragment(Context con , Display dis){
       HistoryListSectionFragment.contextAct = con;
}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        this.inf = inflater;
        this.con = container;
        this.bl = savedInstanceState;
     
    readHRDatas();
    Toast.makeText(contextAct, "onCreateView",
        Toast.LENGTH_SHORT).show();
        return rootView;
    }
 
    public void callRefresh(){
//    onCreateView(inf, con, bl);
   getFragmentManager().beginTransaction().detach(this).attach(this).commit();
    }
 
 
    public void readHRDatas(){
    rootView = inf.inflate(R.layout.activity_historylist, con, false);

 
    tl = (TableLayout) rootView.findViewById(R.id.hrtable1);
   /* Create a new row to be added. */
 
   File[] files = contextAct.getFilesDir().listFiles();
 
   StringBuilder sb = new StringBuilder();
 
   for (int i = 0 ; i < files.length  ; i++ ){
            sb.append(files[i].getName()+",");
    String[] splitName;
   
    if((splitName = files[i].getName().split("\\s+")).length == 3  && splitName[0].contains("_")){
    insertHRRecord(i , splitName , files[i].getName() );
    }
   }
 
    Toast.makeText(contextAct, "all file:"+sb.toString(),
    Toast.LENGTH_SHORT).show();
    }
 
 
 
    /**
* add one record for even saved file
* @param count
* @param fileNames
* @param originalName
*/
public void insertHRRecord(int count , String[] fileNames ,final String originalName){
   TableRow tr = new TableRow(contextAct);
 
   tr.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
 
   if(count%2 == 0){
       tr.setBackgroundColor(Color.GRAY);
   }

   TextView t1 = new TextView(contextAct);
   t1.setHeight(150);
   String[] splitTime = fileNames[0].split("_");
   t1.setText("  "+splitTime[0] + "\n      " +splitTime[1]);
   t1.setGravity(Gravity.LEFT);
   t1.setTextSize(18);
   t1.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
   tr.addView(t1);
 
   TextView t2 = new TextView(contextAct);
   t2.setText(fileNames[1]);
   t2.setGravity(Gravity.LEFT);
   t2.setTextSize(18);
   t2.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
   tr.addView(t2);
 
   TextView t3 = new TextView(contextAct);
   t3.setText(fileNames[2]);
   t3.setGravity(Gravity.LEFT);
   t3.setTextSize(18);
   t3.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
   tr.addView(t3);
 
   tr.setOnClickListener(new OnClickListener() {
       @Override
        public void onClick(View v) {

              //Get the text file
              File readFile = new File(contextAct.getFilesDir(), originalName);

              //Read text from file
              List<Integer> intList = new ArrayList<Integer>();

              try {
                 BufferedReader br = new BufferedReader(new FileReader(readFile));
               
                 String line;

                 while ((line = br.readLine()) != null) {
                  intList.add(Integer.valueOf(line));
                 }
              }
              catch (IOException e) {
                 //You'll need to add proper error handling here
              }
             
              int[] ret = new int[intList.size()];
             
              for (int i=0; i < ret.length; i++)
                {
                    ret[i] = intList.get(i).intValue();
                }

             
              //send value to draw graph for this file
               Intent it = new Intent();
               it.setClass(HistoryListSectionFragment.contextAct, HistoryList_Graph.class);
               it.putExtra("hrvalues", ret);
               startActivity(it);

        }
});
 
   /* Add row to TableLayout. */
   tl.addView(tr, new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));
}
}

沒有留言:

張貼留言