0

我无法将从文本文件读取的数据传递到应用程序中的另一个类。我可以很好地从文件中读取,但我认为我需要使用 Bundle,但我不知道该怎么做。我想让第二个类处理来自文本文件的数据,然后在第一类中显示它。

编辑:我已经弄清楚如何使用意图从文件中传递字符串。我仍在努力解决一些错误。

第二次编辑:我知道有一种更有效的方法。我可以让它工作的唯一方法是让 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);
            }
        });
    }
}

第二活动.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;
    }
}
4

1 回答 1

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;
    }
}
于 2012-04-01T11:57:44.850 回答