Java activity names when getApplicationContext().fileList()

Java activity names when getApplicationContext().fileList()

在我的应用程序中 activity,它会调用 geApplication().fileList() 列出应用程序文件中的所有文件。

在我的 Elephone P8000 phone 中,我成功地创建了新文件,没有出现任何错误,但是当我在我的三星 phone 上尝试时,出现了这个问题

带"rList-asia.sumikawa"的文件不是我创建的。

其实从文件末尾的名字来看,"MainActivity",就是我activity的名字。

我该如何解决这个问题?

编辑

我的全 Java 代码

public class addressActivity extends Activity {

ListView listView;

String[] SavedFiles;

/**
 * Called when the activity is first created.
 */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_address);

    listView = (ListView) findViewById(R.id.listView);

    addData();
    editData();
    ShowSavedFiles();
    editContent();
    setMultiDelete();
    listView.setOnItemClickListener(getURLOnItemClickListener);
}

void ShowSavedFiles() {
    //SavedFiles = getApplicationContext().fileList();
    //SavedFiles = getApplication().fileList();
    SavedFiles = addressActivity.this.fileList();
    ArrayAdapter<String> adapter
            = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1,
            SavedFiles);

    listView.setAdapter(adapter);
    if (adapter.isEmpty()) {
        Toast.makeText(getBaseContext(), "There is no Address !", Toast.LENGTH_LONG).show();
    }
}

OnItemClickListener getURLOnItemClickListener
        = new OnItemClickListener(){

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
                            long id) {
        // TODO Auto-generated method stub
        String clickedFile = (String) parent.getItemAtPosition(position);
        getURL(clickedFile);
    }

};

void getURL(final String file){
    if (clickAble == true){
        FileInputStream fis;
        String content = "";
        try {
            fis = openFileInput(file);
            byte[] input = new byte[fis.available()];
            while (fis.read(input) != -1) {}
            content += new String(input);
            //Toast.makeText(getBaseContext(),content,Toast.LENGTH_SHORT).show();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Intent i = new Intent(addressActivity.this, liveActivity.class);
        String strName = content.toString();
        i.putExtra("urlAddress", strName);
        startActivity(i);
    }
}

OnItemClickListener getFileEditContent = new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        String clickedFile = (String)parent.getItemAtPosition(position);
        setMultiEdit(clickedFile);
        // change the checkbox state
        CheckedTextView checkedTextView = ((CheckedTextView) view);
        if (checkedTextView.isChecked()) {

            ((CheckedTextView)view).setChecked(true);
            checkedTextView.setChecked(!checkedTextView.isChecked());
            utils.myPosInt.clear();
            utils.myPos.clear();
            utils.myPosTitle.clear();

        } else {
            for(int i=0; i<listView.getChildCount();i++)
            {
                view = listView.getChildAt(i);
                ((CheckedTextView)view).setChecked(false);
            }

            checkedTextView.setChecked(!checkedTextView.isChecked());
            utils.myPosTitle.add(clickedFile);
        }

    }
};

这是我的代码

Test.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ListView
        android:id="@+id/test_list"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</LinearLayout>

Test.java

public class Test extends AppCompatActivity
{
    @Bind(R.id.test_list)
    ListView listView;
    String[] SavedFiles;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test);
        ButterKnife.bind(this);

        ShowSavedFiles();
    }
    void ShowSavedFiles()
    {
        SavedFiles = getApplicationContext().fileList();
        ArrayAdapter<String> adapter
                = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1,
                SavedFiles);

        listView.setAdapter(adapter);
        if (adapter.isEmpty()) {
            Toast.makeText(getBaseContext(), "There is no Address !", Toast.LENGTH_LONG).show();
        }
    }

}

输出

来自文档

fileList()

fileList() : Returns an array of strings naming the private files associated with this Context's application package.

候补

String[] iFileList = YourActivityName.this.fileList();

希望这对您有所帮助..

更新

试试这个替代解决方案

    String dataDr=getApplicationInfo().dataDir;
    Log.i(TAG,"data dir"+dataDr);

    showDirFile(dataDr);


    void showDirFile(String dirpth)
    {
        String path = dirpth+"/files";
        Log.d("Files", "Path: " + path);
        File f = new File(path);
        File file[] = f.listFiles();
        Log.d("Files", "Size: "+ file.length);

        SavedFiles=new String[file.length];
        for (int i=0; i < file.length; i++)
        {
            Log.d("Files", "FileName:" + file[i].getName());

            SavedFiles[i]=file[i].getName();
        }

        ArrayAdapter<String> adapter
                = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1,
                SavedFiles);

        listView.setAdapter(adapter);


    }