2012-03-31 58 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