如何从android中的文本文件onListItemClick获取数据?

如何从android中的文本文件onListItemClick获取数据?,android,text,file-management,Android,Text,File Management,我目前正在开发一个应用程序,为minecraft制作mods。我的应用程序有一个简单的文件管理器,当用户选择一个文件时,我想在其中获取文件数据并将其放在另一个EditText活动中。我不知道如何获取数据并将其发送到另一个活动中的EditText 编辑: 这是我的OpenScript.class,我试图在另一个活动中以EditText的形式将数据推送到文件中,但我不知道如何做 public class OpenScript extends ListActivity { private Li

我目前正在开发一个应用程序,为minecraft制作mods。我的应用程序有一个简单的文件管理器,当用户选择一个文件时,我想在其中获取文件数据并将其放在另一个EditText活动中。我不知道如何获取数据并将其发送到另一个活动中的EditText

编辑: 这是我的OpenScript.class,我试图在另一个活动中以EditText的形式将数据推送到文件中,但我不知道如何做

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

当用户在活动a上选择一个文件时,您希望打开一个文件并读取其内容,然后将内容发送到活动B,在活动B中它将显示在EditText小部件中。对吧?

不需要将文件内容发送到活动B。只需将文件URI作为意图的一部分发送到活动B,然后在那里执行文件读取操作并填充EditText。那要干净得多

如何从android中的文本文件onListItemClick获取数据

并在onListItemClick方法中调用上述函数

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

        }
    }

你能粘贴一些代码吗?到目前为止你尝试了什么?我现在粘贴了我的代码。我怎么能继续这样做呢?我似乎不明白如何将URI发送到我的第二个活动。我知道如何从给定路径读取文件,但不知道用户选择文件时如何读取。我可以发送字符串吗?例如:Intent i=newintent(this,ScriptWriter.class);i.putExtra(“代码”/*?*/);startActivityForResult(i,1);对以字符串形式发送文件URI。根据需要使用startActivity或startActivityForResult。然后我是否只发送文件的位置?我还添加了第二个代码,因为当我单击文件时,应用程序只是强制关闭,但我不知道出了什么问题。
@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){

        }
    }
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);
    }
private String getFileAtPosition(int position){

return items.get(position);

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

        }
    }