getApplicationContext()。fileList()时的Java活动名称

时间:2016-08-22 01:21:22

标签: android listview android-activity android-arrayadapter samsung-mobile

在我的应用中的此活动中,它会调用geApplication().fileList()列出apps文件中的所有文件。

在我的Elephone P8000手机中,我设法创建新文件而没有任何错误,但当我在三星手机上尝试时,会出现此问题

enter image description here

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

事实上,根据文件末尾的名称判断,#34; MainActivity",这些是我活动的名称。

我该如何解决这个问题?

修改

我的全部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);
        }

    }
};

1 个答案:

答案 0 :(得分:0)

这是我的代码

<强>的test.xml

$schedule->command("queue:work --tries=3")->everyMinute();

<强> Test.java

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

<强>输出

output

来自文档的

fileList()

  

fileList():返回命名私有文件的字符串数组   与此Context的应用程序包相关联。

<强>交替

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

}

希望这会有所帮助..

<强>更新

尝试此备用解决方案

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