如何在editbox的另一个活动上显示内部存储项目

时间:2014-10-23 05:26:42

标签: java android android-edittext internal

我是一名新的Android开发人员。我的问题是项目如何从内部存储显示到编辑框的另一个活动。内部存储文件包含(名称,年龄,位置)该文件项显示在另一个活动的三个编辑框中。

在我的项目中,用户从收件箱中选择MSG,MSG显示在activity_main.xml的文本视图上。当我单击保存按钮时,该文件存储在内部存储器中,但是当我单击读取按钮时,所有项目都显示在一个编辑框中不是单独从内部存储三个编辑框。

例如,在我的内部存储文件中包含(abc,14,ANDROID DEVLOPER)所有项目分别显示另外三个编辑框的活动。
     Mainctivity.java

Read.setOnClickListener(new View.OnClickListener() {

            //private Context context;

            @Override
            public void onClick(View v) {
                //Intent intent = new Intent(getApplicationContext(), MessageBox.class);
                // TODO Auto-generated method stub
                //Intent intent = new Intent(context,MessageBox.class);
                 try{
                     FileInputStream fin = openFileInput(file);
                     int c;
                     String temp="";
                     while( (c = fin.read()) != -1){
                        temp = temp + Character.toString((char)c);
                        Intent in = new Intent(getApplicationContext(),data.class);

                        //String msg = null;
                        in.putExtra("Msg_Detail", temp); 
                     startActivity(in);

                    // et.setText(temp);
                     Toast.makeText(getBaseContext(),"file read",
                     Toast.LENGTH_SHORT).show();
                     }


                  }catch(Exception e){

                  }
        }
    });}

     public void save(View view){
          data = tv.getText().toString();
          try {
             FileOutputStream fOut = openFileOutput(file,MODE_WORLD_READABLE);
             fOut.write(data.getBytes());
             fOut.close();
             Toast.makeText(getBaseContext(),"file saved",
             Toast.LENGTH_SHORT).show();
          } catch (Exception e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
          }
       }
**data.java**
setContentView(R.layout.data);
        et11 = (EditText)(findViewById(R.id.eText123));
        Intent intent = getIntent();
        String msg = intent.getStringExtra("Msg_Detail");
        //String msg = intent.getExtras().getString("Msg_Detail");
        ((EditText)findViewById(R.id.eText123)).setText(msg);
        //et11.setText(msg);   

activity_main.xml contain save, read and text view and data.xml contain three edit box(name,age,position).how that msg display on edit box of data.xml from internal storage.
Internal file generate and user select msg display on text view of activity_main.xml of another project but how internal file item display on edit box.
                problem is when I click read button all data item display on one edit box.

1 个答案:

答案 0 :(得分:0)

这个片段......

 while( (c = fin.read()) != -1){
                    temp = temp + Character.toString((char)c);
                    Intent in = new Intent(getApplicationContext(),data.class);

                    //String msg = null;
                    in.putExtra("Msg_Detail", temp); 
                 startActivity(in);

                // et.setText(temp);
                 Toast.makeText(getBaseContext(),"file read",
                 Toast.LENGTH_SHORT).show();
                 }
除了文件流读取第一个字节并且文件流保持打开状态之外,

实际上只执行其他操作,而是启动新活动并暂停当前活动。这就是为什么你可能在data.java中得不到任何东西。

您应该等到文件流完成读取文件以启动新活动并确保关闭该流。因此,移动创建意图的代码,启动活动并显示while循环中的toast ......

FileInputStream in = null;
int c;
String temp="";

try{
    fin = openFileInput(file);

    while( (c = fin.read()) != -1){
        temp = temp + Character.toString((char)c);
    }
}
catch(Exception e){...}
finally{
    if(in != null)
        in.close();
}

Intent in = new Intent(getApplicationContext(),data.class);
in.putExtra("Msg_Detail", temp); 

startActivity(in);

Toast.makeText(getBaseContext(),"file read", Toast.LENGTH_SHORT)
    .show();