2012-03-31 64 views
0

我無法將從文本文件讀取的數據傳遞到應用程序中的另一個類。我可以從文件中讀取,但我認爲我需要使用Bundle,但我不知道如何去做。我想讓第二個類處理文本文件中的數據,然後將其顯示在第一個類中。Android:無法將數據從文件讀取到多個類

編輯:我已經想出瞭如何使用意圖從文件中傳遞字符串。我仍在努力解決一些錯誤。

第2編輯:我知道有一個更有效的方法來做到這一點。唯一可以讓它工作的方法是讓MainActivity中的第一個按鈕使用startActivity(intent)來允許secondActivity捆綁從文件中讀取的字符串。

MainActivity.java:

public class MainActivity extends Activity { 

    Button btn; 
    Button bReadFile; 
    TextView tvRead; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     btn = (Button) findViewById(R.id.btnNext); 
     bReadFile = (Button) findViewById(R.id.btnRead); 
     tvRead = (TextView) findViewById(R.id.tvMain); 

     btn.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       //trying to find a way to remove this button 
       Intent intent = new Intent(MainActivity.this, econdActivity.class); 
       startActivity(intent); 
      } 
     }); 

     bReadFile.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       String value = getIntent().getExtras().getString("key"); 
       tvRead.setText(value); 
      } 
     }); 
    } 
} 

secondActivity.java:

public class secondActivity extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     Intent mIntent = new Intent(secondActivity.this, MainActivity.class); 
     mIntent.putExtra("key", readDataFile()); 
     startActivity(mIntent); 
    } 

    public String readDataFile() { 
     String strData = null; 
     InputStream is = null; 
     try { 
      is = getResources().openRawResource(R.raw.data_text); 
      BufferedReader br = new BufferedReader(new InputStreamReader(is)); 
      strData = br.readLine(); 
      is.close(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     return strData; 
    } 
} 
+0

奇怪你在做什麼。從主要活動你稱爲secondactivity,然後在第二個你再次開始maincretivty oncreate()然後有什麼用這個第二個活動。您可以在mainactivity本身中擁有readDataFile()方法。 難道你正在嘗試做什麼是你的實際需求嗎 – Ishu 2012-04-01 06:23:04

+0

我真正想要做的是讓第二個活動句柄讀取文本文件。我已經按照你的建議使用MainActivity來處理文件輸入,但是我想在添加更多內容時將其移動到另一個類。 – 2012-04-01 10:54:19

+0

你的目標是什麼? – Ishu 2012-04-01 10:55:31

回答

0

使用您的要求,下面編輯的代碼 MainActivity.java

public class MainActivity extends Activity { 

    Button btn; 
    Button bReadFile; 
    TextView tvRead; 
    String value; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     btn = (Button) findViewById(R.id.btnNext); 
     bReadFile = (Button) findViewById(R.id.btnRead); 
     tvRead = (TextView) findViewById(R.id.tvMain); 

     btn.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       //trying to find a way to remove this button 
       Intent intent = new Intent(MainActivity.this, secondActivity.class); 
       startActivityForResults(intent,0); 
      } 
     }); 

     bReadFile.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       tvRead.setText(value); 
      } 
     }); 
    } 
    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data){ 
     value = data.getStringExtra("key"); 
} 
} 

爲secondActivity代碼.java

public class secondActivity extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     Intent i = new Intent(); 
    i.putExtra("key", readDataFile()); 
    setResult(RESULT_OK, i); 
    finish(); 
    } 

    public String readDataFile() { 
     String strData = null; 
     InputStream is = null; 
     try { 
      is = getResources().openRawResource(R.raw.data_text); 
      BufferedReader br = new BufferedReader(new InputStreamReader(is)); 
      strData = br.readLine(); 
      is.close(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     return strData; 
    } 
} 
+0

是否解決了您的問題...? – Ishu 2012-04-01 11:58:16

+0

謝謝,這有很大的幫助。它看起來像我正在尋找setResult方法。 – 2012-04-01 12:56:03