-1

I am currently working on an app to make mods for minecraft. My app has a simple file manager where I want to get the file data and put it in another activity in an EditText, when the user selects a file. I don't know how to get the data and send it to an EditText in another activity.

EDIT: This is my OpenScript.class which I'm trying to push the data inside the file in a EditText on another activity but I have no clue how to do that.

public class OpenScript extends ListActivity {
    private List<String> items =null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.scripts_list);

        getFiles(new File("/sdcard/ChatoGuy1/ModPE Scripter/Scripts").listFiles());
    }
    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        try{
             Intent i = new Intent(this, ScriptWriter.class);
        i.putExtra("code", /* ? */);
        startActivityForResult(i, 1);
        }
        catch(Exception e){

        }
    }

    private void getFiles(File[] files) {
        items = new ArrayList<String>();
        for (File file : files) {
            items.add(file.getPath());
        }
        ArrayAdapter<String> fileList = new ArrayAdapter<String>(this, R.layout.file_list_row, items);
        setListAdapter(fileList);
    }
}

Second Activity:

public class ScriptWriter extends Activity{
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.code_editor);

        final EditText editText = (EditText) findViewById(R.id.codeEditor);
        final Button codeSave = (Button) findViewById(R.id.bCodeSave);
        //get file
         Intent intent = getIntent();
        String test = intent.getExtras().getString("code");
        //read file
        StringBuilder text = new StringBuilder();
        try {
            BufferedReader br = new BufferedReader(new FileReader(test));
            String line;

            while ((line = br.readLine()) != null) {
                text.append(line);
                text.append('\n');
            }
        }
        catch (IOException e) {
            Toast.makeText(this, "File does not exist.", Toast.LENGTH_LONG).show();
        }
        editText.setText(text);
    }
}
tshepang
  • 12,111
  • 21
  • 91
  • 136
user3055552
  • 116
  • 8

2 Answers2

0

When the user selects a file on Activity A, you want to open a file and read it's contents and send the contents to Activity B where it will be displayed in an EditText widget. Right?

There's no need to send the file contents to Activity B in an intent. Just send the file URI as part of the intent to Activity B and then do the file read operation there and populate your EditText. That's much cleaner.

HTH.

VJ Vélan Solutions
  • 6,434
  • 5
  • 49
  • 63
  • How could I go about doing that? I can't seem to understand on how to send the URI to my second activity. I know how to read a file from a given path but not when user selects a file. – user3055552 Dec 02 '13 at 04:35
  • Could I be able to send a string? For example: Intent i = new Intent(this, ScriptWriter.class); i.putExtra("code", /* ? */); startActivityForResult(i, 1); – user3055552 Dec 02 '13 at 04:50
  • yes. send the file URI as a string. use startActivity or startActivityForResult depending on your needs. – VJ Vélan Solutions Dec 02 '13 at 04:56
  • Then do I just send the position of the file? – user3055552 Dec 02 '13 at 05:10
  • I've added my second code as well because when I click on file the app just force closes but I don't know what's wrong. – user3055552 Dec 02 '13 at 05:18
0

How to get data from text file onListItemClick in android?

@Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        try{
           TextView tv = v.findViewById(<id_of_infliated_textView_in_your_adapter_class>)
           String path - tv.getText().toString();
           Intent i = new Intent(this, ScriptWriter.class);
           i.putExtra("code", path );
           startActivityForResult(i, 1);
        }
        catch(Exception e){

        }
    }

The above code works when you use the custom adapter (creating your own adapter class and infiliating layout file).

In your case it is quite simple.

How could I go about doing that? I can't seem to understand on how to send the URI to my second activity. I know how to read a file from a given path but not when user selects a file

To get the file path

As you already declare your Array list globally, and using this method in your code.

private void getFiles(File[] files) {
        items = new ArrayList<String>();
        for (File file : files) {
            items.add(file.getPath());
        }
        ArrayAdapter<String> fileList = new ArrayAdapter<String>(this, R.layout.file_list_row, items);
        setListAdapter(fileList);
    }

make one more method saying "getFileAtPosition"

private String getFileAtPosition(int position){

return items.get(position);

}

And call the above function in your onListItemClick method

@Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        try{
             Intent i = new Intent(this, ScriptWriter.class);
        i.putExtra("code",getFileAtPosition(position));
        startActivityForResult(i, 1);
        }
        catch(Exception e){

        }
    }
Jeremy D
  • 4,787
  • 1
  • 31
  • 38
vinay kumar
  • 1,451
  • 13
  • 33