如何在内部存储中显示多个微调器上的文本项目

时间:2014-09-25 02:49:41

标签: java android storage android-spinner

当我点击写入按钮而不是在内部存储中创建文本文件但是当我阅读它们时,如何在微调器上显示文本文件项目。在文本文件中包含name,age,std项,当我单击读取按钮时,该项显示在特定的微调器上。         在文本文件中的项目是(name,age,std)         三个微调器(名称,年龄,标准)         当我点击阅读按钮时,所有文本文件项(名称,年龄,标准)显示在每个微调器的另一个活动上。         问题是文本文件项如何显示每个微调器。

    Mainactivity.java
    btnWriteSDFile = (Button) findViewById(R.id.b2);
            btnWriteSDFile.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                Jour=display.getText().toString();
                FILENAME = display.getText().toString();
                if (FILENAME.contentEquals("")){
                    FILENAME = "UNTITLED";
                }

                // write on SD card file data in the text box
                try {
                     FileOutputStream fOut = openFileOutput(FILENAME,MODE_APPEND);
                     fOut.write(display.getText().toString().getBytes());
                     fOut.close();
                     Toast.makeText(getBaseContext(),"file saved",
                     Toast.LENGTH_SHORT).show();
                  } catch (Exception e) {
                     // TODO Auto-generated catch block
                     e.printStackTrace();
                  }
    btnReadSDFile = (Button) findViewById(R.id.b3);
            btnReadSDFile.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // write on SD card file data in the text box
                int id = v.getId();
                for (int i = 0; i < activities.length; i++) {
                    if (id == getResources().getIdentifier("b" + i, "id",
                            "com.example.pp")) {
                        Intent intent = new Intent("com.example.pp."
                                + activities[i]);
                        startActivity(intent);
                    }
                }
            }       }); 

list.java   


 @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.list);
            sp1=(Spinner) findViewById(R.id.spinner1);
            sp2=(Spinner) findViewById(R.id.spinner2);
            sp3=(Spinner) findViewById(R.id.spinner3);
            entry = (TextView)findViewById(R.id.TextView123);
            getFilesnames();        
        }
        private void getFilesnames() {
            // TODO Auto-generated method stub
            String[] filenames=getApplicationContext().fileList();
            List<String> list=new ArrayList<String>();
            for(int i=0;i<filenames.length;i++){
                list.add(filenames[i]);

            }
            /*if(list.size() <= 0) {
                   int i = 0;
                list.add("You string.....");
                }*/

            ArrayAdapter<String> filenameAdapter=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,list);
            sp1.setAdapter(filenameAdapter);
        }
        public void onClick(View v) {
            // TODO Auto-generated method stub
            String selectFile = String.valueOf(sp1.getSelectedItem());
            openFile(selectFile);
        }


        private void openFile(String selectFile) {
            // TODO Auto-generated method stub
            String value = "";
            FileInputStream fis;

            try {
                fis = openFileInput(selectFile);
                byte[] input = new byte[fis.available()];
                while(fis.read(input) != -1){
                    value += new String(input);
                }
                fis.close();
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            entry.setText(value);

        }

0 个答案:

没有答案