How to get data from text file onListItemClick in android?

767 views Asked by At

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);
    }
}
2

There are 2 answers

0
vinay kumar On BEST ANSWER

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){

        }
    }
5
VJ Vélan Solutions On

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.